Hi Friends ,
I am trying to generate RSA keys from the txt files.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
public class JcaKeyPair
{
private final File privKeyFile;
private final File pubKeyFile;
public JcaKeyPair(int keySize, String output, String algorithm) throws Exception
{
this.privKeyFile = new File(output + ".pri");
this.pubKeyFile = new File(output + ".pub");
System.out.println("in JcaKeyPair constructor");
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();
FileOutputStream out = new FileOutputStream(privKeyFile);
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 = " + privKeyFile.getCanonicalPath());
System.out.println("Size = " + ky.length);
System.out.println("Format = " + priKey.getFormat());
// System.out.println("toString = " + priKey.toString());
out = new FileOutputStream(pubKeyFile);
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 = " + pubKeyFile.getCanonicalPath());
System.out.println("Size = " + ky.length);
System.out.println("Format = " + pubKey.getFormat());
// System.out.println("toString = " + pubKey.toString());
}
public void encrypt() throws Exception
{
System.out.println("in Encrypt");
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
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("Source.txt");
fis.read(data);
cipher = cf.doFinal(data);
System.out.println("3");
FileOutputStream fos = new FileOutputStream("Data.enc");
fos.write(cipher);
}
public void decrypt() throws Exception
{
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
FileInputStream priKeyStream = new FileInputStream(privKeyFile);
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("Data.enc");
fis.read(cipher);
plain = cf.doFinal(cipher);
FileOutputStream fos = new FileOutputStream("Result.txt");
fos.write(plain);
}
public static void main(String[] a) throws Exception
{
System.out.println("in main");
int keySize = 1024; // 1024 bits, 128 bytes
String output = "RSA";
String algorithm = "RSA"; // RSA, DSA
JcaKeyPair jcaKeyPair = new JcaKeyPair(keySize, output, algorithm);
jcaKeyPair.encrypt();
jcaKeyPair.decrypt();
}
}
But NetBeans wrote:
Exception in thread "main" java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException: null
at sun.security.rsa.RSAKeyFactory.engineGeneratePublic(RSAKeyFactory.java:188)
at java.security.KeyFactory.generatePublic(KeyFactory.java:304)
at JcaKeyPair.encrypt(JcaKeyPair.java:80)
at JcaKeyPair.main(JcaKeyPair.java:130)
Caused by: java.security.InvalidKeyException: IOException: null
at sun.security.x509.X509Key.decode(X509Key.java:380)
at sun.security.x509.X509Key.decode(X509Key.java:386)
at sun.security.rsa.RSAPublicKeyImpl.<init>(RSAPublicKeyImpl.java:66)
at sun.security.rsa.RSAKeyFactory.generatePublic(RSAKeyFactory.java:281)
at sun.security.rsa.RSAKeyFactory.engineGeneratePublic(RSAKeyFactory.java:184)
+... 3 more+
Java Result: 1
Please help me, I do not know what to do ;(