2013-09-30 76 views
1

我想在C#中执行AES加密并在CryptoJS中进行解密。C#中的AES加密和CryptoJS中的解密

+0

你的问题不解释你做了什么,试图解决问题;它目前的读取方式类似于代码请求。请分享您尝试的实施方式,并解释如何达不到您的要求。 –

回答

2

它从Google CryptoJS组获得一些参考(https://groups.google.com/forum/#!msg/crypto-js/ysgzr2Wxt_k/_Wh8l_1rhQAJ)后正在工作。

这里是C#.NET中的加密代码。

public class ClsCrypto 
     { 
      private RijndaelManaged myRijndael = new RijndaelManaged(); 
      private int iterations; 
      private byte [] salt; 

      public ClsCrypto(string strPassword) 
      { 
       myRijndael.BlockSize = 128; 
       myRijndael.KeySize = 128; 
       myRijndael.IV = HexStringToByteArray("e84ad660c4721ae0e84ad660c4721ae0"); 

       myRijndael.Padding = PaddingMode.PKCS7; 
       myRijndael.Mode = CipherMode.CBC; 
       iterations = 1000; 
       salt = System.Text.Encoding.UTF8.GetBytes("insight123resultxyz"); 
       myRijndael.Key = GenerateKey(strPassword); 
      } 

      public string Encrypt(string strPlainText) 
      { 
       byte [] strText = new System.Text.UTF8Encoding().GetBytes(strPlainText); 
       ICryptoTransform transform = myRijndael.CreateEncryptor(); 
       byte [] cipherText = transform.TransformFinalBlock(strText, 0, strText.Length); 

       return Convert.ToBase64String(cipherText); 
      } 

      public static byte [] HexStringToByteArray(string strHex) 
      { 
       dynamic r = new byte[strHex.Length/2]; 
       for (int i = 0; i <= strHex.Length - 1; i += 2) 
       { 
        r[i/2] = Convert.ToByte(Convert.ToInt32(strHex.Substring(i, 2), 16)); 
       } 
       return r; 
      } 

      private byte[] GenerateKey(string strPassword) 
      { 
       Rfc2898DeriveBytes rfc2898 = new   Rfc2898DeriveBytes(System.Text.Encoding.UTF8.GetBytes(strPassword), salt, iterations); 

       return rfc2898.GetBytes(128/8); 
      } 
     } 

以下是Java脚本中的解密代码。

<head runat="server"> 
 
      <script src="rollups/aes.js" type="text/javascript"></script> 
 
      <script src="rollups/sha256.js" type="text/javascript"></script> 
 
      <script src="rollups/pbkdf2.js" type="text/javascript"></script> 
 
      <script type="text/javascript"> 
 
       function DecryptData() { 
 
       var encryptData = document.getElementById('TextEncrypted').value; 
 
       var decryptElement = document.getElementById('TextDecrypt'); 
 
     
 
       try { 
 
        //Creating the Vector Key 
 
        var iv = CryptoJS.enc.Hex.parse('e84ad660c4721ae0e84ad660c4721ae0'); 
 
        //Encoding the Password in from UTF8 to byte array 
 
        var Pass = CryptoJS.enc.Utf8.parse('insightresult'); 
 
        //Encoding the Salt in from UTF8 to byte array 
 
        var Salt = CryptoJS.enc.Utf8.parse("insight123resultxyz"); 
 
        //Creating the key in PBKDF2 format to be used during the decryption 
 
        var key128Bits1000Iterations = CryptoJS.PBKDF2(Pass.toString(CryptoJS.enc.Utf8), Salt, { keySize: 128/32, iterations: 1000 }); 
 
        //Enclosing the test to be decrypted in a CipherParams object as supported by the CryptoJS libarary 
 
        var cipherParams = CryptoJS.lib.CipherParams.create({ 
 
         ciphertext: CryptoJS.enc.Base64.parse(encryptData) 
 
        }); 
 
     
 
        //Decrypting the string contained in cipherParams using the PBKDF2 key 
 
        var decrypted = CryptoJS.AES.decrypt(cipherParams, `enter code here`key128Bits1000Iterations, { mode: CryptoJS.mode.CBC, iv: iv, padding: CryptoJS.pad.Pkcs7 }); 
 
        decryptElement.value = decrypted.toString(CryptoJS.enc.Utf8); 
 
       } 
 
       //Malformed UTF Data due to incorrect password 
 
       catch (err) { 
 
        return ""; 
 
       } 
 
      } 
 
      </script> 
 
     </head>

+0

这是我的js编码/解码代码,补充您的建议: 'var cfg = {mode:CryptoJS.mode.CBC,iv:iv,padding:CryptoJS.pad.Pkcs7};' 'var encrypted = CryptoJS。 AES.encrypt(“MMMessageąćęł”,key,cfg);' 'var sEncrypted = CryptoJS.enc.Base64.stringify(encrypted.ciphertext);' 'var decrypted2 = CryptoJS.AES.decrypt(sEncrypted,key,cfg) ;' – Jarekczek

+0

注意IV应该在每次加密运行时随机启动。出于安全原因,你不能两次同样的IV。 https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Initialization_vector_.28IV.29 – Jarekczek