From: http://java.chinaitlab.com/advance/798576_2.html
The code is not tested.
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. rsw.vatekey;
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;
Public class rsacoder {
/**
* Obtain the public key.
* @ Param key string (base64 encoded)
* @ Throws exception
*/
Public static publickey getpublickey (string key) throws exception {
Byte [] keybytes;
Keybytes = (New base64decoder (). decodebuffer (key );
X509encodedkeyspec keyspec = new x509encodedkeyspec (keybytes );
Keyfactory = keyfactory. getinstance ("RSA ");
Publickey = keyfactory. generatepublic (keyspec );
Return publickey;
}
/**
* Obtain the private key.
* @ Param key string (base64 encoded)
* @ Throws exception
*/
Public static privatekey getprivatekey (string key) throws exception {
Byte [] keybytes;
Keybytes = (New base64decoder (). decodebuffer (key );
Pkcs8encodedkeyspec keyspec = new pkcs8encodedkeyspec (keybytes );
Keyfactory = keyfactory. getinstance ("RSA ");
Privatekey = keyfactory. generateprivate (keyspec );
Return privatekey;
}
/**
* Obtain the key string (base64 encoded)
* @ Return
*/
Public static string getkeystring (Key key) throws exception {
Byte [] keybytes = key. getencoded ();
String S = (New base64encoder (). encode (keybytes );
Return S;
}
Public static void main (string [] ARGs) throws exception {
Keypairgenerator keypairgen = keypairgenerator. getinstance ("RSA ");
// Number of keys
Keypairgen. initialize (1024 );
// Key pair
Keypair = keypairgen. generatekeypair ();
// Public Key
Publickey = (rsapublickey) keypair. getpublic ();
// Private Key
Privatekey = (rsw.vatekey) keypair. getprivate ();
String publickeystring = getkeystring (publickey );
System. Out. println ("public: \ n" + publickeystring );
String privatekeystring = getkeystring (privatekey );
System. Out. println ("Private: \ n" + privatekeystring );
// Encryption/Decryption class
Cipher cipher = cipher. getinstance ("RSA"); // cipher. getinstance ("RSA/ECB/pkcs1padding ");
// Plaintext
Byte [] plaintext = "we are all very good! Email: @ Sina.com ". getbytes ();
// Encryption
Cipher. INIT (Cipher. encrypt_mode, publickey );
Byte [] enbytes = cipher. dofinal (plaintext );
// Obtain the key using the key string
Publickey = getpublickey (publickeystring );
Privatekey = getprivatekey (privatekeystring );
// Decrypt
Cipher. INIT (Cipher. decrypt_mode, privatekey );
Byte [] debytes = cipher. dofinal (enbytes );
Publickeystring = getkeystring (publickey );
System. Out. println ("public: \ n" + publickeystring );
Privatekeystring = getkeystring (privatekey );
System. Out. println ("Private: \ n" + privatekeystring );
String S = new string (debytes );
System. Out. println (s );
}
}