本文整理汇总了C#中System.Collections.BitArray.ToByteArray方法的典型用法代码示例。如果您正苦于以下问题:C# BitArray.ToByteArray方法的具体用法?C# BitArray.ToByteArray怎么用?C# BitArray.ToByteArray使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Collections.BitArray
的用法示例。
在下文中一共展示了BitArray.ToByteArray方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Decrypt
/// <summary>
/// Decrypts a set of Taylor Swift lyrics into the original byte data.
/// </summary>
/// <param name="data">The string representation of the Taylor Swift lyrics to decrypt.</param>
/// <param name="key">The 256 bit Key used to encrypt the data.</param>
/// <param name="iv">The 128 bit IV used to encrypt the data.</param>
public byte[] Decrypt(string data, byte[] key, byte[] iv)
{
// Validate key parameter
if (key == null)
throw new ArgumentNullException("key");
if (key.Length == 0)
throw new ArgumentException("The key parameter can not be empty.", "key");
if (key.Length * 8 != KeyBitSize)
throw new ArgumentException($"The key parameter must be {KeyBitSize} bits long.", "key");
// Validate iv parameter
if (iv == null)
throw new ArgumentNullException("iv");
if (iv.Length == 0)
throw new ArgumentException("The iv parameter can not be empty.", "iv");
if (iv.Length * 8 != BlockBitSize)
throw new ArgumentException($"The iv parameter must be {BlockBitSize} bits long.", "iv");
// Get fingerprint id's from the fingerprint strings
var fingerprintIds = new List<int>();
foreach (var lyric in data.Split('\n'))
{
if (lyric.Trim() == string.Empty)
continue;
fingerprintIds.Add(
_fingerprintManager.GetIndexOfFingerprint(
_fingerprintManager.GetFromLyric(lyric)));
}
var bitArray = new BitArray(fingerprintIds.Count * 10);
var index = 0;
foreach (var x in fingerprintIds)
{
var arr = new BitArray(new int[] { x });
bitArray.Set(index++, arr.Get(0));
bitArray.Set(index++, arr.Get(1));
bitArray.Set(index++, arr.Get(2));
bitArray.Set(index++, arr.Get(3));
bitArray.Set(index++, arr.Get(4));
bitArray.Set(index++, arr.Get(5));
bitArray.Set(index++, arr.Get(6));
bitArray.Set(index++, arr.Get(7));
bitArray.Set(index++, arr.Get(8));
bitArray.Set(index++, arr.Get(9));
}
var encryptedData = bitArray.ToByteArray();
// Remove Padding from encrypted data
var paddingLength = encryptedData[encryptedData.Length - 1];
Array.Resize(ref encryptedData, encryptedData.Length - paddingLength);
// Decrypt Data
var decryptedData = DecryptPayload(encryptedData, key, iv);
return decryptedData;
}
示例2: DecodeHiddenMessage
public byte[] DecodeHiddenMessage(byte[] container)
{
var openedFile = TextUtils.GetUTF8CharArrayFromByteStream(container);
var messageBits = new BitArray(openedFile.SpaceCount());
var shouldReadOneBit = false;
var numberOfBitToPut = 0;
for (var i = 0; i < openedFile.Length; i++)
{
if (shouldReadOneBit)
{
messageBits.Set(numberOfBitToPut, openedFile[i] == SPACE);
numberOfBitToPut++;
shouldReadOneBit = false;
continue;
}
shouldReadOneBit = openedFile[i] == SPACE;
}
return messageBits.ToByteArray();
}
示例3: 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();
}
示例4: 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();
}
示例5: EncryptWithAddZero
public static void EncryptWithAddZero(string file_input, string file_output, string key1)
{
FileStream input;
BinaryReader br;
FileStream output;
BinaryWriter bw;
byte[] block;
key = new Key(key1);
Utils.key.makeKey();
try
{
input = new FileStream(file_input, FileMode.Open, FileAccess.Read);
br = new BinaryReader(input);
output = new FileStream(file_output, FileMode.Create, FileAccess.Write);
bw = new BinaryWriter(output);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
try
{
while ((block = br.ReadBytes(8)).Length > 0)
{
if (input.Position == input.Length)
{
byte[] lol = new byte[8];
for (int i = 0; i < 8; i++)
{
if (i < block.Length)
{
lol[i] = block[i];
}
else
{
lol[i] += (byte)0;
}
}
int block_do_zapisu = 8 - block.Length;
BitArray encrypted_message = Utils.makeMessage(napraw(lol));
bw.Write((encrypted_message.ToByteArray()));
BitArray b = new BitArray(new int[] { block_do_zapisu, 0 });
b = Utils.makeMessage(napraw(b.ToByteArray()));
bw.Write((b.ToByteArray()));
}
else
{
BitArray encrypted_message = Utils.makeMessage(napraw(block));
bw.Write((encrypted_message.ToByteArray()));
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
input.Close();
output.Close();
br.Close();
bw.Close();
}
finally
{
input.Close();
output.Close();
br.Close();
bw.Close();
}
}
示例6: DecodeHiddenMessage
public byte[] DecodeHiddenMessage(byte[] container)
{
_customCodingValidator.CheckIfCanDecodeMessageOrThrow(container);
var settings = new SettingsFrameFromContent(container);
var headerPositiveBits = settings.ToBitArray().GetPositiveBitsCount();
var headerSize = headerPositiveBits + HEADER_FRAME_MIN_SIZE;
var hiddenMessageBits = new BitArray(settings.MessageLength);
var decodedHiddenBits = 0;
var hiddenBitCounter = 0;
var containerChars = TextUtils.GetUTF8CharArrayFromByteStream(container);
for (int i = headerSize + settings.Shift; i < headerSize + settings.MessageLength; i+= settings.JumpValue + 1)
{
if (hiddenBitCounter < hiddenMessageBits.Length)
{
var let = containerChars[i + decodedHiddenBits];
if (containerChars[i + decodedHiddenBits] == settings.Coding)
{
hiddenMessageBits.Set(hiddenBitCounter, true);
decodedHiddenBits++;
}
else
{
hiddenMessageBits.Set(hiddenBitCounter, false);
}
hiddenBitCounter++;
}
else
{
break;
}
}
var arej = hiddenMessageBits.ToByteArray();
return arej;
}
示例7: UpdatePermissions
public void UpdatePermissions(IList<Permission> list)
{
var bits = new BitArray(new byte[Enum.GetValues(typeof(Permission)).Length]);
foreach (var item in list)
{
bits[(int)item] = true;
}
Permissions = bits.ToByteArray();
}