循环遍历字节数组中的十六进制数字

时间:2013-02-10 05:45:29

标签: java hex bytearray brute-force

嘿伙计们,我想知道我是否可以得到一点帮助:我试图在字节数组中以十六进制计算。我正在做的是我有8个十六进制数字形式的纯文本和相同形式的密文以及密钥的前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(但其他那些与这个特定部分没有任何关系)

2 个答案:

答案 0 :(得分: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();
        }
    }
}

答案 1 :(得分: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);
}