Java MD5 Encryption and RSA encryption

Source: Internet
Author: User
Tags base64 decrypt md5 encryption

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

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.