【问题标题】:AES and SHA1 from perl to .net从 perl 到 .net 的 AES 和 SHA1
【发布时间】:2012-07-16 18:03:03
【问题描述】:

我正用头撞墙。

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

        }

我做错了什么???

谢谢!

【问题讨论】:

  • 出了什么问题?你得到不正确的结果吗?你有错误吗?

标签: c# .net perl aes


【解决方案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;

【讨论】:

  • 也许这个question和相应的答案可以帮助你
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-29
  • 1970-01-01
相关资源
最近更新 更多