java加密與解密之——對稱加密
對稱加密:
DES代碼示例:
import java.security.Key;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
/**
* 對稱加密演算法 DES
*/
public class TestDES {
/**密鑰演算法**/
public static final String KEY_ALGORITHM = "DES";
/**加密/解密演算法 /工作模式/填充方式**/
public static final String CIPHER_ALGORITHM = "DES/ECB/PKCS5PADDING";
private static final char[] CH_HEX = {"0","1","2","3","4","5","6","7","8","9","A",
"B","C","D","E","F"};
/**
*生成密鑰
* @return
* @throws Exception
*/
public static byte[] initKey() throws Exception{
//實例化密鑰生成器
KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM);
//初始化密鑰生成器
kg.init(56, new SecureRandom());
//生成密鑰
SecretKey secretKey = kg.generateKey();
//獲得密鑰的二進位編碼形式
return secretKey.getEncoded();
}
/**
* 轉換密鑰
* @param key
* @return
* @throws Exception
*/
public static Key toKey(byte[] key) throws Exception{
//實例化des密鑰材料
DESKeySpec keySpec = new DESKeySpec(key);
//實例化密鑰工廠
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM);
//生成秘密密鑰
SecretKey secretKey = keyFactory.generateSecret(keySpec);
return secretKey;
}
/**
* 加密
* @param data
* @param key
* @return
* @throws Exception
*/
public static byte[] encrypt(byte[] data,byte[] key) throws Exception{
//還原密鑰
Key k = toKey(key);
//實例化
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
//初始化,設置化加密模式
cipher.init(Cipher.ENCRYPT_MODE,k);
return cipher.doFinal(data);
}
public static byte[] decrypt(byte[] data,byte[] key) throws Exception{
//還原密鑰
Key k = toKey(key);
//實例化
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
//初始化,設置化加密模式
cipher.init(Cipher.DECRYPT_MODE,k);
return cipher.doFinal(data);
}
public static void main(String[] args) {
String str = "ceshides";
try {
//獲得密鑰
byte[] keys = initKey();
//加密
byte[] encrypes = encrypt(str.getBytes(),keys);
String result = byteArrayToHex(encrypes);
System.out.println("加密後結果為:"+result);
//解密
hexStringToBytes(result);
byte[] decrypts = decrypt(hexStringToBytes(result), keys);
System.out.println("解密結果為:"+new String(decrypts));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 將位元組數組轉化為16進位字元串
* @param bytes
* @return
*/
private static String byteArrayToHex(byte[] bytes){
// 一個位元組佔8位,一個十六進位字元佔4位;十六進位字元數組的長度為位元組數組長度的兩倍
char[] chars = new char[bytes.length*2];
int index = 0;
for(byte b:bytes){
//取位元組的高4位
chars[index++] = CH_HEX[b>>>4 & 0xf];
//取位元組的低4位
chars[index++] = CH_HEX[b & 0xf];
}
return new String(chars).toUpperCase();
}
/**
* 16進位字元串轉換為位元組數組
* @param hexString
* @return
*/
public static byte[] hexStringToBytes(String hexString) {
if (hexString == null || hexString.equals("")) {
return null;
}
hexString = hexString.toUpperCase();
int length = hexString.length() / 2;
char[] hexChars = hexString.toCharArray();
byte[] d = new byte[length];
for (int i = 0; i < length; i++) {
int pos = i * 2;
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
}
return d;
}
private static byte charToByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(c);
}
}
測試結果為:
加密後結果為:4B9ADEDC61FD508F082A1965C8AEB669
解密結果為:ceshides
微公號:roc_shangcp
※Java加密與解密
※Java加密與解密——非對稱加密
※Java加密解密——數字簽名
※數據泵導入導出資料庫
※oracle插入時如何自動生成主鍵
TAG:笑月天狼 |
※base64加密的解密
※如何運用OpenSSL 對文件進行加密和解密
※AES加密解密以及遇到的問題
※Nvidia加大VR顯卡產量 應對加密幣需求
※如何解密LockCrypt勒索軟體加密的文件
※Twitter將封殺加密幣廣告
※俄羅斯禁用加密通訊軟體Telegram:拒向官方提供密鑰
※Python字元串加密解密方法總結
※絕密秘密私密加密
※對稱加密和Base64編碼
※Telegram 拒交加密金鑰遭俄羅斯政府封殺
※密碼及加密方式
※繼Facebook後:谷歌也加入封殺加密貨幣廣告隊伍
※Ripple對於監管機構參與加密貨幣感到興奮
※區塊鏈加密技術,非對稱加密是什麼?
※Sodium:一個提供加密、解密、簽名等功能的軟體庫
※我與加密貨幣的故事「不加密」——談談我眼中的加密貨幣
※Facebook的Libra缺乏加密密鑰安全的基礎組件
※Python 的加密庫入門
※Linux下實現 OpenSSL 簡單加密與解密字元串