0

I am banging my head to the wall.

I have the following Perl code, and I am trying to do this in .NET without success:

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

my .net code is:

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();

        }

What am i doing wrong???

Thanks!

Himberjack
  • 5,682
  • 18
  • 71
  • 115

1 Answers1

1

Try with this:

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;
Steve
  • 213,761
  • 22
  • 232
  • 286
  • Perhaps this [question](http://stackoverflow.com/questions/569871/why-cant-c-sharp-decrypt-the-output-from-perls-cryptrijndael?rq=1) and the corresponding answer could help you – Steve Jul 16 '12 at 19:17