加密解密,添加密码秘钥

package com.sjzx.common.util;
 
import java.lang.reflect.Method;
import java.security.Key;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
 
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
 
/**
 * Java加密解密工具.3DES SHA1 MD5 BASE64编码 AES加密 Title: EnDecryptUtil.java
 * Description:
 * 
 * @author xwp
 * @date 2017年4月10日下午1:20:19
 * @version 1.0
 */
public class EnDecryptUtil {
	// 无需创建对象
	private EnDecryptUtil() {
		
	}
 
	public static void main(String[] args) {
//		byte[] arr1 = new byte[] { 1, 2, 3, 4, 5, 6, 7, 22 };
//		System.out.println("SHA1 加密 : " + SHA1Bit(arr1));
//		System.out.println("SHA1 解密 : " + SHA1("arr1"));
//		System.out.println("MD5 加密 : " + MD5Bit(arr1));
//		System.out.println("MD5 加密 : " + MD5("arr1"));// 54f04b52a75382a157f69457810c41bd
//		System.out.println("BASE64 加密 : " + encodeBASE64("arr1"));
//		System.out.println("BASE64 解密 : " + decodeBASE64(encodeBASE64("arr1")));
//		System.out.println("3DES 加密 : " + d3esEncode("arr1"));
//		System.out.println("d3esDecode(decodeBASE64(encodeBASE64(d3esEncode('arr1')))) = "
//				+ d3esDecode(decodeBASE64(encodeBASE64(d3esEncode("arr1")))));
//		System.out.println("3DES 解密 : " + d3esDecode(d3esEncode("arr1")));
//		System.out.println("AES 加密 : " + encryptBitAES(arr1, "arr1"));
//		System.out.println("AES 解密 : " + decryptBitAES(encryptBitAES(arr1, "arr1"), "arr1"));
		String password = EnDecryptUtil.encodeBASE64(EnDecryptUtil.d3esEncode("111111"));
		System.out.println(password);
 
	}
 
	/**
	 * SHA1加密Bit数据
	 * 
	 * @param source
	 *            byte数组
	 * @return 加密后的byte数组
	 */
	public static byte[] SHA1Bit(byte[] source) {
		try {
			MessageDigest sha1Digest = MessageDigest.getInstance("SHA-1");
			sha1Digest.update(source);
			byte targetDigest[] = sha1Digest.digest();
			return targetDigest;
		} catch (NoSuchAlgorithmException e) {
			throw new RuntimeException(e);
		}
	}
 
	/**
	 * SHA1加密字符串数据
	 * 
	 * @param source
	 *            要加密的字符串
	 * @return 加密后的字符串
	 */
	public static String SHA1(String source) {
		return byte2HexStr(SHA1Bit(source.getBytes()));
	}
 
	/**
	 * MD5加密Bit数据
	 * 
	 * @param source
	 *            byte数组
	 * @return 加密后的byte数组
	 */
	public static byte[] MD5Bit(byte[] source) {
		try {
			// 获得MD5摘要算法的 MessageDigest对象
			MessageDigest md5Digest = MessageDigest.getInstance("MD5");
			// 使用指定的字节更新摘要
			md5Digest.update(source);
			// 获得密文
			return md5Digest.digest();
		} catch (NoSuchAlgorithmException e) {
			throw new RuntimeException(e);
		}
	}
 
	/**
	 * MD5加密字符串,32位长
	 * 
	 * @param source
	 *            要加密的内容
	 * @return 加密后的内容
	 */
	public static String MD5(String source) {
		return byte2HexStr(MD5Bit(source.getBytes()));
	}
 
	/**
	 * BASE64编码
	 * 
	 * @param source
	 *            要编码的字符串
	 * @return 编码过的字符串
	 */
	public static String encodeBASE64(String source) {
		Class<?> clazz = null;
		Method encodeMethod = null;
		try {// 优先使用第三方库
			clazz = Class.forName("org.apache.commons.codec.binary.Base64");
			encodeMethod = clazz.getMethod("encodeBase64", byte[].class);
			System.out.println("encodeBASE64-->" + clazz);
			System.out.println("encodeMethod-->" + encodeMethod);
			// 反射方法 静态方法执行无需对象
			return new String((byte[]) encodeMethod.invoke(null, source.getBytes()));
		} catch (ClassNotFoundException e) {
			String vm = System.getProperty("java.vm.name");
			System.out.println(vm);
			try {
				if ("Dalvik".equals(vm)) {// Android
					clazz = Class.forName("android.util.Base64");
					// byte[] Base64.encode(byte[] input,int flags)
					encodeMethod = clazz.getMethod("encode", byte[].class, int.class);
					System.out.println("encodeBASE64-->" + clazz);
					System.out.println("encodeMethod-->" + encodeMethod);
					return new String((byte[]) encodeMethod.invoke(null, source.getBytes(), 0));
				} else {// JavaSE/JavaEE
					clazz = Class.forName("sun.misc.BASE64Encoder");
					encodeMethod = clazz.getMethod("encode", byte[].class);
					System.out.println("encodeBASE64-->" + clazz);
					System.out.println("encodeMethod-->" + encodeMethod);
					return (String) encodeMethod.invoke(clazz.newInstance(), source.getBytes());
				}
			} catch (ClassNotFoundException e1) {
				return null;
			} catch (Exception e1) {
				return null;
			}
		} catch (Exception e) {
			return null;
		}
	}
 
	/**
	 * BASE64解码
	 * 
	 * @param encodeSource
	 *            编码过的字符串
	 * @return 编码前的字符串
	 */
	public static String decodeBASE64(String encodeSource) {
		Class<?> clazz = null;
		Method decodeMethod = null;
 
		try {// 优先使用第三方库
			clazz = Class.forName("org.apache.commons.codec.binary.Base64");
			decodeMethod = clazz.getMethod("decodeBase64", byte[].class);
			System.out.println("decodeBASE64-->" + clazz);
			System.out.println("decodeMethod-->" + decodeMethod);
			// 反射方法 静态方法执行无需对象
			return new String((byte[]) decodeMethod.invoke(null, encodeSource.getBytes()));
		} catch (ClassNotFoundException e) {
			String vm = System.getProperty("java.vm.name");
			System.out.println(vm);
			try {
				if ("Dalvik".equals(vm)) {// Android
					clazz = Class.forName("android.util.Base64");
					// byte[] Base64.decode(byte[] input, int flags)
					decodeMethod = clazz.getMethod("decode", byte[].class, int.class);
					System.out.println("decodeBASE64-->" + clazz);
					System.out.println("decodeMethod-->" + decodeMethod);
					return new String((byte[]) decodeMethod.invoke(null, encodeSource.getBytes(), 0));
				} else { // JavaSE/JavaEE
					clazz = Class.forName("sun.misc.BASE64Decoder");
					decodeMethod = clazz.getMethod("decodeBuffer", String.class);
					System.out.println("decodeBASE64-->" + clazz);
					System.out.println("decodeMethod-->" + decodeMethod);
					return new String((byte[]) decodeMethod.invoke(clazz.newInstance(), encodeSource));
				}
			} catch (ClassNotFoundException e1) {
				return null;
			} catch (Exception e1) {
				return null;
			}
		} catch (Exception e) {
			return null;
		}
	}
 
	/**
	 * AES加密
	 * 
	 * @param content
	 *            待加密的内容
	 * @param password
	 *            加密密码
	 * @return
	 */
	public static byte[] encryptBitAES(byte[] content, String password) {
		try {
			Cipher encryptCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");// 创建密码器
			encryptCipher.init(Cipher.ENCRYPT_MODE, getKey(password));// 初始化
			byte[] result = encryptCipher.doFinal(content);
			return result; // 加密
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		// return null;
	}
 
	/**
	 * AES解密
	 * 
	 * @param content
	 *            待解密内容
	 * @param password
	 *            解密密钥
	 * @return
	 */
	public static byte[] decryptBitAES(byte[] content, String password) {
		try {
			Cipher decryptCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");// 创建密码器
			decryptCipher.init(Cipher.DECRYPT_MODE, getKey(password));// 初始化
			byte[] result = decryptCipher.doFinal(content);
			return result; // 加密结果
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		// return null;
	}
 
	/**
	 * AES字符串加密
	 * 
	 * @param content
	 *            待加密的内容
	 * @param password
	 *            加密密码
	 * @return
	 */
	public static String encryptAES(String content, String password) {
		return byte2HexStr(encryptBitAES(content.getBytes(), password));
	}
 
	/**
	 * AES字符串解密
	 * 
	 * @param content
	 *            待解密内容
	 * @param password
	 *            解密密钥
	 * @return
	 */
	public static String decryptAES(String content, String password) {
		return new String(decryptBitAES(hexStr2Bytes(content), password));
	}
 
	/**
	 * 从指定字符串生成密钥
	 * 
	 * @param password
	 *            构成该秘钥的字符串
	 * @return 生成的密钥
	 * @throws NoSuchAlgorithmException
	 */
	private static Key getKey(String password) throws NoSuchAlgorithmException {
		SecureRandom secureRandom = new SecureRandom(password.getBytes());
		// 生成KEY
		KeyGenerator kgen = KeyGenerator.getInstance("AES");
		kgen.init(128, secureRandom);
		SecretKey secretKey = kgen.generateKey();
		byte[] enCodeFormat = secretKey.getEncoded();
		// 转换KEY
		SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
		return key;
	}
 
	/**
	 * 将byte数组转换为表示16进制值的字符串. 如:byte[]{8,18}转换为:0812 和 byte[]
	 * hexStr2Bytes(String strIn) 互为可逆的转换过程.
	 * 
	 * @param bytes
	 *            需要转换的byte数组
	 * @return 转换后的字符串
	 */
	public static String byte2HexStr(byte[] bytes) {
		int bytesLen = bytes.length;
		// 每个byte用两个字符才能表示,所以字符串的长度是数组长度的两倍
		StringBuffer hexString = new StringBuffer(bytesLen * 2);
		for (int i = 0; i < bytesLen; i++) {
			// 将每个字节与0xFF进行与运算,然后转化为10进制,然后借助于Integer再转化为16进制
			String hex = Integer.toHexString(bytes[i] & 0xFF);
			if (hex.length() < 2) {
				hexString.append(0);// 如果为1位 前面补个0
			}
			hexString.append(hex);
		}
		return hexString.toString();
	}
 
	/**
	 * 将表示16进制值的字符串转换为byte数组, 和 String byte2HexStr(byte[] bytes) 互为可逆的转换过程.
	 * 
	 * @param bytes
	 * @return 转换后的byte数组
	 */
	public static byte[] hexStr2Bytes(String strIn) {
		byte[] arrB = strIn.getBytes();
		int iLen = arrB.length;
 
		// 两个字符表示一个字节,所以字节数组长度是字符串长度除以2
		byte[] arrOut = new byte[iLen / 2];
		for (int i = 0; i < iLen; i = i + 2) {
			String strTmp = new String(arrB, i, 2);
			arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
		}
		return arrOut;
	}
 
	// 密钥
	private final static String secretKey = "liuyunqiang@lx100$#365#$";
	// 向量
	private final static String iv = "01234567";
	// 加解密统一使用的编码方式
	private final static String encoding = "utf-8";
 
	/**
	 * 3DES加密
	 * 
	 * @param plainText
	 *            普通文本
	 * @return
	 * @throws Exception
	 */
	public static String d3esEncode(String plainText) {
		Key deskey = null;
		byte[] encryptData = null;
		try {
			DESedeKeySpec spec = new DESedeKeySpec(secretKey.getBytes());
			SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
			deskey = keyfactory.generateSecret(spec);
 
			Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");
			IvParameterSpec ips = new IvParameterSpec(iv.getBytes());
			cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);
			encryptData = cipher.doFinal(plainText.getBytes(encoding));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Base64.encode(encryptData);
 
	}
 
	/**
	 * 3DES解密
	 * 
	 * @param encryptText
	 *            加密文本
	 * @return
	 * @throws Exception
	 */
	public static String d3esDecode(String encryptText) {
		Key deskey = null;
		byte[] decryptData = null;
		String result = "";
		if (null != encryptText && !"".equals(encryptText)) {
			try {
				DESedeKeySpec spec = new DESedeKeySpec(secretKey.getBytes());
				SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
				deskey = keyfactory.generateSecret(spec);
				Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");
				IvParameterSpec ips = new IvParameterSpec(iv.getBytes());
				cipher.init(Cipher.DECRYPT_MODE, deskey, ips);
 
				decryptData = cipher.doFinal(Base64.decode(encryptText));
				result = new String(decryptData, encoding);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return result;
	}
 
}

 

好的,以下是一个添加秘钥协商和对称加密部件的Python代码示例: ```python import socket import hashlib from Crypto.Cipher import AES # 创建一个套接字对象 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 获取本地IP地址 local_ip = socket.gethostbyname(socket.gethostname()) # 设置端口号 port = 8888 # 绑定IP地址和端口号 sock.bind((local_ip, port)) # 监听客户端请求 sock.listen() print(f"Listening on {local_ip}:{port}") # 等待客户端1连接 client1_sock, client1_address = sock.accept() print(f"Accepted connection from {client1_address}") # 发送公钥给客户端1 public_key = "12345" # 公钥 client1_sock.send(public_key.encode()) # 等待客户端2连接 client2_sock, client2_address = sock.accept() print(f"Accepted connection from {client2_address}") # 发送公钥给客户端2 client2_sock.send(public_key.encode()) # 接收客户端1的密钥 key1 = client1_sock.recv(1024).decode() # 发送密钥给客户端2 client2_sock.send(key1.encode()) # 接收客户端2的密钥 key2 = client2_sock.recv(1024).decode() # 发送密钥给客户端1 client1_sock.send(key2.encode()) # 对称加密算法 def encrypt(key, message): cipher = AES.new(key, AES.MODE_EAX) ciphertext, tag = cipher.encrypt_and_digest(message.encode()) return ciphertext, cipher.nonce, tag # 对称解密算法 def decrypt(key, nonce, ciphertext, tag): cipher = AES.new(key, AES.MODE_EAX, nonce=nonce) plaintext = cipher.decrypt(ciphertext) try: cipher.verify(tag) except ValueError: print("Message authentication failed") return None return plaintext.decode() # 生成密钥 def generate_key(secret): return hashlib.sha256(secret.encode()).digest() while True: # 接收客户端1发来的消息 ciphertext = client1_sock.recv(1024) nonce = client1_sock.recv(16) tag = client1_sock.recv(16) # 解密消息 message = decrypt(generate_key(key1), nonce, ciphertext, tag) if not message: break print(f"Received message from client 1: {message}") # 发送消息给客户端2 message = input("Enter message to send to client 2: ") ciphertext, nonce, tag = encrypt(generate_key(key2), message) client2_sock.send(ciphertext) client2_sock.send(nonce) client2_sock.send(tag) # 接收客户端2发来的消息 ciphertext = client2_sock.recv(1024) nonce = client2_sock.recv(16) tag = client2_sock.recv(16) # 解密消息 message = decrypt(generate_key(key2), nonce, ciphertext, tag) if not message: break print(f"Received message from client 2: {message}") # 发送消息给客户端1 message = input("Enter message to send to client 1: ") ciphertext, nonce, tag = encrypt(generate_key(key1), message) client1_sock.send(ciphertext) client1_sock.send(nonce) client1_sock.send(tag) # 关闭套接字 client1_sock.close() client2_sock.close() sock.close() ``` 在这个示例中,我们首先向两个客户端发送公钥。然后我们等待客户端1发送密钥,将其发送给客户端2,并等待客户端2发送密钥,将其发送给客户端1。这样,双方就完成了秘钥协商。 接下来我们使用一个对称加密算法对通信内容进行加密解密。这里我们使用了AES算法,我们使用一个密码字符串生成一个密钥,然后使用该密钥对通信内容进行加密解密。 在每次接收到消息时,我们首先对其进行解密,然后再打印出来。然后我们要求用户输入要发送给对方的消息,并将其加密后发送给对方。 请注意,这只是一个简单的示例,用于演示如何实现秘钥协商和对称加密。在实际应用中,需要使用更安全的加密算法和更复杂的协商过程来确保通信的安全。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值