更新しました
ブロックサイズ256を使用するようにC#コードに変更を加えましたが、Hello Worldは次のようになりますhttp://pastebin.com/5sXhMV11で、rtrim()で何を使用すればよいかわかりません。最後に混乱の乗り物。
また、IVをランダムにする必要があると言う場合、これは、同じIVを2回以上使用しないことを意味しますか、それとも私がコーディングした方法が間違っていますか?
再度、感謝します!
やあ、
C#で暗号化されたPHPで文字列を復号化しようとしています。PHPにmcryptを使用して復号化させることができないようですので、助けを借りてください。phpで次のエラーが発生するので、IVを正しく設定していないと推測しています。
エラー:IVパラメータはブロックサイズと同じ長さである必要があります
両方の機能は同じ暗号、キー、IVを使用し、CBCモードに設定されています。
c#からの暗号化テキスト=UmzUCnAzThH0nMkIuMisqg==
キー32long= qwertyuiopasdfghjklzxcvbnmqwerty
iv 16 long = 1234567890123456
C#
public static string EncryptString(string message, string KeyString, string IVString)
{
byte[] Key = ASCIIEncoding.UTF8.GetBytes(KeyString);
byte[] IV = ASCIIEncoding.UTF8.GetBytes(IVString);
string encrypted = null;
RijndaelManaged rj = new RijndaelManaged();
rj.Key = Key;
rj.IV = IV;
rj.Mode = CipherMode.CBC;
try
{
MemoryStream ms = new MemoryStream();
using (CryptoStream cs = new CryptoStream(ms, rj.CreateEncryptor(Key, IV), CryptoStreamMode.Write))
{
using (StreamWriter sw = new StreamWriter(cs))
{
sw.Write(message);
sw.Close();
}
cs.Close();
}
byte[] encoded = ms.ToArray();
encrypted = Convert.ToBase64String(encoded);
ms.Close();
}
catch (CryptographicException e)
{
Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
return null;
}
catch (UnauthorizedAccessException e)
{
Console.WriteLine("A file error occurred: {0}", e.Message);
return null;
}
catch (Exception e)
{
Console.WriteLine("An error occurred: {0}", e.Message);
}
finally
{
rj.Clear();
}
return encrypted;
}
PHP
var $mcrypt_cipher = MCRYPT_RIJNDAEL_256;
var $mcrypt_mode = MCRYPT_MODE_CBC;
function decrypt($key, $iv, $encrypted)
{
$encrypted = base64_decode($encrypted);
$decrypted = rtrim(mcrypt_decrypt($this->mcrypt_cipher, $key, $encrypted, $this->mcrypt_mode, $iv), "\0");;
return $decrypted;
}
ありがとう