I am having the code to encrypt data on Angular, But I don't know how to decrypt on server side
var panno = CryptoJS.AES.encrypt("FEAPS8905Q", "myPassword").toString();
Encrypted as U2FsdGVkX19mi5mXlJ14Lj0XcJBbqMPDzi/UeNXK4Cw=
in angular,
after sending the encrypted using Http.post method, I am not getting the exact data, instead, getting 楀뢖᷈鍩ԏ건뫨샞일䜍钚䁞
I used this reference also Decrypting on C#, but I am getting some data like 壓섢⻫捼笺ﵑ戛ꔉ됒퍿誁累♟콒ꚦ
public string Decrypt(string cipherText)
{
string EncryptionKey = "myPassword";
cipherText = cipherText.Replace(" ", "+");
byte[] cipherBytes = Convert.FromBase64String(cipherText);
using (Aes encryptor = Aes.Create())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] {
0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32);
encryptor.Padding = PaddingMode.None;
encryptor.IV = pdb.GetBytes(16);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(cipherBytes, 0, cipherBytes.Length);
cs.Close();
}
cipherText = Encoding.Unicode.GetString(ms.ToArray());
}
}
return cipherText;
}
CryptoJS.AES.encrypt(text, password)
implicitly derives encryption key and iv from your password using derivation algorithm which is kind of not-native for C#. Instead of relying on that implicit derivation - it's better to explicitly do that yourself, using well known algorithm such as PBKDF2.
Key derivation is needed because your password can have arbitrary size, but given algorithm (AES) needs key of specific size, for example 256 bits. So we need to go from arbitrary length password to fixed size key (in irreversible way).
Sample javascript code:
function encrypt (msg, pass) {
// random salt for derivation
var keySize = 256;
var salt = CryptoJS.lib.WordArray.random(16);
// well known algorithm to generate key
var key = CryptoJS.PBKDF2(pass, salt, {
keySize: keySize/32,
iterations: 100
});
// random IV
var iv = CryptoJS.lib.WordArray.random(128/8);
// specify everything explicitly
var encrypted = CryptoJS.AES.encrypt(msg, key, {
iv: iv,
padding: CryptoJS.pad.Pkcs7,
mode: CryptoJS.mode.CBC
});
// combine everything together in base64 string
var result = CryptoJS.enc.Base64.stringify(salt.concat(iv).concat(encrypted.ciphertext));
return result;
}
Decrypting that in C# is now easy:
public static string Decrypt(string cipherText, string password) {
byte[] cipherBytes = Convert.FromBase64String(cipherText);
using (Aes encryptor = Aes.Create()) {
// extract salt (first 16 bytes)
var salt = cipherBytes.Take(16).ToArray();
// extract iv (next 16 bytes)
var iv = cipherBytes.Skip(16).Take(16).ToArray();
// the rest is encrypted data
var encrypted = cipherBytes.Skip(32).ToArray();
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(password, salt, 100);
encryptor.Key = pdb.GetBytes(32);
encryptor.Padding = PaddingMode.PKCS7;
encryptor.Mode = CipherMode.CBC;
encryptor.IV = iv;
// you need to decrypt this way, not the way in your question
using (MemoryStream ms = new MemoryStream(encrypted)) {
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Read)) {
using (var reader = new StreamReader(cs, Encoding.UTF8)) {
return reader.ReadToEnd();
}
}
}
}
}
If you understand the consequences, you can use fixed salt (or for example fixed salt per user in your application), and reduce number of iterations in PBKDF2. Don't use fixed IV though, and don't use part of key as IV either.
Encrypt in node.js:
var crypto = require('crypto');
var key = '00000000000000000000000000000000'; //replace with your key
var iv = '0000000000000000'; //replace with your IV
var cipher = crypto.createCipheriv('aes256', key, iv)
var crypted = cipher.update(authorizationKey, 'utf8', 'base64')
crypted += cipher.final('base64');
console.log(crypted);
decrypt c#
string keyString = "00000000000000000000000000000000"; //replace with your key
string ivString = "0000000000000000"; //replace with your iv
byte[] key = Encoding.ASCII.GetBytes(keyString);
byte[] iv = Encoding.ASCII.GetBytes(ivString);
using (var rijndaelManaged =
new RijndaelManaged { Key = key, IV = iv, Mode = CipherMode.CBC })
{
rijndaelManaged.BlockSize = 128;
rijndaelManaged.KeySize = 256;
using (var memoryStream =
new MemoryStream(Convert.FromBase64String(AuthorizationCode)))
using (var cryptoStream =
new CryptoStream(memoryStream,
rijndaelManaged.CreateDecryptor(key, iv),
CryptoStreamMode.Read))
{
return new StreamReader(cryptoStream).ReadToEnd();
}
}
source: https://gsferreira.com/archive/2015/02/how-to-encrypt-in-nodejs-and-decrypt-in-c-sharp/
work for me !
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With