Last active
August 3, 2022 07:36
-
-
Save SoulSu/19c2c56e89ddfd00c5e559c51822209e to your computer and use it in GitHub Desktop.
golang aes ecb pKCS5Padding like java 3DES_ECB_PKCS5Padding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package idcardocr | |
import ( | |
"bytes" | |
"crypto/cipher" | |
"crypto/des" | |
) | |
// 3DES加密 | |
func ECB3DesEncrypt(content, key []byte) ([]byte, error) { | |
block, err := des.NewTripleDESCipher(key) | |
if err != nil { | |
return nil, err | |
} | |
ecb := NewECBEncrypter(block) | |
content = pKCS5Padding(content, block.BlockSize()) | |
crypted := make([]byte, len(content)) | |
ecb.CryptBlocks(crypted, content) | |
return crypted, nil | |
} | |
// 3DES解密 | |
func ECB3DesDecrypt(crypted, key []byte) ([]byte, error) { | |
block, err := des.NewTripleDESCipher(key) | |
if err != nil { | |
return nil, err | |
} | |
blockMode := NewECBDecrypter(block) | |
origData := make([]byte, len([]byte(crypted))) | |
blockMode.CryptBlocks(origData, []byte(crypted)) | |
origData = pKCS5UnPadding(origData) | |
return origData, nil | |
} | |
func pKCS5Padding(ciphertext []byte, blockSize int) []byte { | |
padding := blockSize - len(ciphertext)%blockSize | |
padtext := bytes.Repeat([]byte{byte(padding)}, padding) | |
return append(ciphertext, padtext...) | |
} | |
func pKCS5UnPadding(origData []byte) []byte { | |
length := len(origData) | |
// 去掉最后一个字节 unpadding 次 | |
unpadding := int(origData[length-1]) | |
return origData[:(length - unpadding)] | |
} | |
type ecb struct { | |
b cipher.Block | |
blockSize int | |
} | |
func newECB(b cipher.Block) *ecb { | |
return &ecb{ | |
b: b, | |
blockSize: b.BlockSize(), | |
} | |
} | |
// 引入golang官方 https://codereview.appspot.com/7860047/ | |
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:] | |
} | |
} | |
// end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment