• 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:]
        }
    }
  • 相关阅读:
    PHPStrom 转 VSCode 折腾记录
    vscode php 代码提示 自动完成
    Elasticsearch中文分词加拼音
    AutoMapper用法
    删除所有退出状态的容器
    Linux 安装Docker
    千里眼的修练方法--末法时代即将结束
    Visual NMP
    c#通过反射获取类上的自定义特性
    微信小程序学习笔记
  • 原文地址:https://www.cnblogs.com/lavin/p/5920871.html
Copyright © 2020-2023  润新知