RSA
This algorithm appeared in 1978 and is the first algorithm that can be used both for data encryption and for digital signatures. It is easy to understand and operate, and very popular. The name of the algorithm is named after the inventor's name: Ron Rivest, Adishamir and Leonard Adleman.
The main feature of this encryption algorithm is the change of key, above we see des has only one key. The equivalent of only one key, if the key is lost, the data will not be safe. RSA has two keys at the same time, the public key and the private key. Digital signatures are also supported. The meaning of a digital signature is to verify the data that is transmitted over it. Ensure that the data is not modified in the transfer project.
Process Analysis:
- Party a constructs the key pair, publishes the public key to party B and retains the private key.
- Party A uses the private key to encrypt the data, then uses the private key to sign the encrypted data, and sends to party B's signature and encrypted data; Party B uses the public key and signature to verify that the data being decrypted is valid, and if the public key is used to decrypt the data.
- Party B encrypts the data with the public key, sends the encrypted data to party a, and the party obtains the encrypted data and decrypts it through the private key.
Follow the steps above to give the sequence diagram as follows:
The Java code is implemented as follows:
Package Com.somnus.cipher;import Java.io.ioexception;import Java.io.inputstream;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.signature;import Java.security.interfaces.rsaprivatekey;import Java.security.interfaces.rsapublickey;import Java.security.spec.pkcs8encodedkeyspec;import Java.security.spec.x509encodedkeyspec;import java.util.Arrays; Import Java.util.hashmap;import Java.util.map;import Javax.crypto.cipher;import Org.apache.commons.codec.binary.base64;import org.apache.commons.codec.binary.hex;/** Asymmetric Encryption algorithm RSA * @Title: Rsaencrypt.java * @Description: TODO * @author somnus * @date June 5, 2015 PM 2:02:44 * @version V1.0 */public class Rsauti L {public static final String algorithm = "RSA"; public static final String signature_algorithm = "Md5withrsa"; private static final String Public_key = "Rsapublickey"; private static final String Private_key = "Rsaprivatekey"; private static final String Public_key_path = "Public.cer"; private static final String Private_key_path = "Private.key"; private static map<string, object> keymap; static{rsautil rutil = new Rsautil (); try {keymap = Rutil.initkey ();} catch (Exception e) {e.printstacktrace ();} /** * Digitally sign information with the private key * * @param data * Encrypt the database * @param privatekey * Private key * @return * @throws Exception * * public static string sign (string data) throws Exception { Decrypt the private key encoded by base64 byte[] Keybytes = Base64.decodebase64 (Getprivatekey ()); Constructs the Pkcs8encodedkeyspec object Pkcs8encodedkeyspec pkcs8keyspec = new Pkcs8encodedkeyspec (keybytes); Algorithm the specified cryptographic algorithm keyfactory keyfactory = keyfactory.getinstance (algorithm); Take private Key object Privatekey Prikey = Keyfactory.generateprivate (PKCS8KEYSPEC); Generate digital signatures for information with the private key Signature Signature = Signature.getinstance (signature_algorithm); Signature.initsign (Prikey); Signature.update (Hex.decodehex (Data.tochararray ())); Return base64.encodebase64string (Signature.sign ()); /** * Verify Digital Signature * * @param data * Encrypt data * @param publickey * Public key * @param sign * Digital Signature * * @return Verify Success Returns TRUE Failure returns false * @throws Exception * */public static Boolean verify (String data,string sign) throws Exception {//Decrypt the key encoded by Base64 byte [] keybytes = Base64.decodebase64 (Getpublickey ()); Constructs the X509encodedkeyspec object X509encodedkeyspec keySpec = new X509encodedkeyspec (keybytes); Algorithm the specified cryptographic algorithm keyfactory keyfactory = keyfactory.getinstance (algorithm); Take the public key object PublicKey PubKey = Keyfactory.generatepublic (KeySpec); Signature sIgnature = Signature.getinstance (signature_algorithm); Signature.initverify (PubKey); Signature.update (Hex.decodehex (Data.tochararray ())); Verify that the signature is normal return signature.verify (Base64.decodebase64 (sign)); /** * Decrypt <br> * Decrypt with private key * * @param data * @return * @throws Exception * * public static string Decryptbyprivatekey (String data) throws Exception {//decryption key byte[] Keybytes = B Ase64.decodebase64 (Getprivatekey ()); Obtain the private key pkcs8encodedkeyspec Pkcs8keyspec = new Pkcs8encodedkeyspec (keybytes); Keyfactory keyfactory = keyfactory.getinstance (algorithm); Key Privatekey = keyfactory.generateprivate (Pkcs8keyspec); Initialize the Cipher object, set to decryption mode Cipher Cipher = Cipher.getinstance (Keyfactory.getalgorithm ()); Cipher.init (Cipher.decrypt_mode, Privatekey); Perform decryption operation byte[] buff = cipher.dofinal (Hex.decodehex (Data.tochararray ())); System.out.println (arrays.tostring (buff)); return new String (buff); /** * Decrypt <br> * Decrypt with public key * * @param data * @return * @throws Exception * * public static string Decryptbypublickey (String data) throws Exception {//decryption key byte[] Keybytes = Ba Se64.decodebase64 (Getpublickey ()); Get the public key x509encodedkeyspec X509keyspec = new X509encodedkeyspec (keybytes); Keyfactory keyfactory = keyfactory.getinstance (algorithm); Key PublicKey = Keyfactory.generatepublic (X509keyspec); Initialize the Cipher object, set to decryption mode Cipher Cipher = Cipher.getinstance (Keyfactory.getalgorithm ()); Cipher.init (Cipher.decrypt_mode, PublicKey); Perform decryption operation byte[] buff = cipher.dofinal (Hex.decodehex (Data.tochararray ())); System.out.println (arrays.tostring (buff)); return new String (buff); }/** * Encryption <br> * Encrypted with public key * * @param data *@return * @throws Exception */public static string Encryptbypublickey (String data) throws Exception { Decrypt the public key byte[] keybytes = Base64.decodebase64 (Getpublickey ()); Get the public key x509encodedkeyspec X509keyspec = new X509encodedkeyspec (keybytes); Keyfactory keyfactory = keyfactory.getinstance (algorithm); Key PublicKey = Keyfactory.generatepublic (X509keyspec); Instantiates the Cipher object, which is used to complete the actual cryptographic operation Cipher Cipher = Cipher.getinstance (Keyfactory.getalgorithm ()); Cipher.init (Cipher.encrypt_mode, PublicKey); byte[] Buff = cipher.dofinal (Data.getbytes ()); System.out.println (arrays.tostring (buff)); Performs a cryptographic operation. The result of the encryption is usually transmitted with BASE64 encoding return hex.encodehexstring (buff); /** * Encrypt <br> * Encrypt with private key * * @param data * @return * @throws Exception * * public static string Encryptbyprivatekey (String data) throws Exception {//decryption keybyte[] keybytes = Base64.decodebase64 (Getprivatekey ()); Obtain the private key pkcs8encodedkeyspec Pkcs8keyspec = new Pkcs8encodedkeyspec (keybytes); Keyfactory keyfactory = keyfactory.getinstance (algorithm); Key Privatekey = keyfactory.generateprivate (Pkcs8keyspec); Encrypt the data Cipher Cipher = Cipher.getinstance (Keyfactory.getalgorithm ()); Cipher.init (Cipher.encrypt_mode, Privatekey); byte[] Buff = cipher.dofinal (Data.getbytes ()); System.out.println (arrays.tostring (buff)); Performs a cryptographic operation. The result of the encryption is usually transmitted with BASE64 encoding return hex.encodehexstring (buff); /** * Get the private key * * @param keyMap * @return * @throws Exception */public static S Tring Getprivatekey () throws exception{key key = (Key) keymap.get (Private_key); Return base64.encodebase64string (key.getencoded ()); /** * Get Public key * * @param keyMap * @return * @throws Exception */ public static String Getpublickey () throws Exception {key key = (key) keymap.get (Public_key); Return base64.encodebase64string (key.getencoded ()); }/** * Initialize key * * @return * @throws Exception * @throws Exception */Public Map <string, Object> Initkey () throws exception{InputStream in1 = GetClass (). getClassLoader (). Getresourceasstrea M (Public_key_path); InputStream in2 = GetClass (). getClassLoader (). getResourceAsStream (Private_key_path); try {keyfactory keyfactory = keyfactory.getinstance (algorithm); /* Generate public key */byte[] Encodedpubkey = new byte[in1.available ()]; In1.read (Encodedpubkey); X509encodedkeyspec Pubkeyspec = new X509encodedkeyspec (Encodedpubkey); PublicKey PublicKey = Keyfactory.generatepublic (Pubkeyspec); /* Generate private key */byte[] Encodedprikey = new byte[in2.available ()]; In2.read (Encodedprikey); Pkcs8encodedkeyspec Prikeyspec = new Pkcs8encodedkeyspec (Encodedprikey); Privatekey Privatekey = keyfactory.generateprivate (Prikeyspec); /* encapsulated into map*/map<string, object> keyMap = new hashmap<string, object> (); Keymap.put (Public_key, PublicKey); Keymap.put (Private_key, Privatekey); return KEYMAP; } catch (Exception e) {e.printstacktrace (); Throw e; } finally {in1.close (); In2.close (); }}/** * Initialize key 2 * @return * @throws Exception */public static map<string, object> I NitKey2 () throws Exception {Keypairgenerator Keypairgen = keypairgenerator.getinstance (algorithm); Keypairgen.initialize (1024); KeyPair KeyPair = Keypairgen.generatekeypair (); Public key Rsapublickey PublicKey = (rsapublickey) keypair.getpublic (); Private key Rsaprivatekey Privatekey = (Rsaprivatekey) keypair.getprivate (); map<string, object> keyMap = new hashmap<string, object> (2); Keymap.put (Public_key, PublicKey); Keymap.put (Private_key, Privatekey); return KEYMAP; } public static void Main (string[] args) throws Exception {System.out.println ("private key encrypted ———————————————————— Decryption "); String data = "Somnus"; System.out.println ("Original:" + data); String EncryptData = encryptbypublickey (data); SYSTEM.OUT.PRINTLN ("After encryption:" + EncryptData); String decryptdata = Decryptbyprivatekey (EncryptData); System.out.println ("After decryption:" + decryptdata); SYSTEM.OUT.PRINTLN ("Private key encryption ———————————————————— public key decryption"); System.out.println ("Original:" + data); String encryptData2 = encryptbyprivatekey (data); SYSTEM.OUT.PRINTLN ("After encryption:" + ENCRYPTDATA2); String decryptData2 = Decryptbypublickey (ENCRYPTDATA2); System.out.println ("After decryption:" + decryptData2); SYSTEM.OUT.PRINTLN ("Private key signature--public key verification signature"); Generate a signature String sign (ENCRYPTDATA2); System.out.println ("signature: \ r" + sign); Verify that the signature is Boolean status = Verify (Encryptdata2,sign); System.out.println ("Status:" + status); }}
Console output:
Public key cryptography ———————————————————— private Key decryption original: somnus[82, 112, 47,-49,-44, 28, 117, 87,-32,-5,-116, 56,-60, 87, 1,-36, 89, 84, 9 , 17,-66, 64,-115,-114, 72,-86, 79, 63,-15,-35,-14, 40,-50, 16,-105, 73, 80, 7,-48, 75, 114, 64, 80-74 4, 24,-59,-59, 31, 121,-63, 45, 93, 20, 25,-116, 51,-74,-40, 22, 55,-31,-106] after encryption: 52702FCFD41C7557E0FB8C38C457FFDC 5954F711BE408D8E48AA4F3FF1DDF228CE1097495007D04B8EC0504A06B618C5C51F79C12D5D14198C33B6D81637E196[83, 111, 109, 110, 117, 115] After decryption: Somnus private key encryption ———————————————————— public key decrypted original: somnus[69,-38,-21,-84,-56,-1, 50,-33,-46, 11, 124, 37,-10 6, 53, 67, 81,-9, 39,-15,-89, 59,-49, 102, 71, 89,-7, 22, 42, 49,-29, 28, 114,-36,-1, 123,-7, 124 , 12, 76, 61,-117, 118,-54, 99, 99, 47,-118, 28,-119, 83,-5, 124, 122,-3, 109, 45,-38,-31, 98, 99, 107] after encryption: 45dae bacc8ff32dfd20b7c2596354351f727f1a73bcf664759f9162a31e31c72dcff85f97c98da530c4c3d8b76ca63632f8a1c8953fb7c7afd6d2ddae16263 95[83, 111, 109, 110, 117, 115] after decryption:Somnus private key signature--public key verification signature signature: gfluzt88kdtrotl7njoeorygw0lqu9ev4o9j8ovzh9jbhhga/zzdlcajqs1jwkmyaxrut+w2a8v867mzdvqjtw== Status: True
A brief summary, the use of public key encryption, private key decryption, the completion of party B to party a data transfer, through the private key encryption, public key decryption, at the same time through the private key signature, public key verification signature, completed a party to party B data transmission and verification, two times data transmission complete a set of data interaction!
similar to digital signatures, digital envelopes are described in this way:
Digital Envelopes
Digital envelopes are encrypted to ensure that only a specific recipient can read the contents of the letter.
Process:
The message sender uses the symmetric key to encrypt the information, then encrypts the symmetric key with the receiver's public key (this part is called the digital envelope), and then sends it to the receiver with the corresponding private key, the receiver first opens the digital envelope with the appropriate secret, obtains the symmetric key, and then uses the symmetric key to unlock the information.
Java Encryption Technology (quad) asymmetric encryption algorithm RSA