0

嘿伙计们,我想知道我是否能得到一点帮助:我试图在字节数组中以十六进制计数。我正在做的是我有 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(但其他那些与这个特定部分没有任何关系)

4

2 回答 2

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();
        }
    }
}
于 2013-02-10T06:01:25.850 回答
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);
}
于 2013-02-10T06:46:09.963 回答