Encrypt text using AES in Javascript then Decrypt in C# WCF Service -
i trying encrypt string using aes 128bit encryption. have code both javascript , c#. main objective encrypt string using javascript cryptojs , take resultant cipher text , decrypt 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" plain text javascript ciphertext:
uqhe5ya+misuk4uc1wxxeq==
when passing c# application, upon running decrypt method recieve:
padding invalid , cannot removed.
i stumped here , have tried many solutions resulting in same error.
when encrypting hello through c# encryption aes method receive:
y9nb8drv73+rmmyrucjiog==
i thank in advance!
try using var key = cryptojs.enc.utf8.parse("psvjqrk9qtepnvu1dwuzcrvfgv1vvt0="); instead of hex.
because string putting in key (and iv) , parsing not hex string. hex 0 f.
Comments
Post a Comment