3DES Decryption Error Invalid Key Length

前端 未结 2 1992
無奈伤痛
無奈伤痛 2020-12-18 17:20

I am using 3DESC to decrypt data but i am getting following exception

java.security.InvalidKeyException: Invalid key length: 16 bytes

My Co

2条回答
  •  生来不讨喜
    2020-12-18 17:39

    DES-EDE cipher can be used with 3 different subkeys therefore the key size should be 24 bytes (3 times 8 bytes). If you want to use only 2 keys (i.e. in this mode first key == last key) then you just have to duplicate the first 8 bytes of the key array.

    byte[] key;
    if (keyBytes.length == 16) {
        key = new byte[24];
        System.arraycopy(keyBytes, 0, key, 0, 16);
        System.arraycopy(keyBytes, 0, key, 16, 8);
    } else {
        key = keyBytes;
    }
    

提交回复
热议问题