Java--Simple RSA algorithm encryption and decryption

Source: Internet
Author: User
Tags decrypt

Encryption protocol

public class Rsa_asc {
/** specifies that the encryption algorithm is Desede */
private static String algorithm = "RSA";
/** Specify the size of key */
private static int KEYSIZE = 1024;
/** Specify the public key to store the file */
private static String Public_key_file = "qhtec2017123";
/** Specify the private key to store the file */
private static String Private_key_file = "qhtec2017_java_08";

/**
* Generate key Pair
*/
private static void Generatekeypair () throws Exception {
The/** RSA algorithm requires a trustworthy random number source */
securerandom sr = new SecureRandom ();
/** creating a Keypairgenerator object for the RSA algorithm */
Keypairgenerator KPG = keypairgenerator.getinstance (algorithm);
/** initializes the Keypairgenerator object using the above random data source */
Kpg.initialize (KEYSIZE, SR);
/** Generate key Pair */
KeyPair KP = Kpg.generatekeypair ();
/** get the Public key */
Key PublicKey = Kp.getpublic ();
/** get the private key */
Key Privatekey = Kp.getprivate ();
/** writes the generated key to the file using the object stream */
ObjectOutputStream oos1 = new ObjectOutputStream (New FileOutputStream (
Public_key_file));
ObjectOutputStream Oos2 = new ObjectOutputStream (New FileOutputStream (
Private_key_file));
Oos1.writeobject (PublicKey);
Oos2.writeobject (Privatekey);
/** empty the cache, close the file output stream */
Oos1.close ();
Oos2.close ();
}

/**
* Encryption method Source: source data
*/
public static string Encrypt (string source) throws Exception {
Generatekeypair ();
/** read out the public key object in the file */
ObjectInputStream ois = new ObjectInputStream (New FileInputStream (
Public_key_file));
Key key = (key) ois.readobject ();
Ois.close ();
/** gets the cipher object to implement RSA encryption of the source data */
Cipher Cipher = cipher.getinstance (algorithm);
Cipher.init (Cipher.encrypt_mode, key);
Byte[] B = source.getbytes ();
/** Performing Cryptographic operations */
Byte[] B1 = cipher.dofinal (b);
Base64encoder encoder = new Base64encoder ();
Return Encoder.encode (B1);
}


Currently in use
public static void Main (string[] args) throws Exception {
String Source = "test#_#1511842260";//String to encrypt
String cryptograph = Encrypt (source);//Ciphertext generated
System.out.println (cryptograph);

}
}

Decryption protocol

public class Rsa_desc {
/** specifies that the encryption algorithm is Desede */
private static String algorithm = "RSA";
/** Specify the private key to store the file (encrypted generated key file storage path) */
private static String Private_key_file = System.getproperty ("web.root") + "/download/qhtec2017_java_08";




/**
* Decryption algorithm cryptograph: Ciphertext
*/
public static string decrypt (string cryptograph) {
try {
/** read out the private key object in the file */
ObjectInputStream ois = new ObjectInputStream (New FileInputStream (
Private_key_file));
Key key = (key) ois.readobject ();
/** gets the cipher object to decrypt the data encrypted with the public key by RSA */
Cipher Cipher = cipher.getinstance (algorithm);
Cipher.init (Cipher.decrypt_mode, key);
Base64decoder decoder = new Base64decoder ();
Byte[] B1 = Decoder.decodebuffer (cryptograph);
/** Perform decryption operation */
Byte[] B = cipher.dofinal (B1);
return new String (b);
} catch (Exception e) {
return "123";
}

}

public static void Main (string[] args) throws Exception {

String target = Decrypt ("orl1nilkf/tr0z+5k/ Fqk7vovtwlwtyxlc2cmcx9tpfbzbegh2u9h0txwkgzzvteibjxfs7rkojlizwewnw3z4bnegq12sbcrd+ R6v097uieoml3lc1chxy8s5hhrdy05ae8j9gl4qncvsgrxwcalxi4i1nydnu82eztzstpiyy= ");//Decrypt the text
SYSTEM.OUT.PRINTLN (target);
}
}

Java--Simple RSA algorithm encryption and decryption

Related Article

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.