简体   繁体   中英

Length of the data to decrypt is invalid. Encrypted with Javascript and Decrypted with c#

I have to Encrypt and Encode a URL using Javascript and then decrypt and decode using C#.

If I run the code in Debug using Visual Studio. It performs fine. But when I call the application using an URL from a form, it fails with a Error: "Length of the data to decrypt is invalid."

What am I doing wrong here? I've reviewed multiple tutorials but can't find and fix the issue.

Encryption Javascript Code:

function Encrypt(value){
    var key = CryptoJS.enc.Utf8.parse("1234567812345678");
    var iv = CryptoJS.enc.Utf8.parse("1234567812345678");

    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 encodedValue = window.btoa(encrypted);
    console.log("encodedValue: " + encodedValue);

    var decodedValue = window.atob(encodedValue);
    console.log("decodedValue: " + decodedValue);

    return encodedValue;
}

function openAws(event) {
    event.preventDefault()

    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 = encryptTestURL + "?lastname=" + Encrypt(lastname) + "&firstname=" + Encrypt(firstname) + "&email=" + Encrypt(email) + "&timestamp=" + Encrypt(timestamp);
}

Decryption C# Code:

public string DecryptParams(string param)
{
    var keyBytes = Encoding.UTF8.GetBytes("1234567812345678");
    var iv = Encoding.UTF8.GetBytes("1234567812345678");

    //Decrypt from CryptoJS
    var decodedParam = HttpUtility.UrlDecode(param);
    var encrypted = Convert.FromBase64String(decodedParam);
    var decriptedFromJavascript = DecryptedStringFromBytes(encrypted, keyBytes, iv);

    return decriptedFromJavascript;
}

public static string DecryptedStringFromBytes(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.None;
        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();
                }
            }
        }
    }
    return plainText;
}

I'll have a guess here that maybe your URL param parser might get confused with base64 decoding because of '=' padding characters. On top of that, input / output of AES functions must be block aligned. Seeing you use a 128 bit key / IV, you must make sure you supply the encryption / decryption functions with 16 byte aligned size (unless clearly stated otherwise by your framework).

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