我正在尝试使用RSA 2048加密和解密数据。
我们有一个公钥和私钥,并将在整个过程中使用相同的密钥。 但问题是,当我解密时,我得到javax.crypto.BadPaddingException:数据必须从零开始
File file = new File("C:\\temp-ldi\\pubkey.txt");
FileWriter writer = new FileWriter(file);
file.createNewFile();
encryptedText = RSACrypto.encrypt("PLAIN TEXT"); //no argument of pub-key, generate key pair
writer.write(new BASE64Encoder().encode(RSACrypto.pubKeyToBytes(RSACrypto.publicKey)));
writer.close();
file = new File("C:\\temp-ldi\\privkey.txt");
writer = new FileWriter(file);
file.createNewFile();
writer.write(new BASE64Encoder().encode(RSACrypto.privKeyToBytes(RSACrypto.privateKey)));
writer.close();
然后我使用下面的代码来解密数据
File privfile = new File("C:\\temp-ldi\\privkey.txt");
File pubfile = new File("C:\\temp-ldi\\pubkey.txt");
FileReader reader = new FileReader(pubfile);
// file.createNewFile();
BufferedReader br = new BufferedReader(reader);
StringBuilder sb = new StringBuilder();
String s;
while ((s = br.readLine()) != null) {
sb.append(s);
}
br.close();
reader.close();
this.encryptedText = RSACrypto.encrypt("PLAIN TEXT", sb.toString());
reader = new FileReader(privfile);
br = new BufferedReader(reader);
sb = new StringBuilder();
while ((s = br.readLine()) != null) {
sb.append(s);
}
br.close();
reader.close();
System.out.println(RSACrypto.decrypt(this.encryptedText, sb.toString()));
所有加密/解密字符串将以Base64Encoder / Base64Decoder格式返回。
如何从文件/简单字符串传递私钥,以便不修改密钥。
更新 RSACrypto课程:http://sebsauvage.net/paste/?83517f2b3db94d24#Sdu12/vXPuxa5AxO95FPgKSF6N40R2DzD6lwQkvroyE=
答案 0 :(得分:3)
好的,RSACrypto中的问题。加密文件时,它会在每次新密钥对(encrypt
)时创建。只需从加密中删除新的密钥对生成,在需要时直接调用newKeyPair
。
并且静态变量对多线程环境没有好处。
我建议抛出RSACrypto类,或至少重写它。我不知道为什么你这么害怕使用byte []类型以及为什么你需要所有东西都是BASE64编码的。没有额外的编码/解码,代码将非常简单。
以下是工作示例(没有RSACrypto),您可以将其用作模板:
File file = new File("C:\\temp-ldi\\pubkey.txt");
FileWriter writer = new FileWriter(file);
file.createNewFile();
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
generator.initialize(2048, new SecureRandom());
KeyPair keyPayr = generator.generateKeyPair();
writer.write(new BASE64Encoder().encode(keyPayr.getPublic().getEncoded()));
writer.flush();
writer.close();
file = new File("C:\\temp-ldi\\privkey.txt");
writer = new FileWriter(file);
file.createNewFile();
writer.write(new BASE64Encoder().encode(keyPayr.getPrivate().getEncoded()));
writer.flush();
writer.close();
File privfile = new File("C:\\temp-ldi\\privkey.txt");
File pubfile = new File("C:\\temp-ldi\\pubkey.txt");
FileReader reader = new FileReader(pubfile);
BufferedReader br = new BufferedReader(reader);
StringBuilder sb = new StringBuilder();
String s;
while ((s = br.readLine()) != null) {
sb.append(s);
}
br.close();
reader.close();
PublicKey publicKey = KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(new BASE64Decoder().decodeBuffer(sb.toString())));
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
String encryptedText = new BASE64Encoder().encode(cipher.doFinal("PLAIN TEXT".getBytes("UTF-8")));
System.out.println("encrypted: " + encryptedText);
reader = new FileReader(privfile);
br = new BufferedReader(reader);
sb = new StringBuilder();
while ((s = br.readLine()) != null) {
sb.append(s);
}
br.close();
reader.close();
PrivateKey privateKey = KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(new BASE64Decoder().decodeBuffer(sb.toString())));
cipher.init(Cipher.DECRYPT_MODE, privateKey);
System.out.println( new String(cipher.doFinal (new BASE64Decoder().decodeBuffer(encryptedText))));