gpt4 book ai didi

Java不可靠的字符串RSA加密/解密

转载 作者:行者123 更新时间:2023-12-03 19:05:29 33 4
gpt4 key购买 nike

我有一个非常恼人的问题,Java中使用RSA算法对字符串进行不可靠的加密和解密。它似乎只能在大约 35% 的时间内工作,而且我不明白为什么它有时能工作,有时却不能。这是我写的一些测试代码,试图验证加密/解密中的随机性。它运行了 100 圈,每次都对相同的字符串进行加密和解密,并打印成功的次数:

    public static void main(String[] args) throws Exception {
byte[] dataToEncrypt = "Hello World!".getBytes("UTF-16LE");
byte[] cipherData = null;
byte[] decryptedData;

KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(512);
KeyPair kp = kpg.genKeyPair();
Key publicKey = kp.getPublic();
Key privateKey = kp.getPrivate();
KeyFactory fact = KeyFactory.getInstance("RSA");

RSAPublicKeySpec pub = (RSAPublicKeySpec) fact.getKeySpec(publicKey,
RSAPublicKeySpec.class);

RSAPublicKeySpec spec = new RSAPublicKeySpec(pub.getModulus(), pub
.getPublicExponent());
KeyFactory factory = KeyFactory.getInstance("RSA");

PublicKey publicKeyRSA = factory.generatePublic(spec);
Cipher cipher = Cipher.getInstance("RSA");

int k = 0;
for (int i = 0; i < 100; i++) {
try {
cipher.init(Cipher.ENCRYPT_MODE, publicKeyRSA);
cipherData = cipher.doFinal(dataToEncrypt);
} catch (Exception e1) {
System.out.println("Encrypt error");
}

String s = new String(cipherData, "UTF-16LE");
cipher.init(Cipher.DECRYPT_MODE, privateKey);

try {
decryptedData = cipher.doFinal(s.getBytes("UTF-16LE"));
System.out.println("Decrypted: "
+ new String(decryptedData, "UTF-16LE"));
k += 1;
} catch (Exception e1) {
System.out.println("Decrypt error");
}
}
System.out.println("Number of correct decryptions is: " + k);

}

我尝试用各种值初始化 KeyPairGenerator 但没有成功。

编辑:现在它就像一个魅力,感谢 Base64 :

    import java.io.FileOutputStream;
import java.io.OutputStream;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PublicKey;
import java.security.spec.RSAPublicKeySpec;
import javax.crypto.Cipher;

public class Test {

public static void main(String[] args) throws Exception {
byte[] dataToEncrypt = "Hello World!".getBytes("UTF-16LE");
byte[] cipherData = null;
byte[] decryptedData;


KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(512);
KeyPair kp = kpg.genKeyPair();
Key publicKey = kp.getPublic();
Key privateKey = kp.getPrivate();
KeyFactory fact = KeyFactory.getInstance("RSA");

RSAPublicKeySpec pub = (RSAPublicKeySpec) fact.getKeySpec(publicKey,
RSAPublicKeySpec.class);

RSAPublicKeySpec spec = new RSAPublicKeySpec(pub.getModulus(), pub
.getPublicExponent());
KeyFactory factory = KeyFactory.getInstance("RSA");

PublicKey publicKeyRSA = factory.generatePublic(spec);
Cipher cipher = Cipher.getInstance("RSA");

int k = 0;
for (int i = 0; i < 100; i++) {
try {
cipher.init(Cipher.ENCRYPT_MODE, publicKeyRSA);
cipherData = cipher.doFinal(dataToEncrypt);
} catch (Exception e1) {
System.out.println("Encrypt error");
}

String s = Base64.encodeBytes(cipherData);
cipher.init(Cipher.DECRYPT_MODE, privateKey);

try {
decryptedData = cipher.doFinal(Base64.decode(s));
System.out.println("Decrypted: "
+ new String(decryptedData, "UTF-16LE"));
k += 1;
} catch (Exception e1) {
System.out.println("Decrypt error");
}
}
System.out.println("Number of correct decryptions is: " + k);

}

最佳答案

这就是问题所在,或者至少是问题:

String s = new String(cipherData, "UTF-16LE");

您正在获取任意二进制数据 并尝试从中创建一个字符串,将其视为UTF-16 编码文本。它不是。它是任意二进制数据。

要么将其保存为二进制形式(如 byte[]),要么使用 base64 以安全、可逆的方式将其转换为文本。 (例如,This public domain base64 encoder 具有合理的 API。)

关于Java不可靠的字符串RSA加密/解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9548569/

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