2012-07-16 71 views
0

我正在将我的头撞到墙上。AES和SHA1从Perl到.net

我有以下Perl代码,我试图做到这一点在.NET没有成功:

my $cipher = Crypt::CBC->new(
      -iv => $iv, 
      -literal_key => 1, 
      -key => $key, 
      -cipher => "Crypt::OpenSSL::AES", 
      -blocksize => length($iv), 
      -keysize => length($key), 
      -header => "none" 
      ); 

我的.NET代码:

byte[] iv = new byte[] { 0x01,..... }; 
byte[] key = new byte[] { 0x01..... }; 

      RijndaelManaged aes = new RijndaelManaged(); 
      aes.Key = key; 
      aes.IV = iv; 

encryptStringToBytes_AES("bla bla bla", aes.KEY, aes.IV) 

... 
static byte[] encryptStringToBytes_AES(string plainText, byte[] Key, byte[] IV) 
     { 
      // Check arguments. 
      if (plainText == null || plainText.Length <= 0) 
       throw new ArgumentNullException("plainText"); 
      if (Key == null || Key.Length <= 0) 
       throw new ArgumentNullException("Key"); 
      if (IV == null || IV.Length <= 0) 
       throw new ArgumentNullException("Key"); 

      // Declare the stream used to encrypt to an in memory 
      // array of bytes. 
      MemoryStream msEncrypt = null; 

      // Declare the RijndaelManaged object 
      // used to encrypt the data. 
      RijndaelManaged aesAlg = null; 

      try 
      { 
       // Create a RijndaelManaged object 
       // with the specified key and IV. 
       aesAlg = new RijndaelManaged(); 
       aesAlg.Key = Key; 
       aesAlg.IV = IV; 

       // Create a decrytor to perform the stream transform. 
       ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV); 

       // Create the streams used for encryption. 
       msEncrypt = new MemoryStream(); 
       using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) 
       { 
        using (StreamWriter swEncrypt = new StreamWriter(csEncrypt)) 
        { 

         //Write all data to the stream. 
         swEncrypt.Write(plainText); 
        } 
       } 

      } 
      finally 
      { 
       // Clear the RijndaelManaged object. 
       if (aesAlg != null) 
        aesAlg.Clear(); 
      } 

      // Return the encrypted bytes from the memory stream. 
      return msEncrypt.ToArray(); 

     } 

我到底做错了什么? ??

谢谢!

+0

到底哪里出问题了?你得到不正确的结果?你有错误吗? – 2012-07-16 18:09:06

+0

我没有得到相同的结果 – Himberjack 2012-07-16 18:13:41

回答

1

尝试这样的:

UnicodeEncoding UE = new UnicodeEncoding(); 
byte[] bfr; 
byte[] pwdBits = UE.GetBytes(plainText); 
byte[] result; 

int extends = (1 + (pwdBits.Length/16)) * 16; 
bfr = new byte[extends]; 

pwdBits.CopyTo(bfr, 0); 

using (MemoryStream msCrypt = new MemoryStream()) 
{ 
    RijndaelManaged RMCrypto = new RijndaelManaged(); 
    RMCrypto.Padding = PaddingMode.PKCS7; 
    using (ICryptoTransform encriptor = RMCrypto.CreateEncryptor(Key, IV)) 
    { 
     using (CryptoStream cs = new CryptoStream(msCrypt, encriptor, CryptoStreamMode.Write)) 
     { 
       cs.Write(bfr, 0, bfr.Length); 
       cs.FlushFinalBlock(); 
       result = msCrypt.ToArray(); 
       cs.Close(); 
     } 
     msCrypt.Close(); 
    } 
} 
return result; 
+0

不起作用... – Himberjack 2012-07-16 19:10:39

+0

也许这[问题](http://stackoverflow.com/questions/569871/why-cant-c-sharp-decrypt-the-output-from -perls-cryptrijndael?rq = 1),相应的答案可以帮助你 – Steve 2012-07-16 19:17:52