Java RSA encryption and decryption

Source: Internet
Author: User

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 );

}

}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.