本文整理汇总了C#中System.Collections.BitArray.PrintInBinary方法的典型用法代码示例。如果您正苦于以下问题:C# BitArray.PrintInBinary方法的具体用法?C# BitArray.PrintInBinary怎么用?C# BitArray.PrintInBinary使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Collections.BitArray
的用法示例。
在下文中一共展示了BitArray.PrintInBinary方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Encrypt
/// <summary>
/// DES加密
/// </summary>
/// <param name="plain">8Byte字节数组明文</param>
/// <param name="key">8Byte字节数组密文</param>
/// <returns></returns>
public static byte[] Encrypt(Byte[] plain, Byte[] key)
{
if (plain.Length > 8 || key.Length > 8)
{
throw new ArgumentException("Plain text and key should be 8 bytes.");
}
//不足8字节,补0
if (plain.Length < 8)
{
plain = plain.Concat(new Byte[8 - plain.Length]).ToArray();
}
if (key.Length < 8)
{
key = key.Concat(new Byte[8 - key.Length]).ToArray();
}
//转为位数组 处理小端->大端
BitArray input = new BitArray(plain.Reverse().ToArray()).Reverse();
BitArray inputKey = new BitArray(key.Reverse().ToArray()).Reverse();
Debug.WriteLine("[PLAIN]" + input.PrintInBinary());
Debug.WriteLine("[KEY]" + inputKey.PrintInBinary());
//初始置换
input = FirstSwap(input);
//Debug.WriteLine(input.PrintInHex());
BitArray[] keys = new BitArray[16];
//设置L R
RoundPackage rounds = new RoundPackage();
rounds.L.SetRange(0, 32, input);
rounds.R.SetRange(0, 32, input, 32);
//生成16轮用子密钥
keys = GenerateKeys(inputKey);
//16轮加密
for (int i = 0; i < 16; i++)
{
rounds = Round(rounds, keys[i]);
//Debug.WriteLine("i:{3}, L:{0},R:{1},Ki:{2}", rounds.L.PrintInBinary(), rounds.R.PrintInBinary(), keys[i].PrintInBinary(),i+1);
}
//Debug.WriteLine("L:{0},R:{1}", rounds.L.PrintInBinary(), rounds.R.PrintInBinary());
BitArray output = new BitArray(64);
//拼接:R+L
output = rounds.R.Append(rounds.L);
//Debug.WriteLine(output.PrintInBinary());
//逆初始置换
output = ReverseFirstSwap(output);
Debug.WriteLine("[ENCRYPT]" + output.PrintInBinary());
return output.ToByteArray();
}
示例2: Decrypt
public static byte[] Decrypt(Byte[] inputBytes, Byte[] key)
{
if (inputBytes.Length > 8 || key.Length > 8)
{
throw new ArgumentException("Encrypted text and key should be 8 bytes.");
}
if (inputBytes.Length < 8)
{
inputBytes = inputBytes.Concat(new Byte[8 - inputBytes.Length]).ToArray();
}
if (key.Length < 8)
{
key = key.Concat(new Byte[8 - key.Length]).ToArray();
}
//BitArray input = new BitArray(inputBytes);
//BitArray inputKey = new BitArray(key);
//处理小端->大端
BitArray input = new BitArray(inputBytes.Reverse().ToArray()).Reverse();
BitArray inputKey = new BitArray(key.Reverse().ToArray()).Reverse();
Debug.WriteLine("[ENCRYPTED]" + input.PrintInBinary());
Debug.WriteLine("[KEY]" + inputKey.PrintInBinary());
input = FirstSwap(input);
BitArray[] keys = new BitArray[16];
RoundPackage rounds = new RoundPackage();
rounds.L.SetRange(0, 32, input);
rounds.R.SetRange(0, 32, input, 32);
keys = GenerateKeys(inputKey);
//Debug.WriteLine("L:{0},R:{1},Ki:{2}", rounds.L.PrintInHex(), rounds.R.PrintInHex(), keys[15].PrintInHex());
for (int i = 15; i >= 0; i--)
{
rounds = Round(rounds, keys[i]);
//Debug.WriteLine("L:{0},R:{1},Ki:{2}", rounds.L.PrintInHex(), rounds.R.PrintInHex(), keys[i].PrintInHex());
}
BitArray output = new BitArray(64);
output = rounds.R.Append(rounds.L);
output = ReverseFirstSwap(output);
Debug.WriteLine("[DECRYPT]" + output.PrintInBinary());
return output.ToByteArray();
}