Created
March 10, 2024 06:52
-
-
Save lhlyu/eb3958429179df0d4f999133f6808c0b to your computer and use it in GitHub Desktop.
Go和CryptoJS通用AES加密解密
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 utils | |
// go 版本号:1.22.0 | |
import ( | |
"bytes" | |
"crypto/aes" | |
"crypto/cipher" | |
"encoding/base64" | |
"fmt" | |
) | |
var iv = []byte("0000000000000000") | |
func AesEncrypt(text, secret string) string { | |
block, err := aes.NewCipher(paddingSecret(secret)) | |
if err != nil { | |
panic(err) | |
} | |
// 使用PKCS7填充 | |
plaintext := PKCS7Padding([]byte(text), block.BlockSize()) | |
blockMode := cipher.NewCBCEncrypter(block, iv) | |
encrypted := make([]byte, len(plaintext)) | |
blockMode.CryptBlocks(encrypted, plaintext) | |
return base64.StdEncoding.EncodeToString(encrypted) | |
} | |
func AesDecrypt(text, secret string) string { | |
ciphertext, err := base64.StdEncoding.DecodeString(text) | |
if err != nil { | |
panic(err) | |
} | |
block, err := aes.NewCipher(paddingSecret(secret)) | |
if err != nil { | |
panic(err) | |
} | |
// 解密器 | |
blockMode := cipher.NewCBCDecrypter(block, iv) | |
blockMode.CryptBlocks(ciphertext, ciphertext) | |
// 移除填充 | |
plaintext := PKCS7UnPadding(ciphertext, block.BlockSize()) | |
return string(plaintext) | |
} | |
// PKCS7Padding 填充 | |
func PKCS7Padding(ciphertext []byte, blockSize int) []byte { | |
padding := blockSize - len(ciphertext)%blockSize | |
plaintext := bytes.Repeat([]byte{byte(padding)}, padding) | |
return append(ciphertext, plaintext...) | |
} | |
// PKCS7UnPadding 负责去除补码 | |
func PKCS7UnPadding(plaintext []byte, blockSize int) []byte { | |
length := len(plaintext) | |
padding := int(plaintext[length-1]) | |
return plaintext[:(length - padding)] | |
} | |
// 填充密码,不足在前面补充0 | |
func paddingSecret(secret string) []byte { | |
l := len(secret) | |
if l < 16 { | |
return []byte(fmt.Sprintf("%016s", secret)) | |
} | |
if l < 24 { | |
return []byte(fmt.Sprintf("%024s", secret)) | |
} | |
if l < 32 { | |
return []byte(fmt.Sprintf("%032s", secret)) | |
} | |
// 超过32位的截取 | |
return []byte(secret[:32]) | |
} |
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
import * as CryptoJS from 'crypto-js' | |
// crypto-js 版本号:4.2.0 | |
const ivValue = ''.padStart(16, '0') | |
const paddingSecret = (secret: string): string => { | |
const l = secret.length | |
if (l < 16) { | |
return secret.padStart(16, '0') | |
} | |
if (l < 24) { | |
return secret.padStart(24, '0') | |
} | |
if (l < 32) { | |
return secret.padStart(32, '0') | |
} | |
return secret.substring(0, 32) | |
} | |
// 加密方法 | |
export const encryptText = (text: string, secret: string = ''): string => { | |
const key = CryptoJS.enc.Utf8.parse(paddingSecret(secret)) | |
const iv = CryptoJS.enc.Utf8.parse(ivValue) | |
const encode = CryptoJS.AES.encrypt(text, key, { | |
iv: iv, | |
padding: CryptoJS.pad.Pkcs7, | |
mode: CryptoJS.mode.CBC | |
}) | |
return encode.toString() | |
} | |
// 解密方法 | |
export const decryptText = (text: string, secret: string = ''): string => { | |
const key = CryptoJS.enc.Utf8.parse(paddingSecret(secret)) | |
const iv = CryptoJS.enc.Utf8.parse(ivValue) | |
return CryptoJS.AES.decrypt(text, key, { | |
iv: iv, | |
padding: CryptoJS.pad.Pkcs7, | |
mode: CryptoJS.mode.CBC | |
}).toString(CryptoJS.enc.Utf8) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment