有什么方法可以解密pkcs7从红宝石中填充abs 256 cbc?

时间:2009-08-27 14:53:19

标签: c# ruby encryption aes rijndaelmanaged

在尝试使用相同的iv双方从c#程序解密字符串时,我经常得到“不良解密”。这有点烦人,我无法弄清楚问题。

这是红宝石代码

def unencrypt(message) 
 c = OpenSSL::Cipher::Cipher.new("aes-256-cbc")
 c.padding = 1
 c.decrypt
 c.key = key = Digest::SHA1.hexdigest("mytestiv1111111111111111111111111").unpack('a2'*32).map{|x| x.hex}.pack('c'*32)
 c.iv = iv = key
 e = c.update(Base64.decode64(message))
 e << c.final
 puts e
end

这就是c#侧加密的原因

public string  encrypt(string text, //the text to be encrypt
                       string password,// the encryption key
                       int cipherindex//The strength of the encryption
                      )
{
    try
    {    
        //get the cipher strength--from cipherindex
        CipherKey Key=CipherKey.getCipherKey(cipherindex);
        //build and init the Encryptor
        RijndaelManaged rijndaelCipher = new RijndaelManaged();
        rijndaelCipher.Mode = sCipherMode;
        rijndaelCipher.Padding = sPaddingMode;
        rijndaelCipher.KeySize = Key.Size;
        rijndaelCipher.BlockSize =Key.Size;
        byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(password);
        byte[] keyBytes = new byte[Key.Len]; 
        int len= pwdBytes.Length;
        if (len > keyBytes.Length) len= keyBytes.Length;
        System.Array.Copy(pwdBytes,keyBytes,len);
        rijndaelCipher.Key = keyBytes;
        rijndaelCipher.IV = keyBytes;
        ICryptoTransform transform = rijndaelCipher.CreateEncryptor();

        byte [] plainText = System.Text.Encoding.UTF8.GetBytes(text);
        byte[] cipherBytes = transform.TransformFinalBlock(plainText, 0, plainText.Length);
        return Convert.ToBase64String(cipherBytes);
    }
    catch (Exception e)
    {
        throw;
    }

}

有什么想法吗?干杯大卫

1 个答案:

答案 0 :(得分:1)

你的钥匙看起来与我完全不同,

  1. 在Ruby中,您使用SHA1派生密钥。
  2. 在C#中,密码是原始使用的。
  3. 在两个平台上转储二进制密钥缓冲区以确保它们是相同的。然后,你只需清理你的第一个障碍:)