Java Write asymmetric encryption, decryption, public key encryption, private key decryption, Rsa,rsa

Source: Internet
Author: User
Tags base64 decrypt generator asymmetric encryption

Asymmetric encryption has been rated as the encryption standard, mainly includes (public key encryption private key decryption, or private key encryption public key decryption) This article mainly explains how to generate the public and private keys and string encryption and string decryption in Java

If you need code copy as follows


Import Java.security.KeyPair;
Import Java.security.KeyPairGenerator;
Import java.security.NoSuchAlgorithmException;
Import Java.security.PrivateKey;
Import Java.security.PublicKey;
Import Java.util.HashMap;
Import Java.util.Map;

Import Javax.crypto.Cipher;
Import javax.crypto.NoSuchPaddingException;

Import Sun.applet.resources.MsgAppletViewer;
Import Sun.misc.BASE64Decoder;
Import Sun.misc.BASE64Encoder;

public class KeyRSA1 {
Key Pair Generator
Private Keypairgenerator Keypairgenerator;
Key pair contains (public key he private key)
Private KeyPair KeyPair;
Public Key Object
Private PublicKey PublicKey;
Private Key Object
Private Privatekey Privatekey;
Cryptographic decryption Common types
Private String KeyType = "RSA";
/*** constructor function
* @param keySize Key length range (512-16384) recommended 1024 or 2048 do not take too long to reduce performance
* @throws exception*/
Public KeyRSA1 (int keySize) throws Exception {
1 Creating the Keypairgengerator Key Generator object
Keypairgenerator = Keypairgenerator.getinstance (KeyType);
2 specifying the length of the key generator
Keypairgenerator.initialize (keySize);
3 Create keypair based on key generator to get key pair inside contains (public key and private key)
KeyPair = Keypairgenerator.genkeypair ();
4 Creating a public key based on a key pair
PublicKey = Keypair.getpublic ();
5 Creating a private key based on a key pair
Privatekey = Keypair.getprivate ();
}
Use public?? Encryption
public string Encrypt (String str) throws Exception {
Create key class KeyType for RSA
Cipher Cipher = cipher.getinstance (KeyType);
Specifies that the key class is encrypted cipher class Encrypt_mode constant is encrypted publickey public key
Cipher.init (Cipher.encrypt_mode, PublicKey);
Public key encryption processing
byte[] bytes = cipher.dofinal (Str.getbytes ());
Base64encoder encoder=new Base64encoder ();
Returns the string after Base64 encoded encryption
Return Encoder.encode (bytes);
}
Use private?? Decrypt
public string Decrypt (string str) throws Exception {
Create key class KeyType for RSA
Cipher Cipher = cipher.getinstance (KeyType);
Specifies that the key is the decryption type of the Cipher Decrypt_mode decryption Privatekey private key
Cipher.init (Cipher.decrypt_mode, Privatekey);
Base64decoder decoder=new Base64decoder ();
For Base64 decryption
Byte[] Bytes1=decoder.decodebuffer (str);
Private key decryption
byte[] bytes = cipher.dofinal (bytes1);
The result is a byte array that returns the decrypted string to the byte array programming string
return new String (bytes);
}



}

The test class code copy is as follows


public static void Main (string[] args) {
try {

KeyRSA1 key = new KeyRSA1 (1024);
String str = "Data encrypted content";
System.out.println ("The content to be encrypted is = = =" + str);
String Encryptresult = Key.encrypt (str);
SYSTEM.OUT.PRINTLN ("Secret text after encryption is = = = =" + Encryptresult);
String Decryptresult = Key.decrypt (Encryptresult);
SYSTEM.OUT.PRINTLN ("decrypted data is = = = =" + Decryptresult);

} catch (Exception e) {
E.printstacktrace ();
}

}

Java Write asymmetric encryption, decryption, public key encryption, private key decryption, Rsa,rsa

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.