欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

各种加密 解密 Java实现代码

程序员文章站 2022-06-21 20:38:28
...
AES
package com.sf.core.sgs.utils;

import java.security.Key;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Hex;

public class AESCoder {
	
	public static final String ALGORITHM = "AES";
	
	public static final String CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";
	
	public static byte[] initKey(){
		try {
			KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
			keyGenerator.init(128);
			
			return keyGenerator.generateKey().getEncoded();
		} catch (NoSuchAlgorithmException e) {
			throw new RuntimeException(e.getMessage(), e);
		}
	}
	
	
	private static Key toKey(byte[] key){
		return new SecretKeySpec(key, ALGORITHM);
	}
	
	public static String encrypt(String data, String k) throws Exception {
		Key key = toKey(Hex.decodeHex(k.toCharArray()));
		Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
		//初始化, 设置为加密模式
		cipher.init(Cipher.ENCRYPT_MODE, key);
		byte[] enBytes = cipher.doFinal(data.getBytes());
		return String.valueOf(Hex.encodeHex(enBytes));
	}
	
	public static String decrypt(String data, String k) throws Exception{
		Key key = toKey(Hex.decodeHex(k.toCharArray()));
		Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
		//初始化, 设置为解密模式
		cipher.init(Cipher.DECRYPT_MODE, key);
		byte[] bytes = cipher.doFinal(Hex.decodeHex(data.toCharArray()));
		return new String(bytes);
	}
	
	public static void main(String[] args) throws Exception{
		String inputStr = "123456";
		byte[] k = AESCoder.initKey();
		
		String keyStr = String.valueOf(Hex.encodeHex(k));
		System.out.println("密钥: " + keyStr);
		String en = AESCoder.encrypt(inputStr, keyStr);
		System.out.println("密文: " + en);
		
		//解密
		String de = AESCoder.decrypt(en, keyStr);
		System.out.println("解密后明文: " + new String(de));
	}
}




HmacMD5
public class MACEncoder {
	
	public static final String ALGORITHM = "HmacMD5";
	
	public static byte[] initHmacMD5Key() throws Exception{
		KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
		SecretKey secretKey = keyGenerator.generateKey();
		return secretKey.getEncoded();
	}
	
	public static byte[] encodeHmacMD5(byte[] data, byte[] key) throws Exception{
		SecretKey secretKey = new SecretKeySpec(key, ALGORITHM);
		Mac mac = Mac.getInstance(secretKey.getAlgorithm());
		mac.init(secretKey);
		return mac.doFinal(data);
	}
	
	
	public static void main(String[] args) throws Exception{
		String id = "13141283947192347174";
		String key = "11";
		long t1 = System.currentTimeMillis();
		System.out.println(encodeHmacMD5(id, key));
		System.out.println(encodeHmacMD5(id, key));
		System.out.println(encodeHmacMD5(id, key));
		System.out.println(encodeHmacMD5(id, key));
		System.out.println(System.currentTimeMillis() - t1);
		//System.out.println(Hex.encodeHex(initHmacMD5Key()));
	}
	
	public static String encodeHmacMD5(String data, String key) throws Exception{
		
		SecretKey secretKey = new SecretKeySpec(Hex.decodeHex(key.toCharArray()), "HmacMD5");
		Mac mac = Mac.getInstance(secretKey.getAlgorithm());
		mac.init(secretKey);
		return String.valueOf(Hex.encodeHex(mac.doFinal(data.getBytes())));
	}
	
}


MD5
public class MD5Encoder2 {
	
	public static void main(String[] args) {
		String str = "12345";
		try {
			MessageDigest digest = MessageDigest.getInstance("MD5");
			byte[] bytes = digest.digest(str.getBytes());
			System.out.println(Hex.encodeHex(bytes));
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
	
	
	
}



RSA
public class RSAEncoder {
	
	public static final String ALGORITHM = "rsa";
	
	public static final String PUBLIC_KEY = "RSAPublicKey";
	
	public static final String PRIVATE_KEY = "RSAPrivateKey";
	
	public static final int KEY_SIZE = 512;
	
	/**
	 * 私钥解密
	 * @param data
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static byte[] decryptByPrivateKey(byte[] data, byte[] key) throws Exception{
		PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(key);
		KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
		PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
		
		Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
		cipher.init(Cipher.DECRYPT_MODE, privateKey);
		return cipher.doFinal(data);
	}
	
	/**
	 * 公钥加密
	 * @param data
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static byte[] encryptByPublicKey(byte[] data, byte[] key) throws Exception{
		X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(key);
		KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
		PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
		Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
		cipher.init(Cipher.ENCRYPT_MODE, publicKey);
		return cipher.doFinal(data);
	}
	
	/**
	 * 公钥加密
	 * @param data
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static byte[] encryptByPublicKey(byte[] data, PublicKey publicKey) throws Exception {
		KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
		Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
		cipher.init(Cipher.ENCRYPT_MODE, publicKey);
		return cipher.doFinal(data);
	}
	
	/**
	 * 公钥解密
	 * @param data
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static byte[] decryptByPublicKey(byte[] data, byte[] key) throws Exception{
		X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(key);
		KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
		PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
		Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
		cipher.init(Cipher.DECRYPT_MODE, publicKey);
		return cipher.doFinal(data);
	}
	
	/**
	 * 私钥加密
	 * @param data
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static byte[] encryptByPrivateKey(byte[] data, byte[] key) throws Exception{
		PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(key);
		KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
		PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
		
		Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
		cipher.init(Cipher.ENCRYPT_MODE, privateKey);
		return cipher.doFinal(data);
	}
	
	/**
	 * 初始化密钥
	 * @return
	 * @throws Exception
	 */
	public static KeyPair initKey() throws Exception{
		KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM);
		KeyPair keyPair = keyPairGenerator.generateKeyPair();
		return keyPair;
	}
	
	public static void main(String[] args) throws Exception{
		KeyPair keyPair = initKey();//密钥对
		String data = "明文数据12345";
		
		System.out.println("私钥: " + String.valueOf(Hex.encodeHex(keyPair.getPrivate().getEncoded())));
		System.out.println("公钥: " + String.valueOf(Hex.encodeHex(keyPair.getPublic().getEncoded())));
		
		byte[] byte1 = encryptByPublicKey(data.getBytes(), keyPair.getPublic().getEncoded());
		System.out.println("公钥加密后: " + String.valueOf(Hex.encodeHex(byte1)));
		
		byte[] byte2 = decryptByPrivateKey(byte1, keyPair.getPrivate().getEncoded());
		System.out.println("私钥解密后: " + new String(byte2));
		
		byte[] byte3 = encryptByPrivateKey(data.getBytes(), keyPair.getPrivate().getEncoded());
		System.out.println("私钥加密后: " + String.valueOf(Hex.encodeHex(byte3)));
		
		byte[] byte4 = decryptByPublicKey(byte3, keyPair.getPublic().getEncoded());
		System.out.println("公钥解密后: " + new String(byte4));
	}
	
}







相关标签: MD5 RSA AES