0

I am using a sample code from a git repository to understand twofish algorithm, The code below works very fine the results are also correct checked from an online tool ref http://twofish.online-domain-tools.com/

the problem is as below :-

int[] plainText = new int[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C,
            0x0D, 0x0E, 0x0F };

    int[] p = new int[4];
    for (int i = 0; i < 4; i++) {
        p[i] = plainText[4 * i] + 256 * plainText[4 * i + 1] + (plainText[4 * i + 2] << 16)
                + (plainText[4 * i + 3] << 24);
    }
    System.out.println("Input:");
    Utils.printInput(p);
    int[] key = p; //
    System.out.println("Key:");
    Utils.printInput(key);
    //
    int[] encrypted = TwoFish.encrypt(p, key);
    //
    System.out.println("Encrypted:");
    Utils.printInput(encrypted);
    System.out.println();

    int[] decrypted = TwoFish.decrypt(encrypted, key);
    System.out.println("Decrypted:");
    Utils.printInput(decrypted);

In the code above same key is used as plainText and Key, While there is a requirement of passing plain text and key

int[] encrypted = TwoFish.encrypt(p, key);

above code needs to take input from

int[] encrypted = TwoFish.encrypt("The plain text information", "000102030405060708090A0B0C0D0E0F");
4

1 回答 1

1

OK, cryptography primer:

  • You need a mode of operation for the Twofish block cipher. I have trouble to recognize one you have in the code though, and that's not a good sign.
  • The mode of operation needs an IV, and a random - or at least a fully unpredictable IV - for CBC mode.
  • Your plaintext you need to encode. Using UTF-8 is recommended nowadays (it's compatible with ASCII, so for your string you really cannot go wrong).
  • You need a hexadecimal decoder to decode the key to a byte array.

By the way, generally we implement cryptographic block ciphers and other primitives to operate on bits - or more specifically bytes. The cipher or at least the mode of operation should accept bytes, not integers.

Good luck!

于 2020-05-20T21:38:22.300 回答