Reputation: 63
I use AES-266-CBC to encrypt data:
const iv = crypto.randomBytes(8).toString('hex');
const encrypt = (text: string): string => {
log('encrypt');
const cipher = crypto.createCipheriv('aes-256-cbc', ENCRYPTION_KEY, iv);
let encrypted = cipher.update(text, 'utf8', 'hex');
encrypted += cipher.final('hex');
return encrypted;
};
const decrypt = (text: string): string => {
log('decrypt');
const decipher = crypto.createDecipheriv('aes-256-cbc', ENCRYPTION_KEY, iv);
let decrypted = decipher.update(text, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
};
And everything works perfectly that means if i use this:
const data = JSON.stringify({
user: 'MHF',
info: {
age: 23,
family: 'HF'
}
});
const encrypted = encrypt(data);
console.log('encrypted: %s', encrypted);
const decrypted = decrypt(encrypted);
console.log('decrypted: %s', decrypted);
The problem is that when i send my encrypted string by a post request http like this:
POST {{baseUrl}}/v1/user-register
Content-Type: application/json
Content-Encrypted: AES
{"payLoad": "3f1d1584b0e1976ccea311b5fbe0b2aee1034198a582a3b8bcc773c175e91bb0ceda6b9fb88aff11a892fa7adb83d432"}
I have a decrypted data by some incorrect chars like this:
'r!!(h\x7F!o#L\x19\x10~\x7F"jnfo":{"age":23,"family":"HF"}}'
Why When i do a post request by encrypted data i have this result?
What is this: 'r!!(h\x7F!o#L\x19\x10~\x7F"j'?
Upvotes: 0
Views: 288
Reputation: 63
Thanks to @Topaco and @Michael Fehr
It was my big mistake about generating and using IV
Upvotes: 1