1, MD5 (Message Digest Algorithm) encryption algorithm
是一种单向加密算法,只能加密不能解密,示例
/** * MD5 simple encryption * @Param Content encrypted content * @return String */ public static String md5Encrypt(final String content) { MessageDigest md5 = null; try { md5 = MessageDigest.getInstance(ALGORITHM_MD5); } catch (NoSuchAlgorithmException e) { e.printStackTrace (); } // md5.update (text.getBytes ()); // Digest () Returns the last md5 hash value, the return value is 8 bit string. Because md5 hash value is a hex value of 16, 8-bit characters is actually // convert BigInteger 8 will function as a string of hex 16 value, is represented by a string; string of the hash value obtained BigInteger = Digest new new a BigInteger (md5.digest (content.getBytes ())); // 32 bit return digest.toString (16 ); }
2, BASE64 encrypt / decrypt
通常用作对二进制数据进行加密,示例
/** * Base64 encryption * @Param Content content to be encrypted * @return byte[] */ public static byte[] base64Encrypt(final String content) { return Base64.getEncoder().encode(content.getBytes()); } /** * Base64 decryption * @Param encoderContent encrypted content * @return byte[] */ public static byte[] base64Decrypt(final byte[] encoderContent) { return Base64.getDecoder().decode(encoderContent); }
3, DES (Data Encryption Standard) symmetric encryption / decryption
数据加密标准算法,和BASE64最明显的区别就是有一个工作密钥,该密钥既用于加密、也用于解密,并且要求密钥是一个长度至少大于8位的字符串,示例
/** * DES encryption * @param key 秘钥key * @Param Content content to be encrypted * @return byte[] */ public static byte[] DESEncrypt(final String key, final String content) { return processCipher(content.getBytes(), getSecretKey(key), Cipher.ENCRYPT_MODE , ALGORITHM_DES); } /** * DES decryption * @param key 秘钥key * @Param encoderContent encrypted content * @return byte[] */ public static byte[] DESDecrypt(final String key, final byte[] encoderContent) { return processCipher(encoderContent, getSecretKey(key), Cipher.DECRYPT_MODE, ALGORITHM_DES); }
4, RSA asymmetric encryption / decryption
非对称加密算法的典型代表,既能加密、又能解密。和对称加密算法比如DES的明显区别在于用于加密、解密的密钥是不同的。使用RSA算法,只要密钥足够长(一般要求1024bit),加密的信息是不能被破解的。示例
/** * RSA encryption * @Param Content content to be encrypted * @return byte[] */ public static byte[] RSAEncrypt(final String content) { return processCipher(content.getBytes(), keyPair.getPrivate(), Cipher.ENCRYPT_MODE , ALGORITHM_RSA); } /** * RSA decryption * @Param encoderContent encrypted content * @return byte[] */ public static byte[] RSADecrypt(final byte[] encoderContent) { return processCipher(encoderContent, keyPair.getPublic(), Cipher.DECRYPT_MODE, ALGORITHM_RSA); }
5, SHA (Secure Hash Algorithm, Secure Hash Algorithm)
数字签名等密码学应用中重要的工具,被广泛地应用于电子商务等信息安全领域,示例
/** * SHA encryption * @Param Content content to be encrypted * @return String */ public static String SHAEncrypt(final String content) { try { MessageDigest sha = MessageDigest.getInstance(ALGORITHM_SHA); byte[] sha_byte = sha.digest(content.getBytes()); HexValue the StringBuffer = new new the StringBuffer (); for ( byte B: sha_byte) { // in which each byte is converted to a hexadecimal string: byte data type highest bit is the sign bit, and for the operation through 0xff the conversion is a positive integer of type int. Integer.toHexString toHexString = String (B & 0xFF ); hexValue.append(toHexString.length() == 1 ? "0" + toHexString : toHexString); } return hexValue.toString(); // StringBuffer hexValue2 = new StringBuffer(); // for (int i = 0; i < sha_byte.length; i++) { // int val = ((int) sha_byte[i]) & 0xff; // if (val < 16) { // hexValue2.append("0"); // } // hexValue2.append(Integer.toHexString(val)); // } // return hexValue2.toString(); } catch (Exception e) { e.printStackTrace (); } return ""; }
6, HMAC (Hash Message Authentication Code, Hash Message Authentication Code)
使用一个密钥生成一个固定大小的小数据块,即MAC,并将其加入到消息中,然后传输。接收方利用与发送方共享的密钥进行鉴别认证,示例
/** * HMAC encryption * @Param key given keys key * @Param Content content to be encrypted * @return String */ public static byte[] HMACEncrypt(final String key, final String content) { try { SecretKey secretKey = new SecretKeySpec(key.getBytes(), ALGORITHM_MAC); Mac mac = Mac.getInstance(secretKey.getAlgorithm()); //初始化mac mac.init(secretKey); return mac.doFinal(content.getBytes()); } catch (Exception e) { e.printStackTrace (); } return null; }
Test code:
public static void main (String [] args) { // MD5 simple encryption String text = "text I AM" ; System.out.println(EnDecoderUtil.md5Encrypt(text)); // Base64 encryption and decryption, is generally used for encrypting binary data byte [] = base64Encrypt EnDecoderUtil.base64Encrypt ( "123456789" ); String toHexString = HexUtils.toHexString(base64Encrypt); System.out.println(toHexString); byte[] base64Decrypt = EnDecoderUtil.base64Decrypt(base64Encrypt); System.out.println(new String(base64Decrypt)); // the DES symmetric encryption / decryption // required key length of at least 8 characters String key = "123456789" ; // encrypted byte [] = encode_bytes EnDecoderUtil.DESEncrypt (key, "the Hello, the DES" ); System.out.println(Base64.getEncoder().encodeToString(encode_bytes)); //解密 byte[] decode_bytes = EnDecoderUtil.DESDecrypt(key, encode_bytes); System.out.println(new String(decode_bytes)); // the RSA // Data encrypted using a private byte [] = en_byte EnDecoderUtil.RSAEncrypt ( "the Hi, the RSA" ); System.out.println(Base64.getEncoder().encodeToString(en_byte)); // user public key to decrypt byte [] = de_byte EnDecoderUtil.RSADecrypt (en_byte); System.out.println(new String(de_byte)); // server generates a private key and a digital signature from the encrypted data byte [] = sign_byte EnDecoderUtil.getSignature (en_byte); System.out.println(Base64.getEncoder().encodeToString(sign_byte)); // user according to the public key, the encrypted data to verify whether the data has been modified Boolean verify_result = EnDecoderUtil.verifySignature (en_byte, sign_byte); System.out.println(verify_result); //SHA String sha = EnDecoderUtil.SHAEncrypt("Hi, RSA"); System.out.println(sha); //HMAC byte[] mac_bytes = EnDecoderUtil.HMACEncrypt(key, "Hi, HMAC"); System.out.println(HexUtils.toHexString(mac_bytes)); }