在AngularJS中检索加密的cookie字符串值

时间:2017-08-16 10:25:38

标签: c# asp.net angularjs encryption cookies

在ASP.NET Web应用程序中,我加密了一个字符串值并将结果存储在Cookie中。现在,我想在AngularJS应用程序中检索cookie值。 问题是我无法在AngularJS中检索加密的cookie值。 有人有任何想法吗?

我用来将字符串值加密到字节数组的代码是:

    public static byte[] Encrypt(string emailId)
    {
        byte[] bytes = Encoding.ASCII.GetBytes(emailId);
        byte[] passwordBytes = Encoding.ASCII.GetBytes("HLCC1PQRSA72017");
        int passwordShiftIndex = 0;
        for (int i = 0; i < bytes.Length; i++)
        {
            bytes[i] = (byte)(bytes[i] + passwordBytes[passwordShiftIndex]);
            passwordShiftIndex = (passwordShiftIndex + 1) % passwordBytes.Length;
        }
        return bytes;
    }

此外,我使用ToBase64String()方法将字节数组转换为字符串并将其存储在Cookie中。

1 个答案:

答案 0 :(得分:0)

感谢您的时间和回复朋友。 我使用以下代码转换字节数组中的字符串值:

atob(stringValue);

此外,我使用以下代码解密字节数组,最后获取加密字符串:

    var str = 'The encrypted byte array'; //Insert here the encrypted byte array value that you have obtained after using the atob function.
    var b = []; // char codes
    var pKey='ABCD39493CDCCD'; //Insert here the password key which was used to encrypt the string.
    var b2=[];
    var passwordIndex= 0;
    var finalString='';

    for (var i = 0; i < str.length; ++i) 
    {
        var code = str.charCodeAt(i);
        b = b.concat([code]);
    }

    for (var i = 0; i < pKey.length; ++i) 
    {
        var code2 = pKey.charCodeAt(i);
        b2 = b2.concat([code2]);
    }

    for (var i = 0; i < b.length; i++)
    {
        b[i] = (b[i] - b2[passwordIndex]);
        passwordIndex = (passwordIndex + 1) % b2.length;
    }

    for(var i=0;i<b.length;i++)
    {
        finalString=finalString+(String.fromCharCode(b[i]));
    }
    return finalString;