1. generate key
KeyRSA.java
import java.io.*;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
/**
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class KeyRSA {
private KeyPairGenerator kpg = null;
private KeyPair kp = null;
private PublicKey public_key = null;
private PrivateKey private_key = null;
private FileOutputStream public_file_out = null;
private ObjectOutputStream public_object_out = null;
private FileOutputStream private_file_out = null;
private ObjectOutputStream private_object_out = null;
/**
*/
public KeyRSA(int in, String address) throws NoSuchAlgorithmException,FileNotFoundException, IOException
{
kpg = KeyPairGenerator.getInstance("RSA"); //generate pair
kpg.initialize(in); //(512-2048)
kp = kpg.genKeyPair(); //
public_key = kp.getPublic(); //
private_key = kp.getPrivate(); //
//
public_file_out = new FileOutputStream(address + "/public_key.dat");
public_object_out = new ObjectOutputStream(public_file_out);
public_object_out.writeObject(public_key);
//
private_file_out = new FileOutputStream(address + "/private_key.dat");
private_object_out = new ObjectOutputStream(private_file_out);
private_object_out.writeObject(private_key);
}
public static void main(String[] args) {
try {
System.out.println("generated");
new KeyRSA(1024, "./");
}
catch (IOException ex) {
}
catch (NoSuchAlgorithmException ex) {
}
}
}
2. RAS encrypt
RSACoderSimple.java
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
//import sun.misc.BASE64Decoder;
//import sun.misc.BASE64Encoder;
//import org.bouncycastle.util.encoders.Hex;
public class RSACoderSimple {
public static String byte2hex(byte[] b) {
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++) {
stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1)
hs = hs + "0" + stmp;
else
hs = hs + stmp;
}
return hs.toUpperCase();
}
public static void main(String[] args) throws Exception {
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
//numbers
keyPairGen.initialize(1024);
//key pair
KeyPair keyPair = keyPairGen.generateKeyPair();
//public key
PublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
//private key
PrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
//cipher class
Cipher cipher = Cipher.getInstance("RSA");//Cipher.getInstance("RSA/ECB/PKCS1Padding");
byte[] plainText = "adcdef".getBytes();
System.out.println("plain="+byte2hex(plainText));
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] enBytes = cipher.doFinal(plainText);
//decode
System.out.println("after en="+byte2hex(enBytes));
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[]deBytes = cipher.doFinal(enBytes);
System.out.println("after de="+byte2hex(deBytes));
}
}