C# 関数を PHP に変換しようとしています。
C# 関数は次のとおりです。
public string Encrypt(string Value)
{
string RetVal = "";
if(Value != string.Empty && Value != null)
{
MemoryStream Buffer = new MemoryStream();
RijndaelManaged RijndaelManaged = new RijndaelManaged();
UnicodeEncoding UnicodeEncoder = new UnicodeEncoding();
byte[] KeyArray = new Byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
byte[] IVArray = new Byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
try
{
byte[] ValueBytes = UnicodeEncoder.GetBytes(Value);
CryptoStream EncryptStream = new CryptoStream(Buffer,
RijndaelManaged.CreateEncryptor(KeyArray, IVArray),
CryptoStreamMode.Write);
EncryptStream.Write(ValueBytes, 0, ValueBytes.Length);
EncryptStream.FlushFinalBlock();
// Base64 encode the encrypted data
RetVal = Convert.ToBase64String(Buffer.ToArray());
}
catch
{
throw;
}
}
return RetVal;
}
そして、ここにPHPでの私の試みがあります:
function EncryptString ($cleartext)
{
$cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, '');
$key128 = '111111111111111111111111111';
$iv = '111111111111111111111111111';
if (mcrypt_generic_init($cipher, $key128, $iv) != -1) //Parameter iv will be ignored in ECB mode
{
$cipherText = mcrypt_generic($cipher,$cleartext );
mcrypt_generic_deinit($cipher);
$encrypted = (bin2hex($cipherText));
return base64_encode($encrypted);
}
}
現在、これら 2 つの関数を使用してテスト フレーズ「test」をエンコードすると、異なる値が得られます。PHP バージョンでは文字列$key
と$iv
値が使用されるようですが、C# バージョンではバイト配列が使用されます。
PHP 関数を変更して C# 関数を模倣するにはどうすればよいですか?
[編集] c# 関数はサード パーティであり、変更する権限がありません。同じ方法で特定の文字列をエンコードするには、PHPで同等のものを書く必要があります