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");
Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
Dheeraj Singh
  • 109
  • 1
  • 16

1 Answers1

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!

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
  • I have not even seen that "mode of operation" in the code, but I have compared the results generated from the code with an online tool, I am getting correct results under "ECB (Electronic CodeBook)", suggests that java code i have working with ECB mode ? – Dheeraj Singh May 21 '20 at 07:58
  • Sure, but ECB mode is not secure for textual strings, it directly leaks information if a text block repeats, even over multiple encrypted text. – Maarten Bodewes May 21 '20 at 08:00
  • Thanks – @Maarten Bodewes with the knowledge base provided was able to do it with CBC mode. – Dheeraj Singh May 21 '20 at 08:50