加密相关的一些方法

本文介绍如何实现两个十六进制字符串的异或运算,并提供了CBC和DES3加密算法的具体实现与示例。通过这些方法可以进行数据的安全处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(1)求两个字节数组的异或

/***
	 * 求异或.
	 * 
	 * @param strOldHex  : hex string
	 * @param strKeyHex  : hex string
	 * @return
	 */
	public static byte[] xOR(String strOldHex, String strKeyHex) {
		byte[] oldBytes = ByteStringUtil.hexString2Bytes(strOldHex);
		byte[] keyBytes = ByteStringUtil.hexString2Bytes(strKeyHex);
		byte[] xorResult = new byte[oldBytes.length];
		int keyIndex = 0;
		for (int x = 0; x < oldBytes.length; x++) {
			xorResult[x] = (byte) (oldBytes[x] ^ keyBytes[keyIndex]);
			if (++keyIndex == keyBytes.length) {
				keyIndex = 0;
			}
		}
		return xorResult;
	}

 测试如下:

@Test
	public void test_XOR() {
		String strOldHex = "8080";
		String strKeyHex = "8182";
		byte[]xorResult=CustomMACUtil.xOR(strOldHex, strKeyHex);
		
		System.out.println("---------------");
		System.out.println(ByteStringUtil.byteArrayToHexString(xorResult));
	}

 运行结果:

0102

注意:上述方法的参数是十六进制位串

(2)CBC加密

/**
	 * 加密函数
	 * 
	 * @param data
	 *            加密数据
	 * @param key
	 *            密钥
	 * @param iv 
	 * @return 返回加密后的数据
	 */
	public static byte[] desCBCEncrypt(byte[] data, byte[] key, byte[] iv) {
		try {
			// 从原始密钥数据创建DESKeySpec对象
			DESKeySpec dks = new DESKeySpec(key);

			// 创建一个密匙工厂,然后用它把DESKeySpec转换成
			// 一个SecretKey对象
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
			SecretKey secretKey = keyFactory.generateSecret(dks);

			// Cipher对象实际完成加密操作
			// Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
			// 若采用NoPadding模式,data长度必须是8的倍数
			Cipher cipher = Cipher.getInstance("DES/CBC/NoPadding");

			// 用密匙初始化Cipher对象
			IvParameterSpec param = new IvParameterSpec(iv);
			cipher.init(Cipher.ENCRYPT_MODE, secretKey, param);

			// 执行加密操作
			byte encryptedData[] = cipher.doFinal(data);

			return encryptedData;
		} catch (Exception e) {
			System.err.println("DES-CBC算法,加密数据出错!");
			e.printStackTrace();
		}

		return null;
	}

 测试如下:

@Test
	public void test_desCBCEncrypt() {
		String data2 = "03DA9F790A007A1Fe49309DA148F5c00";
		String leftHalf = "1b03aa6415bb0a54";
		byte[] macResultBytes;
		macResultBytes = DESUtil.desCBCEncrypt(
				ByteStringUtil.hexString2Bytes(data2),
				ByteStringUtil.hexString2Bytes(leftHalf), new byte[8]);
		System.out.println(ByteStringUtil.byteArrayToHexString(macResultBytes));
	}

 运行结果:

9a3fa1e6957f79dbad799659880af8e6

 注意:上述加密不是普通的DES加密

 

(3)DES3加密

// keybyte为加密密钥,长度为24字节
	// src为被加密的数据缓冲区(源)
	public static byte[] encryptMode(byte[] src, byte[] keybyte) {
		try {
			// 如果加密密钥的长度为16个字节,则把开始的8个字节补到最后变成24个字节
			if (keybyte.length == 16) {
				String newKeybyte = ByteStringUtil.byteArrayToHexString(keybyte);
				String newKeybyte1 = newKeybyte + newKeybyte.substring(0, 16);
				keybyte = ByteStringUtil.hexString2Bytes(newKeybyte1);
			}
			// 生成密钥
			SecretKey deskey = new SecretKeySpec(keybyte, "DESede");
			final IvParameterSpec iv = new IvParameterSpec(new byte[8]);
			// 加密
			Cipher c1 = Cipher.getInstance("DESede/CBC/PKCS5Padding");
			c1.init(Cipher.ENCRYPT_MODE, deskey,iv);
			return c1.doFinal(src);
		} catch (java.security.NoSuchAlgorithmException e1) {
			e1.printStackTrace();
		} catch (javax.crypto.NoSuchPaddingException e2) {
			e2.printStackTrace();
		} catch (java.lang.Exception e3) {
			e3.printStackTrace();
		}
		return null;
	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值