Looking for assistance with encrypting data in TypeScript and then decrypting it in Go. The issue I'm facing is that the output in Go does not match the input in TypeScript. What could be causing this discrepancy?
Below is my TypeScript code:
import * as CryptoJS from 'crypto-js';
var key = CryptoJS.enc.Utf8.parse('7061737323313233');
var iv = CryptoJS.enc.Utf8.parse('7061737323313233');
var encrypted = CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse("Im new in aes encryption"), key,
{
keySize: 128 / 8,
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
var decrypted = CryptoJS.AES.decrypt(encrypted, key, {
keySize: 128 / 8,
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
console.log('Encrypted :' + encrypted);
console.log('Key :' + encrypted.key);
console.log('Salt :' + encrypted.salt);
console.log('iv :' + encrypted.iv);
console.log('Decrypted : ' + decrypted);
console.log('utf8 = ' + decrypted.toString(CryptoJS.enc.Utf8));
And here is my Go code:
func main() {
d2, _ := Decrypt("VMlk9qzp2BKZi5wZjlzst2iwEre0uD/VHVc6xm2bhXY=", "37303631373337333233333133323333")
fmt.Println(d2)
}
// Decrypt-function to decrypt the given cipher text string into plain text string
func Decrypt(encrypted string, CIPHER_KEY string) (string, error) {
key := []byte(CIPHER_KEY)
cipherText, _ := base64.StdEncoding.DecodeString(encrypted) ////hex.DecodeString(encrypted)
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
if len(cipherText)<aes.BlockSize {
panic("cipherText too short")
}
iv := cipherText[:aes.BlockSize]
cipherText = cipherText[aes.BlockSize:]
if len(cipherText)%aes.BlockSize!=0 {
panic("cipherText is not a multiple of the block size")
}
mode := cipher.NewCBCDecrypter(block, iv)
mode.CryptBlocks(cipherText, cipherText)
cipherText, _ = pkcs7.Pad(cipherText, aes.BlockSize)
return fmt.Sprintf("%s", cipherText), nil
}