2012-05-03 2 views
0

ByteArray에서 비트를 조작하는 방법을 알고 싶습니다. 내가 필요한 것은 '테이블'에 따라 비트를 이동하는 것입니다.ByteArray에서 비트 조작

표 :

Bit 0 -> Bit 26 
Bit 1 -> Bit 31 
Bit 2 -> Bit 17 
... 
Bit 31 -> Bit 5 

나는이 방법

public static BitArray ByteArraytoBitArray(byte[] bytes) 
{ 
    BitArray bits = new BitArray(bytes); 
    return bits; 
} 

를 사용 BitArray하기 위해이 ByteArray로 변환하지만 붙어 있 느니라, 내가 따라 비트를 이동하는 방법을 모른다 테이블로 이동 한 다음 ByteArray로 돌아갑니다.

편집 :

코드 Snipet : 내 입력 바이트 배열은 "00804804A02A1CA20100000000000000D2F8B6FABBB700000000000000000000가"와 zimdanen의 코드 내 출력은 "5012000000000000000000000000000000000000000000000000000000000000"입니다

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); 
} 

입니다

그리고 당신으로 "501200002FD901000000000400000000BFE8C4DB140D11F40000000000000000" 해야한다 알 수 있듯이, 첫 번째 2 바이트를 가져 오지만 나머지는 모두 null입니다.

+0

빠르거나 정확해야합니까? – harold

+0

[C# - 전체 바이트 배열을 왼쪽으로 이동] [1]에 대한 내 대답을보십시오. [1] :이 방법은 '년후 경우 http://stackoverflow.com/questions/8440938/c-sharp-left-shift-an-entire-byte-array/8450271#8450271 – JamieSee

+0

I는 wouldnt 마음 가장 빠르며, 조작법을 올바르게 처리하는 데 정말로 신경을 쓴다. –

답변

1

장소에 있어야합니까? 당신이 이런 식으로 작업을 수행 할 수 있습니다, 당신은 당신의 매핑 "테이블"을 유지하는 방법에 따라,

BitArray source = new BitArray(bytes); 

// Create target array. 
BitArray target = new BitArray(source.Length); 

// Map bits. 
// ... 
target[26] = source[0]; 
// ... 

또는 : 새 BitArray를 작성하고 그냥 비트를 복사 할 수 있습니다

// 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]; 
} 
+0

그 자리에 있어야 할 필요가 없습니다 비트 따라 적절하게 이동해야합니다 및 ByteArray 다시 편집 : 코드를보고 내가 제대로 작동하는지 확인하십시오 :) –

+0

내 입력 바이트 배열 "00804804A02A1CA20100000000000000D2F8B6FABBB700000000000000000000"및 귀하의 코드로 내 결과는 "5012000000000000000000000000000000000000000000000000000000000000"이고 "501200002FD901000000000400000000BFE8C4DB140D11F40000000000000000"이어야합니다. 보시다시피, 처음 2 바이트는 올바르게 표시되지만 모두 0이됩니다. –

+0

전체 코드 스 니펫을 게시 할 수 있습니까? – zimdanen

1

귀하 첫 번째 문제는 BitArray으로 변환하는 32 바이트의 배열이 있다는 것입니다. source.Count 속성의 값은 256입니다. 반면에 메서드는 비트 배열의 처음 32 비트 만 변경합니다. 즉, 4 바이트에 해당합니다 (16 진수 중 8 개에 해당함). 문자. 나머지 배열은 null입니다.

"... 50120000FFFFFF"나는 당신이 결과가 찾을 수있을 거라 생각

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; 
} 

:

당신은 사본을하기 전에를 0xFF로 대상을 채우기 위해 당신의 BitArrayToByteArray 방법을 변경하여이를 확인할 수 있습니다

정확히 무엇을 하려는지 알기가 어렵습니다. 문자열의 바이트를 스크램블하려는 경우 BitArray을 사용할 필요가 없습니다. 정말로 비트를 스크램블하려는 경우 모든 비트를 스크램블해야합니다. 당신이하려는 일에 대해 더 이상의 정보가 없다면, 나는 제안이 없습니다. 예외를 제외하고는 기존의 많은 암호화 알고리즘 중 하나를 사용해야합니다.

+0

오 대단해! 고맙습니다! Im 실제로 여기에 내 자신의 알고리즘을 쓰려고하지 않고 게임에서 파일을 해독하고 암호화합니다. :) –

관련 문제