2013-02-10 33 views
0

嘿家伙我想知道我是否可以得到一些帮助:我试图在一个字节数组内计数十六进制数。我在做什么是我有八个十六进制数字形式的纯文本和相同形式的密文以及密钥的前4个数字。即时尝试使用DES通过强力破解密钥。在字节数组中循环十六进制数字

我的关键是这样的:

[A3 BB 12 44 __ __ __ __] 

,我希望它这样开始我想:

[A3 BB 12 44 00 00 00 00] 

然后

[A3 BB 12 44 00 00 00 01] 

等。我真的不知道如何在十六进制数。在一个字节数组里面!

任何帮助非常感谢!

编辑完成后太大的帮助

这里找到键(我改变了一些东西的名字周围以更好地满足我的程序)

public static void findKey(){ 

    byte [] keyBytes = null; 
    byte [] pt; 
    byte [] ct; 

    codeBreaker KEY = new codeBreaker(new byte[]{(byte)0x01, (byte)0x23, (byte)0x45, (byte)0x67, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00}, 2); 

    String plaintext = "Plaintxt"; 
    ct = new byte [] {(byte)0x4A, (byte)0xC4, (byte)0x55, (byte)0x3D, (byte)0xB3, (byte)0x37, (byte)0xCA, (byte)0xB3}; 

    //convert the plain text "Plaintxt" into a hex byte array 
    String ptHex = asciiToHex(plaintext); 
    pt = getBytes(ptHex); 

    //keyBytes = KEY.inc() 

    //my attempt 
    /*while(!isKey(pt,keyBytes,ct)){ 
     KEY.inc(); // something like increase the key by 1 and send t back in. 
    } 
    */ 


    //this is your original while loop 
    /*while (KEY.inc()) { 
     byte[] bytes = KEY.getBytes(); 
     for (byte b: bytes) { 
      System.out.printf("%02X ", b); 
     } 
     System.out.println(); 
    } 
    */ 


    //Final outputs for the findKey method 
    System.out.println("  Plain Text In Hex Is:");   
    printByteArray(pt); 
    System.out.println(); 
    System.out.println("   The Cipher Text Is:"); 
    printByteArray(ct); 
    System.out.println(); 

} 

,这里是你想出的东西

public codeBreaker(byte[] keyAr, int startIndex) { 
    this.key = keyAr; 
    this.startIndex = startIndex; 
} 

    public boolean inc() { 
    int i; 
    for (i = key.length-1; i >= startIndex; i--) { 
     key[i]++; 
     if (key[i] != 0) 
      break; 
    } 
     // we return false when all bytes are 0 again 
    return (i >= startIndex || key[startIndex] != 0); 
} 

public byte[] getBytes() { 
    return key; 
} 

我把它们放到一个类中,并将其称为codeBreaker与其他方法(但其他方法不与这个特定部分有什么关系)

+0

*“嘿,想知道如果我能得到一点点智慧帮助” * - 不圆这里的队友。我们都只是“愚蠢的程序员”......就像你:-) :-) – 2013-02-10 05:48:03

+0

@StephenC:为自己说话。我认为自己是一个智力沉重的人,与拉什林博和唐纳德特朗普一起。 – 2013-02-10 05:51:36

+0

尽管如此,一个字节不知道或在意它是否为十六进制,因为这只是字节的字符串表示形式。你能给你更多背景吗?字节数组来自哪里,为什么你不能简单地用for循环遍历它,等等...... – 2013-02-10 05:53:15

回答

3

这个怎么样?

public class ByteIncrement 
{ 
    private final byte[] bytes; 
    private final int startIndex; 
    public ByteIncrement(byte[] bytes, int startIndex) { 
     this.bytes = bytes; 
     this.startIndex = startIndex; 
    } 
    public boolean inc() { 
     int i; 
     for (i = bytes.length-1; i >= startIndex; i--) { 
      bytes[i]++; 
      if (bytes[i] != 0) 
       break; 
     } 
     // we return false when all bytes are 0 again 
     return (i >= startIndex || bytes[startIndex] != 0); 
    } 
    public byte[] getBytes() { 
     return bytes; 
    } 

    public static void main(String[] args) { 
     ByteIncrement bi = new ByteIncrement(new byte[]{(byte)0xa4, 0x56, 0x17, (byte)0x9f, 0x00, 0x00, 0x00, 0x00}, 2); // first two bytes are constant -> 2 
     while (bi.inc()) { 
      byte[] bytes = bi.getBytes(); 
      for (byte b: bytes) { 
       System.out.printf("%02X ", b); 
      } 
      System.out.println(); 
     } 
    } 
} 
+0

所以这个后面的一个调用会返回.... 00 00 00 01?就像我需要能够通过加密des发送新的增量字节数组来增加它,并检查密文是否与我给定的密文相同,如果不是,我需要再次增加。或者,因为我会返回它,现在它会是00 00 00 01我wouls只是发回,并且下一个将是00 00 00 02?当我进入a-f时会发生什么? – erp 2013-02-10 06:15:05

+0

第一次inc()后getBytes()的结果实际上是...... 01 00 00 00.我认为这是蛮力,顺序无关紧要吗?如果你想要的确如此...... 00 00 00 01则for循环将不得不向后迭代'i = bytes.length-1; i> = startIndex;我 - '和返回值是'i> = startIndex ||字节[startIndex]!= 0'。当所有数值都翻转到...... 00 00 00 00时,公司返回false。 – 2013-02-10 06:25:03

+0

好的 - 我从您编辑的定义中看到,反向循环就是您实际需要的。我会改变代码 – 2013-02-10 06:27:53

0
public static void tryCipher (
    byte b1, byte b2, byte b3, byte b4, 
    byte b5, byte b6, byte b7, byte b8) 
{ 
    // Try the variant, convert to HEX if necessary 
} 

public static void bruteForce (byte b1, byte b2, byte b3, byte b4) 
{ 
    for (int b5 = Byte.MIN_VALUE; b5 <= Byte.MAX_VALUE, b5++) 
     for (int b6 = Byte.MIN_VALUE; b6 <= Byte.MAX_VALUE, b6++) 
      for (int b7 = Byte.MIN_VALUE; b7 <= Byte.MAX_VALUE, b7++) 
       for (int b8 = Byte.MIN_VALUE; b8 <= Byte.MAX_VALUE, b8++) 
        tryCipher (b1, b2, b3, b4, (byte)b5, (byte)b6, (byte)b7, (byte)b8); 
} 
相关问题