Java 加密和解密最终块未正确填充错误?
我正在尝试修复我的代码。在我的代码中,我试图生成一个16位的密钥,我已经这样做了。其次,生成一条随机消息,这也是完成的。对我出错的数据进行加密和解密。最后,我有一个蛮力算法来解密消息,稍后我会尝试这样做。因此,对于我的加密,代码加密它,但不加密随机生成的字符串。我犯了很多错误Java 加密和解密最终块未正确填充错误?,java,encryption,import,cryptography,Java,Encryption,Import,Cryptography,我正在尝试修复我的代码。在我的代码中,我试图生成一个16位的密钥,我已经这样做了。其次,生成一条随机消息,这也是完成的。对我出错的数据进行加密和解密。最后,我有一个蛮力算法来解密消息,稍后我会尝试这样做。因此,对于我的加密,代码加密它,但不加密随机生成的字符串。我犯了很多错误 Errors: Given final block not properly padded. Such issues can arise if a bad key is used during decryption.
Errors:
Given final block not properly padded. Such issues can arise if a bad key is used during decryption.
at java.base/com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:991)
at java.base/com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:847)
at java.base/com.sun.crypto.provider.BlowfishCipher.engineDoFinal(BlowfishCipher.java:319)
at java.base/javax.crypto.Cipher.doFinal(Cipher.java:2189)
at Assignment1Demo.decryption(Assignment1Demo.java:109)
at Assignment1Demo.bruteForce(Assignment1Demo.java:130)
at Assignment1Demo.main(Assignment1Demo.java:30)
Exception in thread "main" java.lang.NullPointerException
at Assignment1Demo.bruteForce(Assignment1Demo.java:131)
at Assignment1Demo.main(Assignment1Demo.java:30)
我的代码:
import java.util.Random;
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
public class Assignment1Demo {
private static String msg;
private static String msgE;
private static String msgD;
private static int key;
public static void main(String[] args){
//TODO: You can only call methods in main method
key = generateKey();
msg = generateMsg();
msgE = encryption(key,msg);
bruteForce(msgE);
}
private static int generateKey() {
//TODO: implement step a (randomly generate 16-bit key)
//16 bit digit means 2^16 -1 in decimal
Random rand = new Random();
return rand.nextInt((int) (Math.pow(2, 16)-1));
}
private static String generateMsg() {
//TODO: implement step b (randonly generate a string with an even number of characters)
String chractersU="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String chractersL=chractersU.toLowerCase();
String space=" ";
String alphanum=chractersU+space+chractersL;
String random="";
int length=alphanum.length();
Random rand=new Random();
char[] text=new char[length];
for(int i=0;i<length;i++) {
text[i]=alphanum.charAt(rand.nextInt(alphanum.length()));
}
for(int i=0;i<text.length/2;i++) {
if(text.length%2!=0) {
random += text[i];
}
}
return random;
}
private static String encryption (int key, String msg) {
//TODO: implement step c (encrypt the message)
String strData="";
String strKey=Integer.toString(key);
try {
SecretKeySpec skeyspec=new SecretKeySpec(strKey.getBytes(),"Blowfish");
Cipher cipher=Cipher.getInstance("Blowfish");
cipher.init(Cipher.ENCRYPT_MODE, skeyspec);
byte[] encrypted=cipher.doFinal(msg.getBytes());
strData=new String(encrypted);
} catch (Exception e) {
e.printStackTrace();
}
return strData;
}
private static void decryption(int key, String msgE) {
//TODO: implement step d (decryption)
String strKey = Integer.toString(key);
String strData="";
try {
SecretKeySpec skeyspec=new SecretKeySpec(strKey.getBytes(),"Blowfish");
Cipher cipher=Cipher.getInstance("Blowfish");
cipher.init(Cipher.DECRYPT_MODE, skeyspec);
byte[] decrypted=cipher.doFinal(msgE.getBytes());
strData=new String(decrypted);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(strData);
}
private static void bruteForce(String msgE) {
//TODO: implement bruteForce algorithm, you may need the above decryption(key,msgE) method
boolean isEnglisString = msgE.matches("[a-zA-Z]+");
if(isEnglisString)
System.out.println("Yes encrypted message is Randomly English generated message " + msgE);
else
System.out.println("encrypted message is Not Randomly english generated message "+msgE);
decryption(key, msgE);
isEnglisString = msgD.matches("[a-zA-Z]+");
if(isEnglisString)
System.out.println("Yes decrypted message is Randomly english generated message "+ msgD);
else
System.out.println("decrypted message is not Randomly english generated message "+ msgD);
}
}
import java.util.Random;
导入java.security.security;
导入javax.crypto.Cipher;
导入javax.crypto.spec.SecretKeySpec;
公共类分配1内存{
私有静态字符串msg;
私有静态字符串msgE;
私有静态字符串msgD;
私钥;
公共静态void main(字符串[]args){
//TODO:只能在main方法中调用方法
key=generateKey();
msg=generateMsg();
msgE=加密(密钥,msg);
布鲁特福斯(msgE);
}
私有静态int-generateKey(){
//TODO:执行步骤a(随机生成16位密钥)
//16位数字表示十进制中的2^16-1
Random rand=新的Random();
返回rand.nextInt((int)(数学功率(2,16)-1));
}
私有静态字符串生成器msg(){
//TODO:实现步骤b(仅生成偶数个字符的字符串)
字符串chractersU=“abcdefghijklmnopqrstuvxyz”;
字符串chractersL=chractersU.toLowerCase();
字符串空格=”;
字符串alphanum=chractersU+space+chractersL;
字符串random=“”;
int length=alphanum.length();
Random rand=新的Random();
字符[]文本=新字符[长度];
对于(int i=0;i一个问题是,您正在进行一些类型转换,我不确定这些转换是否有效。例如,在加密
方法中,您将加密的
字节数组转换为字符串(使用新字符串(加密)
),然后在解密
方法中返回字节数组(使用getBytes()
)。结果不是相同的字节数组
为什么对明文和密文使用String
而不是byte[]