AES和SHA1从perl到.net

时间:2012-07-16 18:03:03

标签: c# .net perl aes

我正在把头撞到墙上。

我有以下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();

        }

我做错了什么???

谢谢!

1 个答案:

答案 0 :(得分: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;