21 High-Quality Java Architect High Concurrency High Performance High Availability Distributed Cluster E-commerce Cache Performance Optimization Design Project Tutorials
39 Stages of High-Quality Cloud Computing Big Data Project Practical Video Tutorials
Internet Technology (Java Framework Distributed Cluster) Dry Goods Video Collection
Download 200 Classic Programming Related Books
How Programmers Make High-Quality Resumes [Video + Real Resumes]
Simple Java encryption algorithms include:
-
BASE64 Strictly speaking, it is an encoding format rather than an encryption algorithm
-
MD5 (Message Digest Algorithm 5)
-
SHA (Secure Hash Algorithm)
-
HMAC (Hash Message Authentication Code)
1. BASE64
Base64 is one of the most common encoding methods used on the internet to transmit 8-bit byte code. You can refer to RFC2045 to RFC2049 for detailed MIME specifications. Base64 encoding can be used to pass longer identification information in an HTTP environment. For example, in the Java Persistence system Hibernate, Base64 is used to encode a longer unique identifier (usually a 128-bit UUID) into a string to be used as parameters in HTTP forms and HTTP GET URLs. In other applications, binary data often needs to be encoded into a form suitable for placement in URLs (including hidden form fields). At this time, using Base64 encoding provides unreadability, meaning that the encoded data will not be directly visible to the human eye. (Source: Baidu Encyclopedia)
Java implementation code:
package com.cn.single.encryption;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/*
BASE64 encryption and decryption is bidirectional, it can be reversed.
BASE64Encoder and BASE64Decoder are unofficial JDK implementation classes. Although they can be found and used in JDK, they are not documented in the API.
Classes starting with sun and com.sun in JRE are undocumented, they belong to the core java and javax libraries, and their implementations are mostly platform-dependent,
generally not recommended for use.
BASE64 strictly speaking, belongs to an encoding format rather than an encryption algorithm.
Mainly BASE64Encoder and BASE64Decoder classes, we just need to know to use the corresponding methods.
Also, the number of bytes generated after BASE64 encryption is a multiple of 8, if not enough, it is padded with the '=' sign.
BASE64
According to the definition in RFC2045, Base64 is defined as: The Base64 Content-Transfer-Encoding is designed to represent arbitrary sequences of octets in a form that need not be humanly readable.
Commonly found in emails, http encryption, if you intercept http information, you will find that the username and password fields for login operations are encrypted with BASE64.
*/
public class BASE64 { /**
* BASE64 decryption
*
* @param key
* @return
* @throws Exception
*/
public static byte[] decryptBASE64(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);
}
public static void main(String[] args) {
String str="12345678";
try {
String result1= BASE64.encryptBASE64(str.getBytes());
System.out.println("result1=====Encrypted Data=========="+result1); byte result2[]= BASE64.decryptBASE64(result1);
String str2=new String(result2);
System.out.println("str2========Decrypted Data========"+str2);
} catch (Exception e) {
e.printStackTrace();
}
}
}
2. MD5
MD5 stands for Message-Digest Algorithm 5, used to ensure the integrity of information transmission. It is one of the widely used hashing algorithms in computers (also translated as digest algorithm, hash algorithm), and mainstream programming languages generally have MD5 implementations. The basic principle of hashing algorithms is to compute data (like Chinese characters) into another fixed-length value, with MD5’s predecessors being MD2, MD3, and MD4. Widely used in encryption and decryption technologies, often for file verification. Verification? No matter how large the file is, after MD5 processing, a unique MD5 value can be generated. Just like the current ISO verification, which is all MD5 verification. How to use? Of course, generate the MD5 value from the ISO after processing it through MD5. Generally, friends downloading linux-ISO have seen an MD5 string next to the download link. This is used to verify whether the file is consistent.
Java implementation:
package com.cn.single.encryption;
import java.math.BigInteger;
import java.security.MessageDigest;
/*
MD5 (Message Digest Algorithm 5)
Usually we do not directly use the above MD5 encryption. We usually pass the byte array produced by MD5 to BASE64 for another round of encryption to get the corresponding string.
Digest: Assembly
*/
public class MD5 {
public static final String KEY_MD5 = "MD5";
public static String getResult(String inputStr)
{
System.out.println("=======Data before encryption:"+inputStr);
BigInteger bigInteger=null;
try {
MessageDigest md = MessageDigest.getInstance(KEY_MD5);
byte[] inputData = inputStr.getBytes();
md.update(inputData);
bigInteger = new BigInteger(md.digest());
} catch (Exception e) {e.printStackTrace();}
System.out.println("MD5 encrypted:" + bigInteger.toString(16));
return bigInteger.toString(16);
}
public static void main(String args[]){
try {
String inputStr = "Simple Encryption8888888888888888888";
getResult(inputStr);
} catch (Exception e) {
e.printStackTrace();
}
}
}
MD5 algorithm has the following characteristics:
1. Compression: The MD5 value calculated from data of any length is fixed in length. 2. Easy to compute: It is easy to calculate the MD5 value from the original data. 3. Resistance to modification: Any modification to the original data, even changing just 1 byte, results in a significantly different MD5 value. 4. Weak collision resistance: Given the original data and its MD5 value, it is very difficult to find another data with the same MD5 value (i.e., forged data). 5. Strong collision resistance: It is very difficult to find two different data that have the same MD5 value. The purpose of MD5 is to