[英]Converting code from C# to Java - Bit manipulation, Int64 to long
[英]Bit manipulation from a ByteArray
我想知道如何操作ByteArray中的位。 我需要的是根據我所擁有的“表格”來移位這些位。
表:
Bit 0 -> Bit 26 Bit 1 -> Bit 31 Bit 2 -> Bit 17 ... Bit 31 -> Bit 5
我使用此方法將ByteArray轉換為BitArray
public static BitArray ByteArraytoBitArray(byte[] bytes)
{
BitArray bits = new BitArray(bytes);
return bits;
}
但我被困在那里,我不知道如何根據表格移動位,然后再回到ByteArray。
編輯:
Code Snipet:
public static byte[] StringToByteArray(String hex)
{
int NumberChars = hex.Length;
byte[] bytes = new byte[NumberChars / 2];
for (int i = 0; i < NumberChars; i += 2)
bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
return bytes;
}
private void button3_Click(object sender, EventArgs e)
{
string featherearring = "00804804A02A1CA20100000000000000D2F8B6FABBB700000000000000000000";
var strarray = StringToByteArray(featherearring);
byte[] strarray_comp = Enc.Encrypt(strarray);
string conv = BitConverter.ToString(strarray_comp);
MessageBox.Show(conv.Replace("-", ""));
}
public static byte[] BitArrayToByteArray(BitArray bits)
{
byte[] bytes = new byte[bits.Length / 8];
bits.CopyTo(bytes, 0);
return bytes;
}
public static byte[] Encrypt(byte[] input)
{
BitArray source = new BitArray(input);
BitArray target = new BitArray(source.Length);
target[26] = source[0];
target[31] = source[1];
target[17] = source[2];
target[10] = source[3];
target[30] = source[4];
target[16] = source[5];
target[24] = source[6];
target[2] = source[7];
target[29] = source[8];
target[8] = source[9];
target[20] = source[10];
target[15] = source[11];
target[28] = source[12];
target[11] = source[13];
target[13] = source[14];
target[4] = source[15];
target[19] = source[16];
target[23] = source[17];
target[0] = source[18];
target[12] = source[19];
target[14] = source[20];
target[27] = source[21];
target[6] = source[22];
target[18] = source[23];
target[21] = source[24];
target[3] = source[25];
target[9] = source[26];
target[7] = source[27];
target[22] = source[28];
target[1] = source[29];
target[25] = source[30];
target[5] = source[31];
return BitArrayToByteArray(target);
}
我的輸入字節數組是“00804804A02A1CA20100000000000000D2F8B6FABBB700000000000000000000”,我的輸出與zimdanen的代碼是“50120000000000000000000000000000000000000000000000000000000000000000”,它應該是“501200002FD901000000000400000000BFE8C4DB140D11F40000000000000000”正如你所看到的,它得到前2個字節,但其余的都是空的。
它需要到位嗎? 您可以創建一個新的BitArray並只復制這些位:
BitArray source = new BitArray(bytes);
// Create target array.
BitArray target = new BitArray(source.Length);
// Map bits.
// ...
target[26] = source[0];
// ...
或者,根據您希望如何維護映射“表”,您可以這樣做:
// Setup mapping - <source, target>.
Dictionary<int, int> mapping = new Dictionary<int, int>();
// ...
mapping.Add(0, 26);
// ...
BitArray source = new BitArray(bytes);
// Create target array.
BitArray target = new BitArray(source.Length);
// Map bits.
foreach (int sourceIndex in mapping.Keys)
{
int targetIndex = mapping[sourceIndex];
target[targetIndex] = source[sourceIndex];
}
您的第一個問題是您有一個32字節的數組,您正在轉換為BitArray
。 你會發現,價值source.Count
屬性為256 target.Count
,而另一方面,僅32所以,你的Encrypt
方法只是改變了位排列,這相當於四個字節的前32位-八個十六進制字符。 數組的其余部分將為空。
您可以通過在執行復制之前更改BitArrayToByteArray
方法以使用0xFF填充目標來驗證這一點:
public static byte[] BitArrayToByteArray(BitArray bits)
{
byte[] bytes = new byte[bits.Length / 8];
// Fill the array with 0xFF to illustrate.
for (int i = 0; i < bytes.Length; ++i)
{
bytes[i] = 0xFF;
}
bits.CopyTo(bytes, 0);
return bytes;
}
我想你會發現結果是“50120000FFFFFF ......”
很難准確地說出你想要做什么。 如果要對字符串中的字節進行加擾,則無需使用BitArray
。 如果你真的想要擾亂比特,那么你需要加擾所有比特。 如果沒有關於你要做什么的更多信息,我沒有任何建議。 除此之外,您可能應該使用眾多現有加密算法中的一種,而不是嘗試使用自己的加密算法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.