Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encrypt text using AES in Javascript then Decrypt in C# WCF Service

I am trying to Encrypt a string using AES 128bit encryption. I have code for both Javascript and C#. The main objective is to encrypt the string using Javascript CryptoJS and then take the resultant cipher text and Decrypt it using C# AES AesCryptoServiceProvider.

Javascript Code:

function EncryptText()
{
var text = document.getElementById('textbox').value;
var Key = CryptoJS.enc.Hex.parse("PSVJQRk9QTEpNVU1DWUZCRVFGV1VVT0=");
var IV = CryptoJS.enc.Hex.parse("YWlFLVEZZUFNaWl=");
var encryptedText = CryptoJS.AES.encrypt(text, Key, {iv: IV, mode: CryptoJS.mode.CBC, padding:     CryptoJS.pad.Pkcs7});
//var decrypted = CryptoJS.AES.decrypt(encrypted, "Secret Passphrase");
var encrypted = document.getElementById('encrypted');
encrypted.value = encryptedText;
}

C# Code:

private String AES_decrypt(string encrypted)
    {
        byte[] encryptedBytes = Convert.FromBase64String(encrypted);
        AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
        aes.BlockSize = 128;
        aes.KeySize = 256;
        aes.Mode = CipherMode.CBC;
        aes.Padding = PaddingMode.Pkcs7;
        aes.Key = Key;
        aes.IV = IV;
        ICryptoTransform crypto = aes.CreateDecryptor(aes.Key, aes.IV);
        byte[] secret = crypto.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length);
        crypto.Dispose();
        return System.Text.ASCIIEncoding.ASCII.GetString(secret);
    }

When using "hello" as the plain text for javascript i get this ciphertext:

uqhe5ya+mISuK4uc1WxxeQ==

When passing that into the C# application, upon running the Decrypt method i recieve:

Padding is invalid and cannot be removed.

I am stumped here and have tried many solutions resulting in the same error.

When encrypting hello through the C# encryption AES method I receive:

Y9nb8DrV73+rmmYRUcJiOg==

I thank you for your help in advance!

like image 572
EpicJoker Avatar asked Nov 19 '14 14:11

EpicJoker


2 Answers

javascript code :

function EncryptText()
{
   var text = CryptoJS.enc.Utf8.parse(document.getElementById('textbox').value);
   var Key = CryptoJS.enc.Utf8.parse("PSVJQRk9QTEpNVU1DWUZCRVFGV1VVT0="); //secret key
   var IV = CryptoJS.enc.Utf8.parse("2314345645678765"); //16 digit
   var encryptedText = CryptoJS.AES.encrypt(text, Key, {keySize: 128 / 8,iv: IV, mode: CryptoJS.mode.CBC, padding:CryptoJS.pad.Pkcs7});
   var encrypted = document.getElementById('encrypted');
   encrypted.value = encryptedText;
   //Pass encryptedText through service 

}

C# code :

private String AES_decrypt(string encrypted,String secretKey,String initVec)
{
    byte[] encryptedBytes = Convert.FromBase64String(encrypted);
    AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
    //aes.BlockSize = 128; Not Required
    //aes.KeySize = 256; Not Required
    aes.Mode = CipherMode.CBC;
    aes.Padding = PaddingMode.Pkcs7;
    aes.Key = Encoding.UTF8.GetBytes(secretKey);PSVJQRk9QTEpNVU1DWUZCRVFGV1VVT0=
    aes.IV = Encoding.UTF8.GetBytes(initVec); //2314345645678765
    ICryptoTransform crypto = aes.CreateDecryptor(aes.Key, aes.IV);
    byte[] secret = crypto.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length);
    crypto.Dispose();
    return System.Text.ASCIIEncoding.ASCII.GetString(secret);
}

Used above code working fine !!!

like image 132
Praj Avatar answered Oct 29 '22 20:10

Praj


Try using var Key = CryptoJS.enc.Utf8.parse("PSVJQRk9QTEpNVU1DWUZCRVFGV1VVT0="); instead of HEX.
Because actually the string you are putting in your key (and IV) and parsing is not a hex string. hex is 0 to F.

like image 3
Uwe Hafner Avatar answered Oct 29 '22 21:10

Uwe Hafner