JDK RSA algorithm class uses

Source: Internet
Author: User
Tags key string

Package Com.security.rsa;

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;

public class Rsacoder {

public static PublicKey Getpublickey (String key) throws Exception {
Byte[] keybytes;
Keybytes = (new Base64decoder ()). Decodebuffer (key);
X509encodedkeyspec KeySpec = new X509encodedkeyspec (keybytes);
Keyfactory keyfactory = keyfactory.getinstance ("RSA");
PublicKey PublicKey = Keyfactory.generatepublic (KeySpec);
return publickey;
}

public static Privatekey Getprivatekey (String key) throws Exception {
Byte[] keybytes;
Keybytes = (new Base64decoder ()). Decodebuffer (key);
Pkcs8encodedkeyspec KeySpec = new Pkcs8encodedkeyspec (keybytes);
Keyfactory keyfactory = keyfactory.getinstance ("RSA");
Privatekey Privatekey = keyfactory.generateprivate (KeySpec);
return privatekey;
}

/**
* Get 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 key bits
Keypairgen.initialize (1024);
Key pair
KeyPair KeyPair = Keypairgen.generatekeypair ();
Public
PublicKey PublicKey = (rsapublickey) keypair.getpublic ();
Private
Privatekey Privatekey = (rsaprivatekey) keypair.getprivate ();
String publickeystring = getkeystring (PublicKey);
System.out.println ("public:\n" + publickeystring);
String privatekeystring = getkeystring (Privatekey);
System.out.println ("private:\n" + privatekeystring);
Add Decryption class
Cipher Cipher = cipher.getinstance ("RSA");//cipher.getinstance ("rsa/ecb/pkcs1padding");
Plaintext
Byte[] plaintext = "91686280721609". GetBytes ();
Encryption
Cipher.init (Cipher.encrypt_mode, PublicKey);
byte[] Enbytes = cipher.dofinal (plaintext);
string S1 = new String (enbytes);
System.out.println ("Ciphertext after encryption:" + S1);
Get the key through 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);
}

}

JDK RSA algorithm class uses

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.