AES加密 16進制與二進制轉換


import java.security.Key;

import javax.crypto.Cipher;

import javax.crypto.KeyGenerator;

import javax.crypto.SecretKey;

import javax.crypto.spec.SecretKeySpec;

 

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

 

/**

 * AES安全編碼組件

 */

public abstract class AESCoder {

 

/**

* 密鑰算法

*/

public static final String KEY_ALGORITHM = "AES";

 

/**

* 加密/解密算法 / 工作模式 / 填充方式 Java 6支持PKCS5Padding填充方式 Bouncy

* Castle支持PKCS7Padding填充方式

*/

public static final String CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";

 

/**

* 轉換密鑰

* @param key

*            二進制密鑰

* @return Key 密鑰

* @throws Exception

*/

private static Key toKey(byte[] key) throws Exception {

 

// 實例化AES密鑰材料

SecretKey secretKey = new SecretKeySpec(key, KEY_ALGORITHM);

 

return secretKey;

}

 

/**

* 解密

* @param data

*            待解密數據

* @param key

*            密鑰

* @return byte[] 解密數據

* @throws Exception

*/

public static byte[] decrypt(byte[] data, byte[] key) throws Exception {

 

// 還原密鑰

Key k = toKey(key);

 

/*

* 實例化 使用PKCS7Padding填充方式,按如下方式實現 Cipher.getInstance(CIPHER_ALGORITHM,

* "BC");

*/

Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);

 

// 初始化,設置為解密模式

cipher.init(Cipher.DECRYPT_MODE, k);

 

// 執行操作

return cipher.doFinal(data);

}

 

/**

* 加密

* @param data

*            待加密數據

* @param key

*            密鑰

* @return byte[] 加密數據

* @throws Exception

*/

public static byte[] encrypt(byte[] data, byte[] key) throws Exception {

 

// 還原密鑰

Key k = toKey(key);

 

/*

* 實例化 使用PKCS7Padding填充方式,按如下方式實現 Cipher.getInstance(CIPHER_ALGORITHM,

* "BC");

*/

Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);

 

// 初始化,設置為加密模式

cipher.init(Cipher.ENCRYPT_MODE, k);

 

// 執行操作

return cipher.doFinal(data);

}

 

/**

* 生成密鑰 <br>

* @return byte[] 二進制密鑰

* @throws Exception

*/

public static byte[] initKey() throws Exception {

 

// 實例化

KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM);

 

/*

* AES 要求密鑰長度為 128位、192位或 256位,這里用128位

*/

kg.init(128);

 

// 生成秘密密鑰

SecretKey secretKey = kg.generateKey();

 

// 獲得密鑰的二進制編碼形式

return secretKey.getEncoded();

}

 

/**

* 將byte數組轉換成16進制String

* @param buf

* @return

*/

public static String parseByte2HexStr(byte buf[]) {

StringBuffer sb = new StringBuffer();

for (int i = 0; i < buf.length; i++) {

String hex = Integer.toHexString(buf[i] & 0xFF);

if (hex.length() == 1) {

hex = '0' + hex;

}

sb.append(hex.toUpperCase());

}

return sb.toString();

}

 

/**

* 將16進制String轉換為byte數組

* @param hexStr

* @return

*/

public static byte[] parseHexStr2Byte(String hexStr) {

if (hexStr.length() < 1)

return null;

byte[] result = new byte[hexStr.length() / 2];

for (int i = 0; i < hexStr.length() / 2; i++) {

int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);

int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);

result[i] = (byte) (high * 16 + low);

}

return result;

}

 

public static void main(String[] args) throws Exception {

// 初始化密鑰

    byte[] key = Base64.decodeBase64("Geyd4zScWAbJT9VrWr5cUQ==");

    

    String inputStr = "TCWeb.Bank";

byte[] inputData = inputStr.getBytes();

    inputData = AESCoder.encrypt(inputData, key);

    

    String userName = Base64.encodeBase64String(inputData);

    System.out.println("加密后:" + userName);

    

    // 解密

    byte[] userNameData = AESCoder.decrypt(Base64.decodeBase64(userName), key);

    userName = new String(userNameData);

    System.out.println("userName:" + userName);

}

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM