gpt4 book ai didi

java - 加密和解密最终 block 未正确填充错误?

转载 作者:行者123 更新时间:2023-12-02 11:38:40 25 4
gpt4 key购买 nike

我正在尝试修复我的代码。在我的代码中,我试图生成一个 16 位 key ,我已经完成了。其次,生成一条随机消息,这也完成了。加密和解密我遇到错误的数据。最后有一个强力算法来解密消息,我稍后会尝试这样做。因此,对于我的加密,代码对其进行加密,但不加密随机生成的字符串。我收到一堆错误。

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);
}
}

最佳答案

一个问题是您正在执行一些我不确定是否有效的类型转换。例如,在您的加密方法中,您将加密字节数组转换为字符串(使用新字符串(加密)),然后返回字节数组在 decryption 方法中(使用 getBytes())。结果不是相同的字节数组。

为什么您使用 String 而不是 byte[] 作为明文和密文? (与您的 key 相同,为什么使用 int 而不是 byte[]?)

关于java - 加密和解密最终 block 未正确填充错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48736118/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com