Using other modes except ECB and decrypting with DES will throw “invalidkeyexception: missing parameter”

I basically use http://www.avajava.com/tutorials/lessons/how-do-i-encrypt-and-decrypt-files-using-des.html The code encrypts the application, but I want to be able to select the operation mode, so I added this:

private String key;
private static String algorithmMode;

    public DESCrypt(String password,String algorithmMode) {
        this.key = password;
        this.algorithmMode = "DES/" + algorithmMode + "/PKCS5Padding";
    }

The main looks like this:

public static void main(String[] args) {
        try {
            DESCrypt des = new DESCrypt("12345678",algorithmMode);

            // FileInputStream fis1 = new FileInputStream("d:\\_test.txt");
            // FileOutputStream fos1 = new FileOutputStream("d:\\_test.txt.des");
            // des.encrypt(fis1,fos1);

            FileInputStream fis = new FileInputStream("d:\\_test.txt.des");
            FileOutputStream fos = new FileOutputStream("d:\\_test.txt");
            des.decrypt(fis,fos);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

As I said in the title, it can be used with ECB, but in other modes, I can only encrypt

Solution

You are missing the decrypted IV value You need to be in cipher This is included in the init call:

... Cipher.init(Cipher.DECRYPT,someKey,new IvParameterSpec(eightByteValue));

If it is omitted from the encryption code, a random IV value is generated You need to store it (retrieved through cipher. Getiv()) for use in your decryption code

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>