Hows it going, I have generated a secret key using AES encryption, which is working perfectly. Now I intend the to encrypt that key using an RSA public key that i have generated elsewhere. Ive tried switching from AES to RSA, adding padding and ive also tried importing bouncy castle, but im not sure im doing that right. None of these methods seem to be working as I usually end up with the error - javax.crypto.IllegalBlockSizeException: Data must not be longer than 117 bytes or this one java.security.InvalidKeyException: No installed provider supports this key: sun.security.rsa.RSAPublicKeyImpl. So im here to ask "Whats wrong with my code"
import java.io.*; import java.security.*; import org.bouncycastle.*; import javax.crypto.*; public class ClientSecretKeyEncryption { protected static final String ALGORITHM = "RSA"; public static void main(String args[]) { //Security.addProvider(new BouncyCastleProvider()); // Encrypts secret key using servers public key try { // File containing RSA Public key FileInputStream keyFIS = new FileInputStream("/Users/Client/RSAPublicKeyFile"); ObjectInputStream keyOIS = new ObjectInputStream(keyFIS); // Create RSA cipher instance Cipher rsaCipher = Cipher.getInstance("AES"); // Read in the AES key Key RsaPublicKeyFile = (PublicKey) keyOIS.readObject(); // Initialize the cipher for encryption rsaCipher.init(Cipher.ENCRYPT_MODE, (Key) RsaPublicKeyFile); keyOIS.close(); keyFIS.close(); // rsaCipher.init(Cipher.ENCRYPT_MODE, (PublicKey) keyOIS.readObject()); // File for writing out encrypted secret key to server FileOutputStream fos = new FileOutputStream("/Users/Server/ClientsEncryprtedSecretKey"); //This brings in the secret key File f = new File("/Users/Client/ClientsSecretKey"); FileInputStream in = new FileInputStream(f); byte[]SecKey = new byte[(int)f.length()]; in.read(SecKey); // Encrypt the Secret Key byte[] EncSecKey = rsaCipher.doFinal(SecKey); // Write Secret Key to file fos.write(EncSecKey); fos.close(); } catch (Exception e) { System.out.println(e); } } }
Any help on this issue would be must appreciated, ive been googling the issue for the past 2 days now and have found no satisfactory answers. Cheers in advance