Difference:
MD5 Encryption:
Encrypt from the original string into another string
Decryption requires the original encrypted string to be re-encrypted two times encryption results are consistent
T=RSA Encryption:
Generate a key pair (public key + private key) from the original string when encrypting
Decryption by the public and private keys to decrypt the original string to decrypt the consistency of the comparison
Personal Opinion:
RSA encryption slightly more than MD5 encryption a little bit
But everything is good or bad MD5 encryption execution efficiency is slower than RSA
Not much nonsense to say on chestnuts:
MD5 Encryption:
PackageCn.news.util;Importjava.security.MessageDigest;/** * * @author: Cat on the room * * @time: May 14, 2018 8:04:44 * @ Blog Address:https://www.cnblogs.com/lsy131479/ * */ Public classMD5 {Private Staticstring MD (string s) {Try{messagedigest MD= Messagedigest.getinstance ("MD5"); Md.update (S.getbytes ("Utf-8")); byte[] bytes = Md.digest (s.getbytes ("Utf-8")); returnTohex (bytes); } Catch(Exception e) {Throw NewRuntimeException (e); } } Private StaticString Tohex (byte[] bytes) { Final Char[] hex_digits = "0123456789ABCDEF". ToCharArray (); StringBuilder ret=NewStringBuilder (Bytes.length * 2); for(inti = 0; i < bytes.length; i++) {ret.append (hex_digits[(bytes[i)>> 4) & 0x0f]); Ret.append (Hex_digits[bytes[i]& 0x0f]); } returnret.tostring (); } Public Static voidMain (string[] args) {System.out.println (MD ("Hello word")); }}
Results:
RSA Encryption and decryption:
PackageCn.news.util;ImportJava.security.KeyPair;ImportJava.security.KeyPairGenerator;ImportJava.security.PrivateKey;ImportJava.security.PublicKey;Importjava.util.Base64;ImportJavax.crypto.Cipher;/** * * @author: Cat on the room * * @time: May 14, 2018 7:56:12 * @ Blog Address:https://www.cnblogs.com/lsy131479/ * */ Public classRSA { Public StaticString data = "Hello World"; Public Static voidMain (string[] args)throwsException {//TODO auto-generated Method StubKeyPair KeyPair= Genkeypair (1024); //get the public key and print it in base64 formatPublicKey PublicKey =keypair.getpublic (); System.out.println ("Public key:" +NewString (Base64.getencoder (). Encode (publickey.getencoded ())); //get the private key and print it in base64 formatPrivatekey Privatekey =keypair.getprivate (); System.out.println ("Private key:" +NewString (Base64.getencoder (). Encode (privatekey.getencoded ())); //Public Key Cryptography byte[] Encryptedbytes =Encrypt (Data.getbytes (), PublicKey); System.out.println ("After encryption:" +NewString (encryptedbytes)); //private Key Decryption byte[] Decryptedbytes =Decrypt (encryptedbytes, Privatekey); System.out.println ("After decryption:" +NewString (decryptedbytes)); } //generate key Pair Public StaticKeyPair Genkeypair (intKeylength)throwsException {keypairgenerator keypairgenerator= Keypairgenerator.getinstance ("RSA"); Keypairgenerator.initialize (1024); returnKeypairgenerator.generatekeypair (); } //Public Key Cryptography Public Static byte[] Encrypt (byte[] content, PublicKey publickey)throwsException {Cipher Cipher= Cipher.getinstance ("RSA");//java Default "RSA" = "rsa/ecb/pkcs1padding"Cipher.init (Cipher.encrypt_mode, PublicKey); returncipher.dofinal (content); } //private Key Decryption Public Static byte[] Decrypt (byte[] content, Privatekey Privatekey)throwsException {Cipher Cipher= Cipher.getinstance ("RSA"); Cipher.init (Cipher.decrypt_mode, Privatekey); returncipher.dofinal (content); }}
Operation Result:
Java MD5 Encryption and RSA encryption