gpt4 book ai didi

javax.crypto.BadPaddingException : Decryption error when using Java RSA encryption

转载 作者:搜寻专家 更新时间:2023-11-01 03:15:42 27 4
gpt4 key购买 nike

我正在尝试设置 RSA 加密,这样我就可以制作一个简单的库供自己将来使用,但我无法让这个简单的代码正常工作。这是源代码:

package rsa.testing;

import java.security.KeyPair;
import java.security.KeyPairGenerator;

import javax.crypto.Cipher;

public class AloneTest
{
public static void doTest()
{
try
{
KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA");
gen.initialize(2048);
KeyPair pair = gen.genKeyPair();
System.out.println("Public key: " + byteArrayToString(pair.getPublic().getEncoded()));
System.out.println("Private key: " + byteArrayToString(pair.getPrivate().getEncoded()));

Cipher encryptPublic = Cipher.getInstance("RSA");
encryptPublic.init(Cipher.ENCRYPT_MODE, pair.getPublic());

Cipher decryptPublic = Cipher.getInstance("RSA");
decryptPublic.init(Cipher.DECRYPT_MODE, pair.getPublic());

Cipher encryptPrivate = Cipher.getInstance("RSA");
encryptPrivate.init(Cipher.ENCRYPT_MODE, pair.getPrivate());

Cipher decryptPrivate = Cipher.getInstance("RSA");
decryptPrivate.init(Cipher.DECRYPT_MODE, pair.getPrivate());

System.out.println("All ciphers created.");
System.out.println();

String plainText = "Hello World!";
System.out.println("Plain text: " + plainText);
String encrypted = new String(encryptPrivate.doFinal(plainText.getBytes()));
System.out.println("Encrypted: " + encrypted);
String decrypted = new String(decryptPublic.doFinal(encrypted.getBytes()));
System.out.println("Decrypted: " + decrypted);
}
catch (Exception e)
{
e.printStackTrace();
}
}

private static String byteArrayToString(byte[] bytes)
{
String str = "";

for (byte b : bytes)
{
str += b + " ";
}

return str.trim();
}
}

And this the output (image because it doesn't let me copy the encrypted text).

有什么想法吗?

编辑:更改了以下内容:

String plainText = "Hello World!";
System.out.println("Plain text: " + plainText);
String encrypted = byteToBase64String(encryptPublic.doFinal(plainText.getBytes()));
System.out.println("Encrypted: " + encrypted);
String decrypted = byteToBase64String(decryptPublic.doFinal(encrypted.getBytes()));
System.out.println("Decrypted: " + decrypted);

用于将字节转换为Base64的方法:

private static String byteToBase64String(byte[] bytes)
{
return Base64.getEncoder().encodeToString(bytes);
}

但是现在我得到这个错误:

Plain text: Hello World!
Encrypted: dCTsYrQTDfNkRr6+nupWW4ntsmD69wrWS1TDvS3Uk1B6lCKkycegAb7wJgnXNolZ58dRXgbzEyCkeM/IbH0ruIEMfuhS5vrggo1s9+6MQN2hW1I4iElJfAdGKYQ8H9pFhAws+VQGetjwvJwUI9iWIi15721vc508/ks7rn4oQOk=
javax.crypto.IllegalBlockSizeException: Data must not be longer than 128 bytes
at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:344)
at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:389)
at javax.crypto.Cipher.doFinal(Cipher.java:2164)
at rsa.testing.AloneTest.doTest(AloneTest.java:40)
at rsa.testing.Main.main(Main.java:21)

我必须进行某种拆分吗?还是我做错了什么?

最佳答案

你错过了这个:

公钥加密时,应该用私钥解密
还有
私钥加密时,应该用公钥解密

更新

当使用例如加密时私钥 然后尝试使用相同 私钥解密,您将得到 java.crypto.BadPaddingException

我无法分享证明这一点的代码,但我使用以下方法进行了测试:

KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
keyPairGen.initialize(2048);
keyPair = keyPairGen.generateKeyPair();

我使用的密码是:

Cipher.getInstance("RSA/ECB/PKCS1Padding")

关于javax.crypto.BadPaddingException : Decryption error when using Java RSA encryption,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53969606/

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