BouncyCastle c# cipher ライブラリを使用して暗号化を行う方法を学んでいます。メッセージを送信するつもりはないので、セキュリティなどについては考えていません。Visual Studio で C# コードを作成しました。
これが問題です。Twofish を CFB モードで使用して、 「 Hello World! 」というテキストを暗号化しました。キーは 1234567812345678 です。phpfiddle http://phpfiddle.org/オンライン ツールを使用しました。
$algo = 'twofish';
$mode = 'cfb';
$cipher = mcrypt_module_open($algo,'',$mode,'');
$key = hex2bin('31323334353637383132333435363738'); //1234567812345678
$iv = hex2bin('00000000000000000000000000000000');
mcrypt_generic_init($cipher, $key, $iv);
$plaintext = utf8_encode('Hello World!');
$encrypted = mcrypt_encrypt($algo, $key, $plaintext, $mode, $iv);
printf("<br>Encrypted text: %s<br><br>",base64_encode($encrypted));
$decrypted = mcrypt_decrypt($algo, $key, $encrypted, $mode, $iv);
printf("<br>Decrypted text: %s (%s)<br><br>",$decrypted,bin2hex($decrypted));
mcrypt_generic_deinit($cipher);
mcrypt_module_close($cipher);
結果は次のとおりです
cfdJ+M6MAzG4WJMb (Base64)
次に、同じテキストを復号化するために ac# バージョンを作成しました
// ASCII encoding and Zero padding
encoding = Encoding.ASCII
padding = IBlockCipherPadding.zeroBytePadding
// Set up the engine and cipher types
baseCipher = new TwofishEngine();
blockSize = baseCipher.GetBlockSize();
modeCipher = new CfbBlockCipher(baseCipher, blockSize);
cipher = padding == null ? new PaddedBufferedBlockCipher(modeCipher) : new PaddedBufferedBlockCipher(modeCipher, padding);
// convert the strings to byte array and create a dummy 000000.. iv
byte[] iv = new byte[blockSize]; // i.e. 16 bytes of zero
keyBytes = _encoding.GetBytes(key); //1234567812345678
inputBytes = Convert.FromBase64String(inp);
// initiate the cipher with iv parameters
cipher.Init(true, new ParametersWithIV(new KeyParameter(keyBytes), iv));
// do the decryption
console.write(_encoding.GetString(cipher.DoFinal(inputBytes)) + "\n";)
しかし、これは私にゴミを出します。HIl1oVW�rEdIp�</p>
閉じます (HloWrd) が、1 文字おきに間違っています!
ECB モードは正常に動作するため、初期化ベクトルと関係があるはずです。
まだ学んでいない PHP と c# の違いはありますか?
その場合、私のC#コードはどこが間違っていますか?