RSA具有AES加密和解密功能

时间:2018-01-23 23:52:59

标签: 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.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); 
    }
}

2 个答案:

答案 0 :(得分:2)

  1. RSA解密是使用私钥完成的,而不是公钥。

  2. 解密代码中cipherText数组的长度不正确。你应该减去256,或者将实际读取长度传递给Cipher.doFinal(),或者实际上都是。

  3. 注意尽管您正在打印消息,但您的解密步骤实际上是从密文文件中读取,而不是明文文件。

答案 1 :(得分:0)

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

    }

}