📄 blowfishdemo.cs
字号:
{
Console.WriteLine(Resources.BFDEMO_SIMPLE_UNEXPECTED_MISMATCH);
}
}
#endregion
#region Show Java Interoperability
static readonly string BFEASY_REF_PASSW = "secret";
static readonly string BFEASY_REF_TEXT = "Protect me.";
static readonly string BFEASY_REF_ENC = "e1c799a96e2b1f63f34927d5b7358d9c6fe4cc47ec31b79000642f5cd286007b";
static readonly byte[] BFS_REF_KEY = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
static void ShowJavaInterop()
{
int i, c = 117;
byte[] encBytes;
String enc, dec;
MemoryStream ms;
BlowfishEasy bfes;
BlowfishStream bfs;
// demonstrate BlowfishEasy
bfes = new BlowfishEasy(BFEASY_REF_PASSW);
enc = bfes.EncryptString(BFEASY_REF_TEXT);
Console.WriteLine(enc);
dec = bfes.DecryptString(enc);
Console.WriteLine(dec);
// Here we try to decrypt material encrypted with BlowfishJ.
dec = bfes.DecryptString(BFEASY_REF_ENC);
Console.WriteLine(dec);
// demonstrate BlowfishStream
ms = new MemoryStream();
bfs = BlowfishStream.Create(
ms,
BlowfishStreamMode.Write,
BFS_REF_KEY,
0,
BFS_REF_KEY.Length);
for (i = 0; i < c; i++)
{
bfs.WriteByte((byte)i);
}
bfs.Close();
encBytes = ms.ToArray();
Console.WriteLine(Resources.BFDEMO_WRITTEN_TO_STREAM_2, c, encBytes.Length);
#if DUMP_REF_STREAM
// This data dump is used to have reference data for compatibility testing on
// the Java side. It should actually not be necessary to recreate this data
// (otherwise version-dependend compatibility might not be guaranteed anymore).
for (i = 0; i < encBytes.Length;)
{
Console.Write("(byte)0x{0}", (encBytes[i++] & 0x0ff).ToString("x2"));
Console.Write((0 == i % 6) ? ",\n" : ", ");
}
Console.WriteLine();
#endif
ms = new MemoryStream(encBytes);
bfs = BlowfishStream.Create(
ms,
BlowfishStreamMode.Read,
BFS_REF_KEY,
0,
BFS_REF_KEY.Length);
for (i = 0; i < c; i++)
{
if ((i & 0x0ff) != bfs.ReadByte())
{
Console.WriteLine(Resources.BFDEMO_DECRYPT_ERROR_1, i);
return;
}
}
if (-1 != bfs.ReadByte())
{
Console.WriteLine(Resources.BFDEMO_DECRYPT_OVERSIZED);
}
bfs.Close();
Console.WriteLine(Resources.BFDEMO_STREAM_DECRYPT_OK);
}
#endregion
#region Show Performance Test
static void ShowPerformance()
{
int loop, maxloops, blockc;
long total, tm, rate;
byte[] buf;
BlowfishECB bfe;
maxloops = 30000;
blockc = 1000;
bfe = new BlowfishECB(new byte[0], 0, 0);
buf = new byte[BlowfishECB.BLOCK_SIZE * blockc];
Console.WriteLine(Resources.BFDEMO_RUNNING_PERFORMANCE);
tm = DateTime.Now.Ticks;
for (loop = 0; loop < maxloops; loop++)
{
bfe.Encrypt(buf, 0, buf, 0, buf.Length);
}
total = maxloops * buf.Length;
tm = DateTime.Now.Ticks - tm;
tm /= 10 * 1000;
if (0 == tm) tm = 1L;
rate = (total * 1000) / tm;
Console.WriteLine(Resources.BFDEMO_SPEED_RESULT_1, rate);
}
#endregion
#region Show Blowfish Algorithm Implementation
public static string HexPrint(byte[] buf)
{
StringBuilder result = new StringBuilder(buf.Length * 3);
for (int i = 0, c = buf.Length; i < c; i++)
{
if (0 < i) result.Append(' ');
result.Append(buf[i].ToString("x2"));
}
return result.ToString();
}
static SymmetricAlgorithm MakeAlgo(bool useBlowfish)
{
SymmetricAlgorithm result = (useBlowfish) ? new BlowfishAlgorithm() : SymmetricAlgorithm.Create();
result.Mode = CipherMode.CBC;
if (useBlowfish) result.KeySize = 40;
result.GenerateKey();
result.GenerateIV();
result.Padding = PaddingMode.PKCS7;
return result;
}
static void ShowBlowfishAlgorithm()
{
int i;
byte[] buf = new byte[3];
byte[] encData, decData;
// set up the algorithm (set to false to go with AES for comparison purposes)
SymmetricAlgorithm alg = MakeAlgo(true);
// we encrypt and decrypt from and to a memory stream, so first we have to set up a
// source (by writing some bytes to it) and a target stream
MemoryStream inStream = new MemoryStream();
for (i = 0; i < 11; i++)
{
inStream.WriteByte((byte)i);
}
inStream.Position = 0; // need to reset it for the following reading
MemoryStream outStream = new MemoryStream();
// now we create a crypto stream, to show that our BlowfishAlgorithm plays together
// with a standard .NET framework security component
CryptoStream encStream = new CryptoStream(
outStream,
alg.CreateEncryptor(),
CryptoStreamMode.Write);
// write data from our input stream to the encrypted stream (which then will finally
// put it into our output stream) by using a small buffer (as it it is done usually)
while (inStream.Position < inStream.Length)
{
int read = inStream.Read(buf, 0, buf.Length);
encStream.Write(buf, 0, read);
}
encStream.Close();
// show what we got for the encrypted data
encData = outStream.ToArray();
Console.WriteLine("plain : " + HexPrint(inStream.ToArray()));
Console.WriteLine("encrypted: " + HexPrint(encData));
// decrypt the encrypted data, with the an input stream now set up with the
// encrypted data and being passed to the decryption stream
outStream = new MemoryStream();
CryptoStream decStream = new CryptoStream(
new MemoryStream(encData),
alg.CreateDecryptor(),
CryptoStreamMode.Read);
while (outStream.Position < encData.Length)
{
int read = decStream.Read(buf, 0, buf.Length);
if (0 == read) break;
outStream.Write(buf, 0, read);
}
decStream.Close();
decData = outStream.ToArray();
Console.WriteLine("decrypted: " + HexPrint(decData));
// verify that we got the right data back by simulating and comparing the
// original input data
for (i = 0; i < 11; i++)
{
if (decData[i] != i)
{
Console.WriteLine("decryption error!");
break;
}
}
}
#endregion
/// <summary>The application entry point.</summary>
public static void Main()
{
ShowBlowfishECB();
ShowBlowfishCBC();
ShowBlowfishCFB();
ShowBlowfishSimple();
ShowJavaInterop();
ShowBlowfishAlgorithm();
//ShowPerformance();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -