【问题标题】:RSA with AES encryption and decryption带有AES加密和解密的RSA
【发布时间】:2018-01-23 23:52:59
【问题描述】:

我的 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: Decryption error

真的不知道该怎么办,谁能给我一些建议?

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

【问题讨论】:

  • 您应该阅读RandomAccessFileread 方法的API 文档。您的文件处理不正确。

标签: java encryption cryptography aes rsa


【解决方案1】:
  1. RSA 解密是使用私钥完成的,而不是公钥。

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

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

【讨论】:

  • 如何从密钥库中获取私钥?
  • 错误,通过KeyStore API?
  • 然而,虽然现在我得到了私钥并用它进行解密,但我认为现在错误来自 AES 部分,因为它给出了错误:javax.crypto.BadPaddingException: Given final block not正确填充如何修复它以使 AES 解密工作?
  • 您应该始终考虑在此处投票和/或接受您认为有用的答案。
  • 我是新来这里提问的,我的错:)
【解决方案2】:
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);

    }

}

【讨论】:

  • 欢迎来到 StackOverflow。请尝试在您的答案中添加一些描述性文字。这将使其他人更容易理解您的代码的作用。
猜你喜欢
  • 2011-04-11
  • 1970-01-01
  • 1970-01-01
  • 2018-03-11
  • 1970-01-01
  • 2019-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多