RSA 2048加密解密 - 异常 [英] RSA 2048 Encryption Decryption - Exception

查看:1156
本文介绍了RSA 2048加密解密 - 异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我们有一个公共密钥和私钥,并将一直使用。
但问题是,当我解密,我得到javax.crypto.BadPaddingException:数据必须从零开始

 文件文件=新建文件(C:\\\\\\\\\\\\\ 
FileWriter writer = new FileWriter(file);
file.createNewFile();
encryptedText = RSACrypto.encrypt(PLAIN TEXT); //没有pub-key的参数,生成密钥对
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();

然后我使用下面的代码解密数据

 文件privfile =新文件(C:\\\temp-ldi\\privkey.txt); 
文件pubfile =新文件(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 =

解决方案

OK,RSACrypto中的问题。加密文件时,会创建每次新的密钥对(在 encrypt )中。只需从加密中删除新的密钥对生成,当您需要时,直接调用 newKeyPair
这个静态变量对于多线程环境没有好处。



我建议抛出RSACrypto类,或至少重写。我不知道你为什么害怕使用byte []类型,为什么你需要一切都是BASE64编码的。代码将很简单,无需额外的编码/解码。



这里是工作示例(没有RSACrypto),可以使用它作为模板:

 文件文件=新文件(C:\\\temp-ldi\\pubkey.txt); 
FileWriter writer = new FileWriter(file);
file.createNewFile();
KeyPairGenerator generator = KeyPairGenerator.getInstance(RSA);
generator.initialize(2048,新的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();


文件privfile =新文件(C:\\\temp-ldi\\privkey.txt);
文件pubfile =新文件(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.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)));


I am trying to encrypt and decrypt the data with RSA 2048.

We have one public key and private key and will be using same throughout. but the problem is, when I decrypt, I am getting 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();

then I am using below code to decrypt the data

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

all the encryption/decryption string will return in Base64Encoder/Base64Decoder format.

How do I pass the private key from file/simple string so that the key is not modified.

Update RSACrypto class : http://sebsauvage.net/paste/?83517f2b3db94d24#Sdu12/vXPuxa5AxO95FPgKSF6N40R2DzD6lwQkvroyE=

解决方案

OK, problem in RSACrypto. When you encrypt file, it creates every time new keypair (in encrypt). Just remove new keypair generation from encrypt, call newKeyPair direcly, when you need it. And that static variables does no good for multi-thread environment.

I'll suggest to throw RSACrypto class, or rewrite it at least. I don't know why you so afraid of using byte[] type and why you need everything be BASE64 encoded. Code will a lot simple without additional encoding/decoding.

Here is working example (without RSACrypto), you can use it as template:

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

这篇关于RSA 2048加密解密 - 异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆