正在显示
1 个修改的文件
包含
8 行增加
和
21 行删除
| @@ -40,7 +40,7 @@ func checkKeySize(key []byte) error { | @@ -40,7 +40,7 @@ func checkKeySize(key []byte) error { | ||
| 40 | } | 40 | } |
| 41 | 41 | ||
| 42 | // AES encrypt pkcs7padding CBC, key for choose algorithm | 42 | // AES encrypt pkcs7padding CBC, key for choose algorithm |
| 43 | -func AES_CBC_PKCS7_EncryptByte(plantText, key []byte) ([]byte, error) { | 43 | +func AesEncryptCBC(plantText, key []byte) ([]byte, error) { |
| 44 | err := checkKeySize(key) | 44 | err := checkKeySize(key) |
| 45 | if err != nil { | 45 | if err != nil { |
| 46 | return nil, err | 46 | return nil, err |
| @@ -49,8 +49,8 @@ func AES_CBC_PKCS7_EncryptByte(plantText, key []byte) ([]byte, error) { | @@ -49,8 +49,8 @@ func AES_CBC_PKCS7_EncryptByte(plantText, key []byte) ([]byte, error) { | ||
| 49 | if err != nil { | 49 | if err != nil { |
| 50 | return nil, err | 50 | return nil, err |
| 51 | } | 51 | } |
| 52 | - plantText = pKCS7Padding(plantText, block.BlockSize()) | ||
| 53 | - //偏转向量iv长度等于密钥key的长度 | 52 | + plantText = PKCS7Padding(plantText, block.BlockSize()) |
| 53 | + //偏转向量iv长度等于密钥key块大小 | ||
| 54 | iv := key[:block.BlockSize()] | 54 | iv := key[:block.BlockSize()] |
| 55 | blockModel := cipher.NewCBCEncrypter(block, iv) | 55 | blockModel := cipher.NewCBCEncrypter(block, iv) |
| 56 | 56 | ||
| @@ -60,7 +60,7 @@ func AES_CBC_PKCS7_EncryptByte(plantText, key []byte) ([]byte, error) { | @@ -60,7 +60,7 @@ func AES_CBC_PKCS7_EncryptByte(plantText, key []byte) ([]byte, error) { | ||
| 60 | return cipherText, nil | 60 | return cipherText, nil |
| 61 | } | 61 | } |
| 62 | 62 | ||
| 63 | -func AES_CBC_PKCS7_DecryptByte(cipherText, key []byte) ([]byte, error) { | 63 | +func AesDecryptCBC(cipherText, key []byte) ([]byte, error) { |
| 64 | err := checkKeySize(key) | 64 | err := checkKeySize(key) |
| 65 | if err != nil { | 65 | if err != nil { |
| 66 | return nil, err | 66 | return nil, err |
| @@ -70,37 +70,24 @@ func AES_CBC_PKCS7_DecryptByte(cipherText, key []byte) ([]byte, error) { | @@ -70,37 +70,24 @@ func AES_CBC_PKCS7_DecryptByte(cipherText, key []byte) ([]byte, error) { | ||
| 70 | if err != nil { | 70 | if err != nil { |
| 71 | return nil, err | 71 | return nil, err |
| 72 | } | 72 | } |
| 73 | - //偏转向量iv长度等于密钥key的长度 | 73 | + //偏转向量iv长度等于密钥key块大小 |
| 74 | iv := key[:block.BlockSize()] | 74 | iv := key[:block.BlockSize()] |
| 75 | blockModel := cipher.NewCBCDecrypter(block, iv) | 75 | blockModel := cipher.NewCBCDecrypter(block, iv) |
| 76 | plantText := make([]byte, len(cipherText)) | 76 | plantText := make([]byte, len(cipherText)) |
| 77 | blockModel.CryptBlocks(plantText, cipherText) | 77 | blockModel.CryptBlocks(plantText, cipherText) |
| 78 | - plantText = pKCS7UnPadding(plantText, block.BlockSize()) | 78 | + plantText = PKCS7UnPadding(plantText, block.BlockSize()) |
| 79 | return plantText, nil | 79 | return plantText, nil |
| 80 | } | 80 | } |
| 81 | 81 | ||
| 82 | //AES Decrypt pkcs7padding CBC, key for choose algorithm | 82 | //AES Decrypt pkcs7padding CBC, key for choose algorithm |
| 83 | -func pKCS7UnPadding(plantText []byte, blockSize int) []byte { | 83 | +func PKCS7UnPadding(plantText []byte, blockSize int) []byte { |
| 84 | length := len(plantText) | 84 | length := len(plantText) |
| 85 | unPadding := int(plantText[length-1]) | 85 | unPadding := int(plantText[length-1]) |
| 86 | return plantText[:(length - unPadding)] | 86 | return plantText[:(length - unPadding)] |
| 87 | } | 87 | } |
| 88 | 88 | ||
| 89 | -func pKCS7Padding(cipherText []byte, blockSize int) []byte { | 89 | +func PKCS7Padding(cipherText []byte, blockSize int) []byte { |
| 90 | padding := blockSize - len(cipherText)%blockSize | 90 | padding := blockSize - len(cipherText)%blockSize |
| 91 | padText := bytes.Repeat([]byte{byte(padding)}, padding) | 91 | padText := bytes.Repeat([]byte{byte(padding)}, padding) |
| 92 | return append(cipherText, padText...) | 92 | return append(cipherText, padText...) |
| 93 | } | 93 | } |
| 94 | - | ||
| 95 | -// func PKCS5Padding(cipherText []byte, blockSize int) []byte { | ||
| 96 | -// padding := blockSize - len(cipherText)%blockSize | ||
| 97 | -// padText := bytes.Repeat([]byte{byte(padding)}, padding) | ||
| 98 | -// return append(cipherText, padText...) | ||
| 99 | -// } | ||
| 100 | - | ||
| 101 | -// func PKCS5UnPadding(origData []byte) []byte { | ||
| 102 | -// length := len(origData) | ||
| 103 | -// // 去掉最后一个字节 unpadding 次 | ||
| 104 | -// unpadding := int(origData[length-1]) | ||
| 105 | -// return origData[:(length - unpadding)] | ||
| 106 | -// } |
-
请 注册 或 登录 后发表评论