zoukankan      html  css  js  c++  java
  • blowfish ECB decode

    blowfish  ECB  Decode

    package main
    
    import (
        "crypto/cipher"
        "encoding/hex"
        "fmt"
        "golang.org/x/crypto/blowfish"
    )
    
    var key string = "your key"
    
    func main() {
        ci, err := blowfish.NewCipher([]byte(key))
        if err != nil {
            panic(err)
        }
        s := NewECBDecrypter(ci)
        data, _ := hex.DecodeString("price")
        dst := make([]byte, len(data))
        s.CryptBlocks(dst, data)
        fmt.Println(string(dst))
    }
    
    
    type ECB struct {
        b         cipher.Block
        blockSize int
    }
    
    func NewECB(b cipher.Block) *ECB {
        return &ECB{
            b:         b,
            blockSize: b.BlockSize(),
        }
    }
    
    type ECBEncrypter ECB
    
    // NewECBEncrypter returns a BlockMode which encrypts in electronic code book
    // mode, using the given Block.
    func NewECBEncrypter(b cipher.Block) cipher.BlockMode {
        return (*ECBEncrypter)(NewECB(b))
    }
    func (x *ECBEncrypter) BlockSize() int { return x.blockSize }
    func (x *ECBEncrypter) CryptBlocks(dst, src []byte) {
        if len(src)%x.blockSize != 0 {
            panic("crypto/cipher: input not full blocks")
        }
        if len(dst) < len(src) {
            panic("crypto/cipher: output smaller than input")
        }
        for len(src) > 0 {
            x.b.Encrypt(dst, src[:x.blockSize])
            src = src[x.blockSize:]
            dst = dst[x.blockSize:]
        }
    }
    
    type ECBDecrypter ECB
    
    // NewECBDecrypter returns a BlockMode which decrypts in electronic code book
    // mode, using the given Block.
    func NewECBDecrypter(b cipher.Block) cipher.BlockMode {
        return (*ECBDecrypter)(NewECB(b))
    }
    func (x *ECBDecrypter) BlockSize() int {
        return x.blockSize
    }
    func (x *ECBDecrypter) CryptBlocks(dst, src []byte) {
        if len(src)%x.blockSize != 0 {
            panic("crypto/cipher: input not full blocks")
        }
        if len(dst) < len(src) {
            panic("crypto/cipher: output smaller than input")
        }
        for len(src) > 0 {
            x.b.Decrypt(dst, src[:x.blockSize])
            src = src[x.blockSize:]
            dst = dst[x.blockSize:]
        }
    }
  • 相关阅读:
    基于http实现网络yum源搭建
    基于长轮询简易版聊天室
    放大镜案例
    弹出登录框
    拖拽案例
    js入门之DOM动态创建数据
    heoi2020游记
    省选模拟6&7
    省选模拟5
    后缀自动机总结
  • 原文地址:https://www.cnblogs.com/lavin/p/5920871.html
Copyright © 2011-2022 走看看