This Program gives you a brief idea, that how you can use RSA algorithm in the scenarios where security is very important. This has been implemented in Java only, so can be applied at various places
import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.security.InvalidKeyException; import java.security.Key; import java.security.KeyFactory; import java.security.NoSuchAlgorithmException; import java.security.PrivateKey; import java.security.PublicKey; import java.security.spec.InvalidKeySpecException; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; public class RSAAlgorithm { public static void main(String[] args) throws Exception { String buffer = "This is a Sample Line to Encrypt"; //Text to be Encrypted System.out.println("Before Encryption : " + buffer + "\n"); BASE64Encoder encoder = new BASE64Encoder(); byte[] enc = encrypt(buffer); String encrypt = encoder.encode(enc); System.out.println("After Encryption : " + encrypt + "\n"); System.out.println("Reading private key"); BASE64Decoder decoder = new BASE64Decoder(); byte[] dec = decoder.decodeBuffer(encrypt); String decry = decrypt(dec); System.out.println("After decrypting : " + decry + "\n"); } /* * @method : getPublicKey * @Input Parameter: filename : name of the file which stores public key * @returns: Public Key Stored in file */ public static PublicKey getPublicKey(String filename) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException { File f = new File(filename); FileInputStream fis = new FileInputStream(f); DataInputStream dis = new DataInputStream(fis); byte[] keyBytes = new byte[(int) f.length()]; dis.readFully(keyBytes); dis.close(); X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes); KeyFactory kf = KeyFactory.getInstance("RSA"); // return Public key return kf.generatePublic(spec); } /* * @method : encrypt * @Input Parameter: buffer : Text to be encrypted * @returns: Encrypted Text in bytes */ public static byte[] encrypt(String buffer) { Cipher rsa; try { Key encryptionKey = getPublicKey("../keys/public_key.der"); rsa = Cipher.getInstance("RSA"); rsa.init(Cipher.ENCRYPT_MODE, encryptionKey); return rsa.doFinal(buffer.getBytes()); } catch (IOException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (InvalidKeySpecException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } return null; } /* * @method : getPublicKey * @Input Parameter: filename : name of the file which stores public key * @returns: Public Key Stored in file */ public static PrivateKey getPrivateKey(String filename) throws Exception { File f = new File(filename); FileInputStream fis = new FileInputStream(f); DataInputStream dis = new DataInputStream(fis); byte[] keyBytes = new byte[(int) f.length()]; dis.readFully(keyBytes); dis.close(); PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory kf = KeyFactory.getInstance("RSA"); // return Private Key return kf.generatePrivate(spec); } /* * @method : decrypt * @Input Parameter: buffer : byte data to be decrypted * @returns: Decrypted Text in String */ private static String decrypt(byte[] buffer) { try { Cipher rsa; rsa = Cipher.getInstance("RSA"); rsa.init( Cipher.DECRYPT_MODE, getPrivateKey("../keys/private_key.der")); byte[] utf8 = rsa.doFinal(buffer); return new String(utf8, "UTF8"); } catch (Exception e) { e.printStackTrace(); } return null; } }