1Full code:
Import Java.security.Key;
Import Java.security.KeyPair;
Import Java.security.KeyPairGenerator;
Import Java.security.interfaces.RSAPrivateKey;
Import Java.security.interfaces.RSAPublicKey;
Import Java.util.HashMap;
Import Java.util.Map;
Import Sun.misc.BASE64Decoder;
Import Sun.misc.BASE64Encoder;
@SuppressWarnings ("unused")
public class Keys {
public static final String key_algorithm = "RSA";
public static final String signature_algorithm = "Md5withrsa";
private static final String Public_key = "Rsapublickey";
private static final String Private_key = "Rsaprivatekey";
public static void Main (string[] args) {
Map<string, object> KeyMap;
try {
KeyMap = Initkey ();
String PublicKey = Getpublickey (KEYMAP);
System.out.println (PublicKey);
String Privatekey = Getprivatekey (KEYMAP);
System.out.println (Privatekey);
} catch (Exception e) {
E.printstacktrace ();
}
}
public static String Getpublickey (map<string, object> keyMap) throws Exception {
Key key = (key) keymap.get (Public_key);
byte[] PublicKey = key.getencoded ();
Return encryptBASE64 (key.getencoded ());
}
public static String Getprivatekey (map<string, object> keyMap) throws Exception {
Key key = (key) keymap.get (Private_key);
Byte[] Privatekey =key.getencoded ();
Return encryptBASE64 (key.getencoded ());
}
public static byte[] decryptBASE64 (String key) throws Exception {
Return (new Base64decoder ()). Decodebuffer (key);
}
public static String encryptBASE64 (byte[] key) throws Exception {
Return (new Base64encoder ()). Encodebuffer (key);
}
public static map<string, Object> Initkey () throws Exception {
Keypairgenerator Keypairgen = keypairgenerator.getinstance (key_algorithm);
Keypairgen.initialize (1024);
KeyPair KeyPair = Keypairgen.generatekeypair ();
Rsapublickey PublicKey = (rsapublickey) keypair.getpublic ();
Rsaprivatekey Privatekey = (rsaprivatekey) keypair.getprivate ();
map<string, object> keyMap = new hashmap<string, object> (2);
Keymap.put (Public_key, PublicKey);
Keymap.put (Private_key, Privatekey);
return KEYMAP;
}
}