2
 static byte[] keyBytes = new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
                                              1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
                                              1, 1, 1, 1, 1, 1, 1, 1
                                            };
    static byte[] iv = new byte[] { 1, 1, 1, 1 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };

    static SymmetricAlgorithm getKey()
    {
        RijndaelManaged key = new RijndaelManaged();
        key.Key = keyBytes;
        key.IV = iv;
        return key;
    }

    static string Encrypt(string PlainText)
    {
        SymmetricAlgorithm key = getKey();
        MemoryStream ms = new MemoryStream();
        CryptoStream encStream = new CryptoStream(ms, key.CreateEncryptor(), CryptoStreamMode.Write);
        StreamWriter sw = new StreamWriter(encStream);
        sw.WriteLine(PlainText);
        sw.Close();
        encStream.Close();
        byte[] buffer = ms.ToArray();
        ms.Close();
        return Convert.ToBase64String(buffer);
    }

    static string Decrypt(string encrypted)
    {
        SymmetricAlgorithm key = getKey();
        byte[] CypherText = Convert.FromBase64String(encrypted);
        MemoryStream ms = new MemoryStream(CypherText);
        CryptoStream encStream = new CryptoStream(ms, key.CreateDecryptor(), CryptoStreamMode.Read);
        StreamReader sr = new StreamReader(encStream);
        string val = sr.ReadLine();
        sr.Close();
        encStream.Close();
        ms.Close();
        return val;
    }

Obviously the key and iv have been changed to all "ones" to protect the guilty.

I have tried several other SO article on here but to no avail.

I thought i'd go ahead and show the ruby openssl code i'm trying to use:

def Crypt.decrypt(encrypted_data, key, iv, cipher_type)
    aes = OpenSSL::Cipher::Cipher.new(cipher_type)
    aes.decrypt
    #aes.padding = 1
    aes.key = key
    aes.iv = iv if iv != nil
    aes.update(encrypted_data) + aes.final  
  end
fregas
  • 3,192
  • 3
  • 25
  • 41

1 Answers1

2

It appears that the OpenSSL module for Ruby uses PKCS5 type padding by default (http://www.ruby-doc.org/stdlib-1.9.3/libdoc/openssl/rdoc/OpenSSL/Cipher.html). But the Microsoft Rj-Managed uses PKCS7 as default. PKCS7 is supported in the Ruby module, so you may just want to stick with that (http://www.ruby-doc.org/stdlib-1.9.3/libdoc/openssl/rdoc/OpenSSL/PKCS7.html). Personally, since encryption is so finicky, I like to manually set every option rather than go by the default, even if I am using the default setting just to make sure they are the same on both ends. I've done this before with C#/PHP, check out my post to see if that helps at all (C# Encryption to PHP Decryption) my completed solution is posted as the second answer.

Community
  • 1
  • 1
solidau
  • 4,021
  • 3
  • 24
  • 45
  • so you're saying i should use the OpenSSL::PKCS7 class directly to decrypt? I've never really done that before but i'll give it a shot and get back to you. – fregas Sep 27 '12 at 14:57
  • ha okay, well i'm completely lost. I'm not sure how to use this thing at all. Would you be able to whip up a code example? – fregas Sep 27 '12 at 15:15
  • sorry, i dont really code ruby... but i looked around for an example and i think this might be close: http://stackoverflow.com/questions/9062830/in-ruby-rails-how-to-decrypt-a-string-encrypted-and-signed-by-pkcs7 the relevant line for you is probably going to be encrypted_data = OpenSSL::PKCS7::encrypt([@paypal_cert], signed_data.to_der, cypher, OpenSSL::PKCS7::BINARY) – solidau Sep 27 '12 at 17:10