简体   繁体   English

具有C#的AES加密功能并使用javascript解密

[英]AES crypt function with C# and decrypt using javascript

我正在尝试编写可以使用JavaScript解密的C#加密函数,是否有可能做到这一点?

For applications that require higher security I usually encode and decode using C#, and use $.ajax in JS, but there is an easy way using JS to decode a pre-encrypted string . 对于要求更高安全性的应用程序,我通常使用C#进行编码和解码,并在JS中使用$ .ajax,但是有一种使用JS解码预加密字符串的简便方法。

You can use Base64String... 您可以使用Base64String ...

C# : C# :

string encoded = Convert.ToBase64String(Encoding.UTF8.GetBytes(str));

JS : JS:

function decode(str) {
  var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  var output = "";
  var chr1, chr2, chr3;
  var enc1, enc2, enc3, enc4;
  var i = 0;

  do {
     enc1 = keyStr.indexOf(str.charAt(i++));
     enc2 = keyStr.indexOf(str.charAt(i++));
     enc3 = keyStr.indexOf(str.charAt(i++));
     enc4 = keyStr.indexOf(str.charAt(i++));

     chr1 = (enc1 << 2) | (enc2 >> 4);
     chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
     chr3 = ((enc3 & 3) << 6) | enc4;

     output = output + String.fromCharCode(chr1);

     if (enc3 != 64) {
        output = output + String.fromCharCode(chr2);
     }
     if (enc4 != 64) {
        output = output + String.fromCharCode(chr3);
     }
   } while (i < str.length);

  return output;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM