Java simple realization DES encryption and decryption algorithms

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/liu_Yudong/article/details/88966515

Java simple realization DES encryption and decryption algorithms

DES algorithm introduced

DEC encryption algorithm is a symmetric encryption, i.e., using the specified key, data taken in accordance with the length of the password, the data is divided into blocks, and shift keys complicated, arithmetic operations or data processing operations, a password to be able to only certain solutions open data. Encryption and decryption keys are the same, i.e., the encryption key is equal to the decryption key, the encryption key and the decryption key can be deduced for each other.

Implement the relevant class java

1.Key: Key Java class is encrypted all the parent class
2.SecureRandom: specify the security policy group
3.KeyGenerator: generation algorithm objects
4.BASE64Encoder and BASE64Decoder: by base64-bit conversion and complete byte String of
5.Cipher: This is a focus on class
  Java / Android to use any encryption, you need to use this class Cipher
  Cipher cipher = Cipher.getInstance ( "algorithm name")
  cipher.init (encryption / decryption mode, Key sec), Cipher.DECRYPT_MODE decryption; Cipher.ENCRYPT_MODE encryption
  namely: encryption: cipher.init (Cipher.ENCRYPT_MODE, key); decryption: cipher.init (Cipher.DECRYPT_MODE, key);
  call the doFinal () to complete a single-step method to encrypt or decrypt data; if a plurality of sets of data, the need used updata ();

Code

import java.security.Key;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
//去除警告
@SuppressWarnings("restriction")
public class DESUtil {
	private static Key key;
	//设置密钥key
	private static String KEY_STR = "myKey";
	private static String CHARSETNAME = "UTF-8";
	private static String ALGORITHM = "DES";
	static {
		try {
			//生成DES算法对象
			KeyGenerator generator = KeyGenerator.getInstance(ALGORITHM);
			//运用SHA1安全策略
			SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
			//设置密钥种子
			secureRandom.setSeed(KEY_STR.getBytes());
			//初始化基于SHA1的算法对象
			generator.init(secureRandom);
			//生成密钥对象
			key = generator.generateKey();
			generator = null;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	//加密函数
	public static String getEncryptString(String str) {
		BASE64Encoder base64encoder = new BASE64Encoder();
		try {
			//按utf-8编码
			byte[] bytes = str.getBytes(CHARSETNAME);
			//获取加密对象
			Cipher cipher = Cipher.getInstance(ALGORITHM);
			//初始化加密信息
			cipher.init(Cipher.ENCRYPT_MODE, key);
			//加密
			byte[] doFinal = cipher.doFinal(bytes);
			//byte to encode好的String返回
			return base64encoder.encode(doFinal);
		} catch (Exception e) {
			// TODO: handle exception
			throw new RuntimeException(e);
		}
	}
	//解密函数
	public static String getDecryptString(String str) {
		//接受byte[]并转换成String
		BASE64Decoder base64decoder = new BASE64Decoder();
		try {
			//将String变成byte
			byte[] bytes = base64decoder.decodeBuffer(str);
			//获取解密对象
			Cipher cipher = Cipher.getInstance(ALGORITHM);
			//初始化解密信息
			cipher.init(Cipher.DECRYPT_MODE, key);
			//解密
			byte[] doFinal = cipher.doFinal(bytes);
			//返回解密信息
			return new String(doFinal, CHARSETNAME);
		} catch (Exception e) {
			// TODO: handle exception
			throw new RuntimeException(e);
		}
	}
	//测试
	public static void main(String[] args) {
		System.out.println(getEncryptString("root"));
//		System.out.println(getDecryptString("WnplV/ietfQ="));
	}
}

Guess you like

Origin blog.csdn.net/liu_Yudong/article/details/88966515