为C#bouncycastle编写PHP MCrypt twofish [英] writing php MCrypt twofish for c# bouncycastle
问题描述
我正在学习如何使用BouncyCastle c#密码库进行加密.我不想发送邮件,所以我没有考虑安全性等. 我已经在Visual Studio中编写了我的C#代码.
I'm learning how to use the BouncyCastle c# cipher library for doing encryption. I'm not looking to send messages so I'm not thinking about security etc. I have written my c# code in Visual Studio.
这是问题所在.我已经在CFB模式下使用Twofish对文本" Hello World!"进行了加密.密钥是1234567812345678.我使用了phpfiddle http://phpfiddle.org/在线工具.
Here is the problem. I have encrypted the text "Hello World!", using Twofish in CFB mode. The key is 1234567812345678. I've used phpfiddle http://phpfiddle.org/ online tool.
$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)
cfdJ+M6MAzG4WJMb (Base64)
然后我创建了一个c#版本来解密相同的文本
I have then created a c# version to decrypt the same text
// 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
But this gives me garbage out. I get HIl1oVW�rEdIp�
关闭(H.l.o.W.r.d.),但每隔一个字母都是错误的!
Close (H.l.o.W.r.d.) but every other letter is wrong!
ECB模式可以正常工作,因此必须与初始化向量有关.
ECB mode works fine so it must be something to do with the initialization vector.
我还没有学过PHP和c#之间的一些区别吗?
Are there some differences between PHP and c# that I havn't learnt yet?
在这种情况下,我的C#代码在哪里不正确?
Where is my c# code incorrect in that case?
推荐答案
确定.所以我终于解决了.我对密码块模式的理解是一个基本错误. CFB和OFB是流密码,不是填充缓冲块密码.
OK. So I solved it finally. A basic error in my understanding of cipher block modes. CFB and OFB are Stream Ciphers not Padded Buffered block ciphers.
因此,密码设置应该已经
Therefore the cipher setup should have been
modeCipher = new CfbBlockCipher(baseCipher,8); // Allways 8 bits for a stream cipher !!
cipher = new StreamBlockCipher(modeCipher);
(cipher.doFinal也已更改,但此处未显示)
(The cipher.doFinal was changed too but not shown here)
现在可以正常工作了.
这篇关于为C#bouncycastle编写PHP MCrypt twofish的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!