暗号化に関するいくつかの方法


(1)2バイト配列の異或を求める
/***
	 *  .
	 * 
	 * @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
注意:上記の方法のパラメータは16進数ビット列です.
(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));
	}
実行結果:
9 a 3 fa 1 e 6957 f 79 dbad 799659880 af 8 e 6注意:上記の暗号化は通常のDES暗号化ではありません
 
(3)DES 3暗号化
// 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;
	}