(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;
- }