Java 带AES加密和解密的RSA
我的RSA解密有什么问题 以下是加密代码:Java 带AES加密和解密的RSA,java,encryption,cryptography,aes,rsa,Java,Encryption,Cryptography,Aes,Rsa,我的RSA解密有什么问题 以下是加密代码: try { //Get the public key from the keyStore and set up the Cipher object PublicKey publicKey = getPubKey(keyStore,keyName); Cipher rsaCipher = Cipher.getInstance("RSA"); rsaCipher.init(Cipher
try {
//Get the public key from the keyStore and set up the Cipher object
PublicKey publicKey = getPubKey(keyStore,keyName);
Cipher rsaCipher = Cipher.getInstance("RSA");
rsaCipher.init(Cipher.ENCRYPT_MODE, publicKey);
//Read the plainText
System.out.println("Loading plaintext file: "+inFile);
RandomAccessFile rawDataFromFile = new RandomAccessFile(inFile, "r");
byte[] plainText = new byte[(int)rawDataFromFile.length()];
rawDataFromFile.read(plainText);
// Generate a symmetric key to encrypt the data and initiate the AES Cipher Object
System.out.println("Generating AES key");
KeyGenerator sKenGen = KeyGenerator.getInstance("AES");
Key aesKey = sKenGen.generateKey();
Cipher aesCipher = Cipher.getInstance("AES");
aesCipher.init(Cipher.ENCRYPT_MODE, aesKey);
// Encrypt the symmetric AES key with the public RSA key
System.out.println("Encrypting Data");
byte[] encodedKey = rsaCipher.doFinal(aesKey.getEncoded());
// Encrypt the plaintext with the AES key
byte[] cipherText = aesCipher.doFinal(plainText);
//Write the encrypted AES key and Ciphertext to the file.
System.out.println("Writting to file: "+outFile);
FileOutputStream outToFile = new FileOutputStream(outFile);
outToFile.write(encodedKey);
outToFile.write(cipherText);
System.out.println("Closing Files");
rawDataFromFile.close();
outToFile.close();
}
catch (Exception e) {
System.out.println("Doh: "+e);
}
这是我的解密代码,我认为它会很好的工作,但它没有。有人能帮我吗
它一直有错误:javax.crypto.BadPaddingException:解密错误
真的不知道该怎么办,谁能给我一些建议
private static void decryptRSA() {
try {
System.out.println("Loading plaintext file: "+inFile);
RandomAccessFile rawDataFromFile = new RandomAccessFile(inFile, "r");
byte[] cipherText = new byte[(int)rawDataFromFile.length()];
byte encodedkey[] = new byte[256];
rawDataFromFile.read(encodedkey, 0, 256);
rawDataFromFile.read(cipherText);
PublicKey publicKey = getPubKey(keyStore,keyName);
Cipher rsaCipher = Cipher.getInstance("RSA");
rsaCipher.init(Cipher.DECRYPT_MODE, publicKey);
byte[] aeskey = rsaCipher.doFinal(encodedkey);
SecretKeySpec aesKey = new SecretKeySpec(aeskey, "AES");
Cipher aesCipher = Cipher.getInstance("AES");
aesCipher.init(Cipher.DECRYPT_MODE, aesKey);
byte[] plainText = aesCipher.doFinal(cipherText);
System.out.println("Writting to file: "+outFile);
FileOutputStream outToFile = new FileOutputStream(outFile);
outToFile.write(plainText);
System.out.println("Closing Files");
rawDataFromFile.close();
outToFile.close();
}
catch (Exception e) {
System.out.println("Doh: "+e);
}
}
密文
数组的长度不正确。您应该减去256,或者将实际读取长度传递给Cipher.doFinal(),或者实际上两者都传递package rsa;
import java.math.BigInteger;
import java.util.Scanner;
public class RSA {
public static void main(String[] args) {
// TODO code application logic here
Scanner sc=new Scanner(System.in);
System.out.println("Enter Plaintext");
String plaintext=sc.nextLine();
BigInteger n,n1,n2,M=BigInteger.valueOf(0),M1,p,q,pn,e,d,c;
System.out.println("Enter p q");
p=sc.nextBigInteger();
q=sc.nextBigInteger();
n=p.multiply(q);
n1=p.subtract(BigInteger.valueOf(1));
n2=q.subtract(BigInteger.valueOf(1));
pn=n1.multiply(n2);
System.out.println("Choose e");
e=sc.nextBigInteger();
d=e.modInverse(pn);
System.out.println("D is"+d);
plaintext=plaintext.toLowerCase();
char arr[]=new char[]{'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
if(plaintext.length()%2!=0)
plaintext=plaintext+"a";
String cc="",s="",plain="",t="";
int z;
for(int i=0;i<plaintext.length();i=i+2)
{
z=plaintext.codePointAt(i)-87;
s=s+z;
z=plaintext.codePointAt(i+1)-87;
s=s+z;
M=BigInteger.valueOf(Long.parseLong(s));
t=t+M.toString();
c=M.pow(e.intValue());
c=c.mod(n);
cc=cc+c;
s="";
M1=c.pow(d.intValue());
M1=M1.mod(n);
plain=plain+M1.toString();
}
System.out.println("Plaintext is "+plaintext);
System.out.println("Before Encryption "+t);
System.out.println("cipher "+cc);
System.out.println("First Decryption "+plain);
String h="";
for(int i=0;i<plain.length();i=i+2)
{
int k=Integer.parseInt(Character.toString(plain.charAt(i))+Character.toString(plain.charAt(i+1)));
h=h+arr[k-10];
}
System.out.println("Decryption "+h);
}
}
密文
数组的长度不正确。您应该减去256,或者将实际读取长度传递给Cipher.doFinal(),或者实际上两者都传递包rsa;
package rsa;
import java.math.BigInteger;
import java.util.Scanner;
public class RSA {
public static void main(String[] args) {
// TODO code application logic here
Scanner sc=new Scanner(System.in);
System.out.println("Enter Plaintext");
String plaintext=sc.nextLine();
BigInteger n,n1,n2,M=BigInteger.valueOf(0),M1,p,q,pn,e,d,c;
System.out.println("Enter p q");
p=sc.nextBigInteger();
q=sc.nextBigInteger();
n=p.multiply(q);
n1=p.subtract(BigInteger.valueOf(1));
n2=q.subtract(BigInteger.valueOf(1));
pn=n1.multiply(n2);
System.out.println("Choose e");
e=sc.nextBigInteger();
d=e.modInverse(pn);
System.out.println("D is"+d);
plaintext=plaintext.toLowerCase();
char arr[]=new char[]{'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
if(plaintext.length()%2!=0)
plaintext=plaintext+"a";
String cc="",s="",plain="",t="";
int z;
for(int i=0;i<plaintext.length();i=i+2)
{
z=plaintext.codePointAt(i)-87;
s=s+z;
z=plaintext.codePointAt(i+1)-87;
s=s+z;
M=BigInteger.valueOf(Long.parseLong(s));
t=t+M.toString();
c=M.pow(e.intValue());
c=c.mod(n);
cc=cc+c;
s="";
M1=c.pow(d.intValue());
M1=M1.mod(n);
plain=plain+M1.toString();
}
System.out.println("Plaintext is "+plaintext);
System.out.println("Before Encryption "+t);
System.out.println("cipher "+cc);
System.out.println("First Decryption "+plain);
String h="";
for(int i=0;i<plain.length();i=i+2)
{
int k=Integer.parseInt(Character.toString(plain.charAt(i))+Character.toString(plain.charAt(i+1)));
h=h+arr[k-10];
}
System.out.println("Decryption "+h);
}
}
导入java.math.biginger;
导入java.util.Scanner;
公共类RSA{
公共静态void main(字符串[]args){
//此处的TODO代码应用程序逻辑
扫描仪sc=新的扫描仪(System.in);
System.out.println(“输入明文”);
字符串明文=sc.nextLine();
BigInteger n,n1,n2,M=BigInteger。值为(0),M1,p,q,pn,e,d,c;
System.out.println(“输入pq”);
p=sc.NextBiginger();
q=sc.nextbiginger();
n=p.乘以(q);
n1=p.subtract(BigInteger.valueOf(1));
n2=q.subtract(BigInteger.valueOf(1));
pn=n1.乘以(n2);
System.out.println(“选择e”);
e=sc.nextBigInteger();
d=e.modInverse(pn);
System.out.println(“D是”+D);
明文=明文。toLowerCase();
char arr[]=新字符[]{'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
if(明文.length()%2!=0)
纯文本=纯文本+a;
字符串cc=“”,s=“”,plain=“”,t=“”;
intz;
对于(int i=0;ipackage rsa;
导入java.math.biginger;
导入java.util.Scanner;
公共类RSA{
公共静态void main(字符串[]args){
//此处的TODO代码应用程序逻辑
扫描仪sc=新的扫描仪(System.in);
System.out.println(“输入明文”);
字符串明文=sc.nextLine();
BigInteger n,n1,n2,M=BigInteger。值为(0),M1,p,q,pn,e,d,c;
System.out.println(“输入pq”);
p=sc.NextBiginger();
q=sc.nextbiginger();
n=p.乘以(q);
n1=p.subtract(BigInteger.valueOf(1));
n2=q.subtract(BigInteger.valueOf(1));
pn=n1.乘以(n2);
System.out.println(“选择e”);
e=sc.nextBigInteger();
d=e.modInverse(pn);
System.out.println(“D是”+D);
明文=明文。toLowerCase();
char arr[]=新字符[]{'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
if(明文.length()%2!=0)
纯文本=纯文本+a;
字符串cc=“”,s=“”,plain=“”,t=“”;
intz;
对于(int i=0;i您应该阅读RandomAccessFile
的read
方法的API文档。您的文件处理不正确。您应该阅读RandomAccessFile
的read
方法的API文档。您的文件处理不正确。如何从密钥存储中获取私钥?呃,通过 KeyStore < /代码> API?但是,虽然现在我得到了私钥并进行解密,但现在我认为错误来自AES部分,因为它给出了错误:JavaX.Fulk.BADPADIDENEXCELT:给定最后一个块没有正确填充,我如何修复它以使AES解密工作?您应该总是考虑向上投票和/或接受答案Y。你觉得这里有用。我是新来问这个问题的,我的坏:)如何从密钥存储中获取私钥?呃,通过KeyStore
API?然而,尽管现在我获取了私钥并使用它进行解密,但我认为现在错误来自AES部分,因为它给出了错误:javax.crypto.BadPaddingException:给定未正确填充的最后一个块,如何修复它以使AES解密工作你应该总是考虑接受和/或接受你在这里找到有用的答案。我是一个新来提出问题的人,我的坏消息:欢迎使用StackOverflow。请尝试在您的答案中添加一些描述性文本。这将使其他人更容易理解您的代码的作用。欢迎使用StackOverflow。请尝试在您的答案中添加一些描述性文本。这将使其他人更容易理解您的代码的作用。