2017-08-16 27 views
0

在ASP.NET Web應用程序中,我加密了字符串值並將結果存儲在Cookie中。現在,我想要在AngularJS應用程序中檢索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中。

+0

請提供您嘗試檢索cookie的JS代碼片段。 –

+0

我不認爲你可以把它算作第一個地方的正確加密....直到你不妨將電子郵件ID存儲在標題中 –

+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;