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:]
        }
    }
  • 相关阅读:
    Windows7下安装golang语言开发环境和revel框架
    Go实战--通过gin-gonic框架搭建restful api服务(github.com/gin-gonic/gin)
    Go语言web框架 gin
    最好的6个Go语言Web框架
    mac:Go安装和配置+GoLand安装和使用之完整教程
    Goland软件使用教程(二)
    Goland软件使用教程(一)
    http_load常见问题及解决方案
    定义类和接口
    关于 xftp 上传文件时,仅仅是上传了0字节的问题
  • 原文地址:https://www.cnblogs.com/lavin/p/5920871.html
Copyright © 2011-2022 走看看