嗨,我想知道我是否可以得到一点帮助:我试图在一个字节数组内的十六进制数中加计数。我在做什么是我有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(但其他方法与该特定部分没有任何关系)

最佳答案

这个如何?

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

10-06 04:58