Issue with Encrypting URL parameter in Javascript and Decrypting with C#

  • Thread starter Thread starter RickL.NYC
  • Start date Start date
R

RickL.NYC

Guest
Hi,


I have viewed almost every sample on the web about how to encrypt and encode a URL parameter in Javascript and decode and decrypt using C#, but none are working for me.

AES encrypt in Javascript and decrypt in C#

Encrypt in JavaScript and Decrypt in C# With AES Algorithm

Encrypt in JavaScript and Decrypt in C#

https://www.codeproject.com/Articles/11479/RSA-Interoperability-between-JavaScript-and-RSACry

AesCryptoServiceProvider Class (System.Security.Cryptography)


I receive errors like:

Invalid length for a Base-64 char array or string.

or

'Length of the data to decrypt is invalid.'



Here is my Javascript:

function Encrypt(value){

console.log("value: " + value);

var key = CryptoJS.enc.Utf8.parse("7061737323313233");
var iv = CryptoJS.enc.Utf8.parse("7061737323313233");

var encrypted = CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse(value), 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));

var encodedValue = btoa(unescape(encodeURIComponent(encrypted.toString())));
console.log("encodedValue: " + encodedValue);

return encodedValue.toString();

}


function openAws(event){
event.preventDefault()

//Gets form data
var lastname = document.getElementById("lastname").value;
var firstname = document.getElementById("firstname").value;
var email = document.getElementById("email").value;

var time1 = new Date();

var hour = time1.getUTCHours();
if(hour < 10)
hour = "0" + hour;

var min = time1.getUTCMinutes();
if(min < 10)
min = "0" + min;

var sec = time1.getUTCSeconds();
if(sec < 10)
sec = "0" + sec;

var timestamp = hour.toString() + min.toString() + sec.toString();

var encryptedValues = "encrypted values: " + encryptTestURL + "?lastname=" + Encrypt(lastname) + "&firstname=" + Encrypt(firstname) + "&email=" + Encrypt(email) + "&timestamp=" + Encrypt(timestamp);
console.log(encryptedValues);

}


And here is my C#:

public class EncryptionProxy
{
public string DecryptParams(string param)
{

var keyBytes = Encoding.UTF8.GetBytes("7061737323313233");
var iv = Encoding.UTF8.GetBytes("7061737323313233");


var encryptStringToBytes = EncryptStringToBytes(param, keyBytes, iv);

var roundTrip = DecryptStringFromBytes(encryptStringToBytes, keyBytes, iv);

var encrypted = Convert.FromBase64String(roundTrip);


var decriptedFromJavascript = DecryptStringFromBytes(encrypted, keyBytes, iv);
return string.Format(
"round result: {0}{1} javascript result {2}",
roundTrip,
encrypted,
decriptedFromJavascript);
}



public static string DecryptStringFromBytes(byte[] cypherText, byte[] key, byte[] iv)
{
if(cypherText == null || cypherText.Length <= 0)
{
throw new ArgumentException("cypherText");
}
if(key == null || key.Length <= 0)
{
throw new ArgumentException("key");
}
if(iv == null || iv.Length <= 0)
{
throw new ArgumentException("key");
}

//Declare String to be used
string plainText = null;

// Create an RijndaelManaged object
// with the specified key and IV.

using (var rijAlg = new RijndaelManaged())
{
rijAlg.Mode = CipherMode.CBC;
rijAlg.Padding = PaddingMode.PKCS7;
rijAlg.FeedbackSize = 128;

rijAlg.Key = key;
rijAlg.IV = iv;

var decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);

using (var msDecrypt = new MemoryStream(cypherText))
{
using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (var srDecrypt = new StreamReader(csDecrypt))
{
//Read Decrypted bytes
plainText = srDecrypt.ReadToEnd();
Logger.LogMessage(plainText);
}

}

}
}
return plainText;

}

private static byte[] EncryptStringToBytes(string plainText, byte[] key, byte[] iv)
{
// Check arguments.
if (plainText == null || plainText.Length <= 0)
{
throw new ArgumentNullException("plainText");
}
if (key == null || key.Length <= 0)
{
throw new ArgumentNullException("key");
}
if (iv == null || iv.Length <= 0)
{
throw new ArgumentNullException("key");
}

byte[] encrypted;

using(var rijAlg = new RijndaelManaged())
{
rijAlg.Mode = CipherMode.CBC;
rijAlg.Padding = PaddingMode.PKCS7;
rijAlg.FeedbackSize = 128;

rijAlg.Key = key;
rijAlg.IV = iv;

var encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);

using(var msEncrypt = new MemoryStream())
{
using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (var swEncrypt = new StreamWriter(csEncrypt))
{
//Write all data to stream
swEncrypt.Write(plainText);

}
encrypted = msEncrypt.ToArray();
}
}

}

// Return the encrypted bytes from the memory stream.
return encrypted;
}
}

Continue reading...
 
Back
Top