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:]
    }
}

 

posted on 2016-09-29 18:24  iokde.com  阅读(740)  评论(0编辑  收藏  举报

导航