Hi All,
I want to generate RSA keys from the txt files. I wrote the code by refering Google but it is not much fruitful. Please help me to get the keys from txt files.
getKeys(keySize, output, algorithm);
------------------------------------
Create RSA keys and write them to text files.
encrypt();
----------
read Public key from text file (RSA.pub) and encrypt a file.
decrypt();
----------
read a Private key from text file (RSA.pri) and decrypt the file.
Program i am using to write Keys to files are:
import java.io.*;
import java.math.*;
import java.security.*;
import java.security.interfaces.*;
import java.security.spec.KeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.security.spec.PKCS8EncodedKeySpec;
import javax.crypto.Cipher;
public class JcaKeyPair {
public static void main(
String[] a) {
System.out.println("in main");
int keySize = 1024; //1024 bits, 128 bytes
String output = "RSA";
String algorithm = "RSA"; // RSA, DSA
try {
getKeys(keySize, output, algorithm);
//encrypt();
//decrypt();
} catch (Exception e) {
System.out.println("Exception: " + e);
return;
}
}
private static void getKeys(int keySize, String output, String algorithm) throws Exception {
System.out.println("in getKeys method");
KeyPairGenerator kg = KeyPairGenerator.getInstance(algorithm);
kg.initialize(keySize);
System.out.println();
System.out.println("KeyPairGenerator Object Info: ");
System.out.println("Algorithm = " + kg.getAlgorithm());
System.out.println("Provider = " + kg.getProvider());
System.out.println("Key Size = " + keySize);
//System.out.println("toString = " + kg.toString());
KeyPair pair = kg.generateKeyPair();
System.out.println("Key Size = " + keySize);
PrivateKey priKey = pair.getPrivate();
PublicKey pubKey = pair.getPublic();
String fl = output + ".pri";
FileOutputStream out = new FileOutputStream("c:\\encrypt\\" + fl);
byte[] ky = priKey.getEncoded();
out.write(ky);
out.close();
System.out.println();
System.out.println("Private Key Info: ");
System.out.println("Algorithm = " + priKey.getAlgorithm());
System.out.println("Saved File = " + fl);
System.out.println("Size = " + ky.length);
System.out.println("Format = " + priKey.getFormat());
//System.out.println("toString = " + priKey.toString());
fl = output + ".pub";
out = new FileOutputStream("c:\\encrypt\\" + fl);
ky = pubKey.getEncoded();
out.write(ky);
out.close();
System.out.println();
System.out.println("Public Key Info: ");
System.out.println("Algorithm = " + pubKey.getAlgorithm());
System.out.println("Saved File = " + fl);
System.out.println("Size = " + ky.length);
System.out.println("Format = " + pubKey.getFormat());
//System.out.println("toString = " + pubKey.toString());
}
public static void encrypt() {
System.out.println("in Encrypt");
try {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
String pubKeyFile = "c:\\encrypt\\RSA.pub";
System.out.println("1");
FileInputStream pubKeyStream = new FileInputStream(pubKeyFile);
System.out.println("2");
byte[] pubKeyBytes = new byte[128]; //1024 bits
System.out.println("3");
pubKeyStream.read(pubKeyBytes);
System.out.println("4");
pubKeyStream.close();
System.out.println("5");
X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(pubKeyBytes);
System.out.println("6");
PublicKey pubKey = keyFactory.generatePublic(pubKeySpec);
System.out.println("7");
Cipher cf = Cipher.getInstance("RSA/ECB/NoPadding");
System.out.println("8");
cf.init(Cipher.ENCRYPT_MODE, pubKey);
System.out.println("9");
byte[] data = new byte[128];
byte[] cipher = new byte[128];
FileInputStream fis = new FileInputStream("c:\\encrypt\\Source.txt");
fis.read(data);
cipher = cf.doFinal(data);
System.out.println("3");
FileOutputStream fos = new FileOutputStream("c:\\encrypt\\Data.enc");
fos.write(cipher);
} catch(Exception e) {
System.out.println("Exception in encryption " + e);
}
}
public static void decrypt() {
try {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
String priKeyFile = "C:\\encrypt\\RSA.pri";
FileInputStream priKeyStream = new FileInputStream(priKeyFile);
byte[] priKeyBytes = new byte[128];
priKeyStream.read(priKeyBytes);
priKeyStream.close();
PKCS8EncodedKeySpec priKeySpec = new PKCS8EncodedKeySpec(priKeyBytes);
PrivateKey priKey = keyFactory.generatePrivate(priKeySpec);
Cipher cf = Cipher.getInstance("RSA/ECB/NoPadding");
cf.init(Cipher.DECRYPT_MODE, priKey);
byte[] cipher = new byte[128];
byte[] plain = new byte[128];
FileInputStream fis = new FileInputStream("C:\\encrypt\\Data.enc");
fis.read(cipher);
plain = cf.doFinal(cipher);
FileOutputStream fos = new FileOutputStream("C:\\encrypt\\Result.txt");
fos.write(plain);
} catch(Exception e) {
System.out.println("Exception in decryption " + e);
}
}
}
It is giving following error message:
Exception in encryption java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException: null