我正在尝试在 golang 服务器上加密字符串,我有一个 aes-128 加密实用程序
func EncryptAES(key []byte, plaintext string) (string, error) {
// create cipher
c, err := aes.NewCipher(key)
if err != nil {
return "", err
}
// allocate space for ciphered data
out := make([]byte, len(plaintext))
for i := 1; i <= len(plaintext)/16; i++ {
tempBuf := make([]byte, 16)
offset := (i - 1) * 16
limit := offset + 16
// encrypt
c.Encrypt(tempBuf, []byte(plaintext[offset:limit]))
for j := 0; j < len(tempBuf); j++ {
out[offset+j] = tempBuf[j]
}
}
// return hex string
return hex.EncodeToString(out), nil
}
在那之后我正在尝试做类似的事情
hasher := sha256.New()
hasher.Write([]byte(word))
sha := hasher.Sum(nil)
cypherText := word + userSessionKey
for len([]byte(cypherText))%16 != 0 {
cypherText += "0"
}
sha128 := sha[:len(sha)/2]
captchaKey, err := utils.EncryptAES([]byte(hex.EncodeToString(sha128)), cypherText)
if err != nil {
SendErrorResponse(ctx, fasthttp.StatusInternalServerError, []byte("error generating aes session key "+err.Error()))
return
}
但是对于这个密钥和密文,我在加密/解密 aes 网站上收到这个错误
用于解码的 js func 也不起作用
async function decryptAES128(key, cipherText){
let hash = CryptoJS.SHA256(key).toString();
hash = hash.substring(0, hash.length/2);
console.log(hash);
console.log(cipherText)
const bytes = await CryptoJS.AES.decrypt(CryptoJS.enc.Hex.parse(cipherText), CryptoJS.enc.Hex.parse(hash), { mode: CryptoJS.mode.ECB });
return CryptoJS.enc.Utf8.stringify(bytes.words)
}
什么都不打印
月关宝盒
相关分类