3

我正在尝试使用 RSA 2048 加密和解密数据。

我们有一个公钥和一个私钥,并且将始终使用相同的。但问题是,当我解密时,我得到 javax.crypto.BadPaddingException: Data must start with zero

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=

4

1 回答 1

4

好的,RSACrypto 中的问题。当您加密文件时,它每次都会创建新的密钥对(in 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))));
于 2012-12-21T14:01:08.830 回答