1

両側で同じ iv を使用して ac# プログラムから文字列の暗号化を解除しようとしているときに、常に「不正な復号化」が発生します。これは少し面倒になり、問題を本当に理解できません。

これがルビーコードです

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;
    }

}

何か案は?乾杯デビッド

4

1 に答える 1