BouncyCastle BlowFish 加密问题
这是我的其他类中的 BlowFishCrypto 类,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Modes;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Math;
namespace Common.Encryption
{
public class BlowfishCryptographer
{
private bool forEncryption;
private IBufferedCipher cipher;
public BlowfishCryptographer(bool forEncryption)
{
this.forEncryption = forEncryption;
cipher = new BufferedBlockCipher(new CfbBlockCipher(new BlowfishEngine(), 64));
cipher.Init(forEncryption, new ParametersWithIV(new KeyParameter(Encoding.ASCII.GetBytes("DR654dt34trg4UI6")), new byte[8]));
}
public void ReInit(byte[] IV,BigInteger pubkey)
{
cipher.Init(forEncryption, new ParametersWithIV(new KeyParameter(pubkey.ToByteArrayUnsigned()),IV));
}
public byte[] DoFinal()
{
return cipher.DoFinal();
}
public byte[] DoFinal(byte[] buffer)
{
return cipher.DoFinal(buffer);
}
public byte[] DoFinal(byte[] buffer, int startIndex, int len)
{
return cipher.DoFinal(buffer, startIndex, len);
}
public byte[] ProcessBytes(byte[] buffer)
{
return cipher.ProcessBytes(buffer);
}
public byte[] ProcessBytes(byte[] buffer, int startIndex, int len)
{
return cipher.ProcessBytes(buffer, startIndex, len);
}
public void Reset()
{
cipher.Reset();
}
}
}
我尝试测试它,所以我做了这样的输出
BlowfishCryptographer incomingCipher=new BlowfishCryptographer(true);
BlowfishCryptographer outgoingCipher=new BlowfishCryptographer(false);
byte[] buffer = new byte[] { 0x83, 0x00, 0xEE, 0x03, 0x26, 0x6D, 0x14, 0x00, 0xF1, 0x65, 0x27, 0x00, 0x19, 0x02, 0xD8, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xDB, 0xD7, 0x0F, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2B, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x41, 0x00, 0x64, 0x00, 0xE4, 0x00, 0x00, 0x00, 0xDD, 0x0A, 0x18, 0x19, 0x00, 0x00, 0x79, 0x91, 0x87, 0x00, 0x00, 0x01, 0x00, 0xA8, 0x02, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x34, 0x00, 0x6A, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x0A, 0x7E, 0x42, 0x6C, 0x75, 0x65, 0x57, 0x61, 0x76, 0x65, 0x7E, 0x00, 0x09, 0x42, 0x6C, 0x61, 0x63, 0x6B, 0x44, 0x75, 0x73, 0x74 };
Console.WriteLine("\n\nBuffer\n" + outPutHex.ToHex(buffer));
Console.WriteLine("\n\nBuffer enc\n" + outPutHex.ToHex(outgoingCipher.DoFinal(buffer)));
Console.WriteLine("\n\nBuffer dec\n" + outPutHex.ToHex(incomingCipher.DoFinal(buffer)));
十六进制函数将结果输出为十六进制,
public static String ToHex(byte[] buf)
{
var builder = new StringBuilder();
foreach (var b in buf) builder.Append(b.ToString("X2")+ " ");
return builder.ToString();
}
所以结果是:
Buffer
83 00 EE 03 26 6D 14 00 F1 65 27 00 19 02 D8 0F 00 00 00 00 00 00 DB D7 0F 08 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 2B 04 00 00 00 00 00 00 07 00 41 0
0 64 00 E4 00 00 00 DD 0A 18 19 00 00 79 91 87 00 00 01 00 A8 02 00 00 64 00 00
00 34 00 6A 18 00 00 00 00 00 00 C2 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 03 0A 7E 42 6C 75 65 57 61 76 65 7E 00 09 42 6C 61 63 6B 44 75 73 74
Buffer enc
EB 28 65 06 EF B5 B9 3E 01 2F D0 B4 C2 25 4C 9C E2 05 D8 B5 93 AC F9 0F 92 87 8B
5D 1E 45 F6 59 F8 FE 57 A8 0D CF 6C 6B E8 8D F9 88 A6 1D 6D 05 CC B8 6A 9F B0 8
D 13 70 AB F6 3F F8 DD EA ED 16 C3 DB A6 77 B2 46 29 0B DA F4 E2 FF A4 BA 6F C0
06 28 71 57 08 C8 EC 0F 65 54 13 46 C1 23 08 A5 28 C9 9F 9F 1D AD F9 66 09 A7 3B
E3 22 64 A3 A0 8C 90 BC 1A 99 F1 4F F6 73 49 32 10 78 7D CF FF 68 01 75
Buffer dec
EB 28 65 06 EF B5 B9 3E B7 BE A2 2A 3D 92 5F D5 CF E3 D5 09 C0 5B 9D AD 01 D6 E4
6D 73 3A 66 59 A9 83 10 11 80 FE 31 48 68 28 A0 01 C9 D8 AD 3E 38 B7 42 4B E5 E
5 56 44 99 91 E8 72 F0 C9 2B AF 83 8C 35 33 6E 08 CA 1E F0 3F 59 E8 64 8D A6 1C
CE 6E FF DC D6 3A FC D0 80 5B 36 81 06 FA 4E 0F 0B FA 54 CA C0 AD 32 52 68 28 8B
05 CA D2 D3 7C 90 48 93 71 99 CE 28 0B 38 F2 8E 93 74 2F B1 67 9E 68 3F
它不起作用,但为什么呢?
here is my BlowFishCrypto Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Modes;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Math;
namespace Common.Encryption
{
public class BlowfishCryptographer
{
private bool forEncryption;
private IBufferedCipher cipher;
public BlowfishCryptographer(bool forEncryption)
{
this.forEncryption = forEncryption;
cipher = new BufferedBlockCipher(new CfbBlockCipher(new BlowfishEngine(), 64));
cipher.Init(forEncryption, new ParametersWithIV(new KeyParameter(Encoding.ASCII.GetBytes("DR654dt34trg4UI6")), new byte[8]));
}
public void ReInit(byte[] IV,BigInteger pubkey)
{
cipher.Init(forEncryption, new ParametersWithIV(new KeyParameter(pubkey.ToByteArrayUnsigned()),IV));
}
public byte[] DoFinal()
{
return cipher.DoFinal();
}
public byte[] DoFinal(byte[] buffer)
{
return cipher.DoFinal(buffer);
}
public byte[] DoFinal(byte[] buffer, int startIndex, int len)
{
return cipher.DoFinal(buffer, startIndex, len);
}
public byte[] ProcessBytes(byte[] buffer)
{
return cipher.ProcessBytes(buffer);
}
public byte[] ProcessBytes(byte[] buffer, int startIndex, int len)
{
return cipher.ProcessBytes(buffer, startIndex, len);
}
public void Reset()
{
cipher.Reset();
}
}
}
in my other class i tried to test it so i made it like this
BlowfishCryptographer incomingCipher=new BlowfishCryptographer(true);
BlowfishCryptographer outgoingCipher=new BlowfishCryptographer(false);
byte[] buffer = new byte[] { 0x83, 0x00, 0xEE, 0x03, 0x26, 0x6D, 0x14, 0x00, 0xF1, 0x65, 0x27, 0x00, 0x19, 0x02, 0xD8, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xDB, 0xD7, 0x0F, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2B, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x41, 0x00, 0x64, 0x00, 0xE4, 0x00, 0x00, 0x00, 0xDD, 0x0A, 0x18, 0x19, 0x00, 0x00, 0x79, 0x91, 0x87, 0x00, 0x00, 0x01, 0x00, 0xA8, 0x02, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x34, 0x00, 0x6A, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x0A, 0x7E, 0x42, 0x6C, 0x75, 0x65, 0x57, 0x61, 0x76, 0x65, 0x7E, 0x00, 0x09, 0x42, 0x6C, 0x61, 0x63, 0x6B, 0x44, 0x75, 0x73, 0x74 };
Console.WriteLine("\n\nBuffer\n" + outPutHex.ToHex(buffer));
Console.WriteLine("\n\nBuffer enc\n" + outPutHex.ToHex(outgoingCipher.DoFinal(buffer)));
Console.WriteLine("\n\nBuffer dec\n" + outPutHex.ToHex(incomingCipher.DoFinal(buffer)));
outputhex func will out put the result as hex
public static String ToHex(byte[] buf)
{
var builder = new StringBuilder();
foreach (var b in buf) builder.Append(b.ToString("X2")+ " ");
return builder.ToString();
}
so the result is:
Buffer
83 00 EE 03 26 6D 14 00 F1 65 27 00 19 02 D8 0F 00 00 00 00 00 00 DB D7 0F 08 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 2B 04 00 00 00 00 00 00 07 00 41 0
0 64 00 E4 00 00 00 DD 0A 18 19 00 00 79 91 87 00 00 01 00 A8 02 00 00 64 00 00
00 34 00 6A 18 00 00 00 00 00 00 C2 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 03 0A 7E 42 6C 75 65 57 61 76 65 7E 00 09 42 6C 61 63 6B 44 75 73 74
Buffer enc
EB 28 65 06 EF B5 B9 3E 01 2F D0 B4 C2 25 4C 9C E2 05 D8 B5 93 AC F9 0F 92 87 8B
5D 1E 45 F6 59 F8 FE 57 A8 0D CF 6C 6B E8 8D F9 88 A6 1D 6D 05 CC B8 6A 9F B0 8
D 13 70 AB F6 3F F8 DD EA ED 16 C3 DB A6 77 B2 46 29 0B DA F4 E2 FF A4 BA 6F C0
06 28 71 57 08 C8 EC 0F 65 54 13 46 C1 23 08 A5 28 C9 9F 9F 1D AD F9 66 09 A7 3B
E3 22 64 A3 A0 8C 90 BC 1A 99 F1 4F F6 73 49 32 10 78 7D CF FF 68 01 75
Buffer dec
EB 28 65 06 EF B5 B9 3E B7 BE A2 2A 3D 92 5F D5 CF E3 D5 09 C0 5B 9D AD 01 D6 E4
6D 73 3A 66 59 A9 83 10 11 80 FE 31 48 68 28 A0 01 C9 D8 AD 3E 38 B7 42 4B E5 E
5 56 44 99 91 E8 72 F0 C9 2B AF 83 8C 35 33 6E 08 CA 1E F0 3F 59 E8 64 8D A6 1C
CE 6E FF DC D6 3A FC D0 80 5B 36 81 06 FA 4E 0F 0B FA 54 CA C0 AD 32 52 68 28 8B
05 CA D2 D3 7C 90 48 93 71 99 CE 28 0B 38 F2 8E 93 74 2F B1 67 9E 68 3F
it's not working but why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想我发现了问题:
您正在创建两个密码上下文
incomingCipher
和outgoingCipher
。您可以在相同的输入缓冲区
上运行它们。如果您要检查测试向量,这可能很有用,但这两个肯定不是相同的。尝试:
I think I found the problem:
You're creating two cipher contexts
incomingCipher
andoutgoingCipher
. You run them both on the same inputbuffer
. This could be useful if you were checking against test vectors, but the two certainly won't be the same.Try: