0

I am trying to generate a key pair in asymmetric encryption in Java, but I am getting an invalid key exception error and it says No installed provider supports this key: sun.security.rsa.RSAPrivateCrtKeyImpl.

private static byte[] encrypt(byte[] inpBytes, PrivateKey prvk,
      String xform) throws Exception {
    Cipher cipher = Cipher.getInstance(xform);
    cipher.init(Cipher.ENCRYPT_MODE, prvk);
    return cipher.doFinal(inpBytes);
}

@Override
public byte[] uploadFile(byte[] data, String name, String file, int size)
      throws RemoteException {
    // TODO Auto-generated method stub
    byte[] keyss=null;
    try {
        OutputStream out =
          new FileOutputStream(new File("C:\\Users\\Amaresh\\Documents\\Cloud\\"
          + name + "\\" + file));
        String xform = "DES/CTR/NoPadding";
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA"); // Original
        kpg.initialize(1024); // 512 is the keysize.//try 1024 biit
        KeyPair kp = kpg.genKeyPair();
        PublicKey pubk = kp.getPublic();
        PrivateKey prvk = kp.getPrivate();
        keyss = pubk.getEncoded();
        byte[] encBytes = encrypt(data, prvk, xform);
        System.out.println("Keypair generated");
        out.write(encBytes, 0, encBytes.length);
        out.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NoSuchProviderException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return keyss;
}

I just want to do asymmetric encryption where I encrypt the data with private key and store the public key to decrypt it. I am a beginner and I am sorry for my blunt mistakes.

blalasaadri
  • 5,990
  • 5
  • 38
  • 58
Sam
  • 104
  • 3
  • 10
  • I'm not exactly sure, but it might be that you need to install the unlimited key strength policy: http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html . Note that this code does not only do public/private key generation, it also encrypts a file in one go. That would then result in a private/public key pair per encrypted file, I can't imagine that is what you really want. – Gimby Jan 20 '15 at 13:15

2 Answers2

4

You are generating the keys correctly.

KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(1024);
KeyPair keys = kpg.generateKeyPair();

The problem is in your:

byte[] encBytes = encrypt(data, prvk, xform);

Most likely because you are passing in the String "DES/CTR/NoPadding". You cannot encrypt using DES with an RSA key pair.

bhspencer
  • 13,086
  • 5
  • 35
  • 44
1

DES Ciphers don't support RSA keys. DES is a symmetric cipher algorithm while RSA is asymmetric. Symmetric ciphers require the use of the same key for encryption and decryption while asymmetric ciphers use public/private keypairs.

You have two options to get your encryption working:

  1. You could use your current symmetric cipher and create a symmetric key (maybe by using a KeyGenerator)

  2. You can change your cipher instance to an assymetric one like "RSA".

    String xform = "RSA";
    

Note:
What you are trying to do is not encrypting but signing as encryption would be done with the public key.
Furthermore you should not encrypt complete files by an asymmetric mode but only a symmetric key which you use to encrypt and decrypt your data symmetrically. See http://en.wikipedia.org/wiki/Public-key_cryptography#Computational_cost.

flo
  • 9,713
  • 6
  • 25
  • 41