简体   繁体   中英

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!

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 !!!

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.

First, your Key variable in JS contains a string with 32 characters (after the odd-looking parse call). Although this might be interpreted as a 128-bit key, there is a certain chance that CryptoJS takes it as a pass phrase instead (and generates a key from it using some algorithm). So your actual key looks quite different. The string also looks suspiciously like hex-encoded, so there might be some additional confusion about its C# value. You have to make sure that you are using the same key in JS and C#.

Second, the IV variable also, after parsing, looks like a hex-encoded value. So you have to be careful what value you are using on the C# side as well.

FYI, here are the values for Key and IV after parsing: Key = 00000000000e00000d000c0000010000 , IV = 0000000e000f0a00

Thank you "Uwe" parsing with UTF8 solved everything.

What happens if you use: var Key = CryptoJS.enc.Utf8.parse("PSVJQRk9QTEpNVU1DWUZCRVFGV1VVT0="); instead >of HEX? And what is your Key and IV in C#? Because actually the string you are putting in your key and >parsing is not a hex string. hex is 0 to F

Thank you so much!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM