我的代码有问题。说明如下: 该项目具有以下要求:
我在第2步和第4步中遇到问题。请您告诉我该怎么做。我不断收到错误消息:线程“ main” j
中的异常avax.crypto.BadPaddingException: Given final block not properly padded. Such issues can arise if a bad key is used during decryption.
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:991)
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:847)
at com.sun.crypto.provider.DESCipher.engineDoFinal(DESCipher.java:314)
at javax.crypto.Cipher.doFinal(Cipher.java:2164)
at Main.main(Main.java:50)
请告诉我该怎么做。
import javax.crypto.*;
import javax.crypto.spec.DESKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException, InvalidKeySpecException, IllegalBlockSizeException, BadPaddingException, NoSuchPaddingException {
Scanner scan = new Scanner(System.in);
System.out.println("Please Input A String Of Characters:");
String myString = scan.nextLine();
System.out.println("Please Input a Password:");
String passw = scan.nextLine();
SecretKeyFactory MyKeyFactory = SecretKeyFactory.getInstance("DES");
byte[] mybytes = myString.getBytes("UTF8");
DESKeySpec myMaterial = new DESKeySpec(mybytes);
SecretKey myDESkey = MyKeyFactory.generateSecret(myMaterial);
Cipher desCipher = Cipher.getInstance("DES");
desCipher.init(Cipher.ENCRYPT_MODE, myDESkey);
byte[] myEncryptedBytes = desCipher.doFinal(mybytes);
System.out.println(Arrays.toString(myEncryptedBytes));
desCipher.init(Cipher.DECRYPT_MODE, myDESkey);
byte[] myDecryptedBytes = desCipher.doFinal(mybytes);
System.out.println(Arrays.toString(myDecryptedBytes));
}
}
答案 0 :(得分:0)