Various cryptographic algorithms in Java

Source: Internet
Author: User
Tags base64 decrypt hmac md5 encryption asymmetric encryption

Java provides us with a wealth of encryption technology, can be basically divided into one-way encryption and asymmetric encryption

1. One-way encryption algorithm

One-way encryption algorithm is mainly used to verify the data transmission process, whether it has been tampered with.

  • BASE64 strictly speaking, it belongs to the encoding format, not the encryption algorithm

  • MD5 (Message Digest algorithm 5, Information Digest algorithm)

  • SHA (Secure Hash algorithm, security hashing algorithm)

  • HMAC (Hash message authentication code, hash messages authentication Code

2. Symmetric and asymmetric encryption algorithmssymmetric and asymmetric encryption algorithms mainly use the form of public and private keys to encrypt data.
  • DES (Data Encryption standard, encryption algorithm)

  • PBE (password-based encryption, password-based authentication)

  • RSA (the name of the algorithm is named after the inventor: Ron Rivest, Adishamir and Leonard Adleman)

  • DH (Diffie-hellman algorithm, key-consistent protocol)

  • DSA (digital Signature algorithm, digitally signed)

  • ECC (Elliptic Curves cryptography, Elliptic curve cipher coding)


For more on the theory, please check it yourself, here is the code to see.
Basic Encryption 
Package Com.test;import Java.security.messagedigest;import Javax.crypto.keygenerator;import Javax.crypto.Mac; Import Javax.crypto.secretkey;import Javax.crypto.spec.secretkeyspec;import Sun.misc.base64decoder;import      sun.misc.base64encoder;/** * @ClassName: Coder * @Description: Cryptographic component * @author: LUCKY * @date: January 4, 2016 1:24:12    */Public abstract class Coder {public static final String Key_sha = "SHA";     public static final String key_md5 = "MD5"; /** * Mac algorithm can choose the following algorithms * * <pre> * HmacMD5 * HmacSHA1 * HmacSHA256 * HmacSHA384 *     HMACSHA512 * </pre> * * public static final String Key_mac = "HmacMD5"; /** * BASE64 Decryption * * @param key * @return * @throws Exception */public static byte[] Decryptbas    E64 (String key) throws Exception {return (new Base64decoder ()). Decodebuffer (key); }/** * BASE64 encryption * * @param key * @return * @throws Exception * * PubLic static String encryptBASE64 (byte[] key) throws Exception {return (new Base64encoder ()). Encodebuffer (key); }/** * MD5 encryption * * @param data * @return * @throws Exception */public static byte[] Encryp        TMD5 (byte[] data) throws Exception {messagedigest MD5 = messagedigest.getinstance (KEY_MD5);         Md5.update (data);     return Md5.digest (); }/** * SHA encryption * * @param data * @return * @throws Exception */public static byte[] Encryp        TSHA (byte[] data) throws Exception {MessageDigest sha = messagedigest.getinstance (Key_sha);         Sha.update (data);     return Sha.digest (); }/** * Initialize HMAC key * * @return * @throws Exception */public static String Initmackey () throws         Exception {Keygenerator keygenerator = keygenerator.getinstance (KEY_MAC);                Secretkey Secretkey = Keygenerator.generatekey (); Return encryptBASE64 (Secretkey.getencodeD ()); }/** * HMAC encryption * * @param data * @param key * @return * @throws Exception */Public St Atic byte[] Encrypthmac (byte[] data, String key) throws Exception {Secretkey Secretkey = new Secretkeyspec (decryp        TBASE64 (key), KEY_MAC);        Mac Mac = Mac.getinstance (Secretkey.getalgorithm ());         Mac.init (Secretkey);     return mac.dofinal (data); }}


RSA Secure Encoding Component 
Package Com.test;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.hashmap;import Java.util.map;import Javax.crypto.Cipher; /** * @ClassName: Rsacoder * @Description: RSA Secure Encoding Component * @author: LUCKY * @date: January 4, 2016 PM 1:25:34 */Public AB    Stract class Rsacoder extends coder {public static final String key_algorithm = "RSA";     public static final String signature_algorithm = "Md5withrsa";    private static final String Public_key = "Rsapublickey";     private static final String Private_key = "Rsaprivatekey";    /** * Digitally sign information with a private key * * @param data * Encrypt Privatekey * @param *        Private key * * @return * @throws Exception * * public static string sign (byte[] data, String Privatekey         ) throws Exception {//Decrypt the private key encoded by base64 byte[] Keybytes = decryptBASE64 (Privatekey);         Constructs the Pkcs8encodedkeyspec object Pkcs8encodedkeyspec pkcs8keyspec = new Pkcs8encodedkeyspec (keybytes);         Key_algorithm the specified cryptographic algorithm keyfactory keyfactory = keyfactory.getinstance (key_algorithm);         Take the 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 (data);    Return encryptBASE64 (Signature.sign ()); /** * Verify Digital Signature * * @param data * Encrypt data * @param publickey * Public key * @pa RAM Sign * Digital Signature * * @return Check Success return true failed return false * @throws Exception * */Public s Tatic Boolean Verify(byte[] Data, string publickey, String sign) throws Exception {//Decrypt the public key base64 encoded by byte[] Keybyte         s = decryptBASE64 (PublicKey);         Constructs the X509encodedkeyspec object X509encodedkeyspec keySpec = new X509encodedkeyspec (keybytes);         Key_algorithm the specified cryptographic algorithm keyfactory keyfactory = keyfactory.getinstance (key_algorithm);         Take the public key object PublicKey PubKey = Keyfactory.generatepublic (KeySpec);        Signature Signature = signature.getinstance (signature_algorithm);        Signature.initverify (PubKey);         Signature.update (data);    Verify that the signature is normal return signature.verify (decryptBASE64 (sign));     /** * Decrypt <br> * Decrypt with private key * * @param data * @param key * @return * @throws Exception        */public static byte[] Decryptbyprivatekey (byte[] data, String key) throws Exception {//decryption key         byte[] keybytes = decryptBASE64 (key); Get the private key Pkcs8encodedkeyspec Pkcs8keyspec = new Pkcs8encodedkeyspec (keybytes);        Keyfactory keyfactory = keyfactory.getinstance (key_algorithm);         Key Privatekey = keyfactory.generateprivate (Pkcs8keyspec);        Decrypt the data Cipher Cipher = Cipher.getinstance (Keyfactory.getalgorithm ());         Cipher.init (Cipher.decrypt_mode, Privatekey);    return cipher.dofinal (data);     /** * Decrypt <br> * Decrypt with private key * * @param data * @param key * @return * @throws Exception        */public static byte[] Decryptbypublickey (byte[] data, String key) throws Exception {//decryption key         byte[] keybytes = decryptBASE64 (key);        Get the public key x509encodedkeyspec X509keyspec = new X509encodedkeyspec (keybytes);        Keyfactory keyfactory = keyfactory.getinstance (key_algorithm);         Key PublicKey = Keyfactory.generatepublic (X509keyspec);        Decrypt the data Cipher Cipher = Cipher.getinstance (Keyfactory.getalgorithm ()); Cipher.init (Cipher.decrypt_mode, PublicKey);    return cipher.dofinal (data);     }/** * Encrypt <br> * Encrypt with public key * * @param data * @param key * @return * @throws Exception        */public static byte[] Encryptbypublickey (byte[] data, String key) throws Exception {//decryption         byte[] keybytes = decryptBASE64 (key);        Get the public key x509encodedkeyspec X509keyspec = new X509encodedkeyspec (keybytes);        Keyfactory keyfactory = keyfactory.getinstance (key_algorithm);         Key PublicKey = Keyfactory.generatepublic (X509keyspec);        Encrypt the data Cipher Cipher = Cipher.getinstance (Keyfactory.getalgorithm ());         Cipher.init (Cipher.encrypt_mode, PublicKey);    return cipher.dofinal (data);     /** * Encrypt <br> * Encrypt with private key * * @param data * @param key * @return * @throws Exception        */public static byte[] Encryptbyprivatekey (byte[] data, String key) throws Exception {//decryption key byte[] Keybytes =DecryptBASE64 (key);        Obtain the private key pkcs8encodedkeyspec Pkcs8keyspec = new Pkcs8encodedkeyspec (keybytes);        Keyfactory keyfactory = keyfactory.getinstance (key_algorithm);         Key Privatekey = keyfactory.generateprivate (Pkcs8keyspec);        Encrypt the data Cipher Cipher = Cipher.getinstance (Keyfactory.getalgorithm ());         Cipher.init (Cipher.encrypt_mode, Privatekey);    return cipher.dofinal (data); /** * Get the private key * * @param keyMap * @return * @throws Exception */public static String GETPR         Ivatekey (map<string, object> keyMap) throws Exception {key key = (key) keymap.get (Private_key);    Return encryptBASE64 (key.getencoded ()); }/** * Gets the public key * * @param keyMap * @return * @throws Exception * * * static String GETPU         Blickey (map<string, object> keyMap) throws Exception {key key = (key) keymap.get (Public_key); Return encryptBASE64 (Key.getencoded ()); }/** * Initialize key * * @return * @throws Exception */public static map<string, object> INITK        EY () throws Exception {Keypairgenerator Keypairgen = keypairgenerator. getinstance (Key_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; }}

For more information, please refer to various encryption algorithmsHave a chat MD5 encryption

Various cryptographic algorithms in Java

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.