java中DES加密(DES/ECB/pkcs5padding)的程式碼分享
阿新 • • 發佈:2019-02-12
package com.oss.util;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
/***
* DES ECB PKCS5Padding 對稱加密 解密
*
*
*/
public class DesECBUtil {
/**
* 加密資料
* @param encryptString
* @param encryptKey
* @return
* @throws Exception
*/
public static String encryptDES(String encryptString, String encryptKey) throws Exception {
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(getKey(encryptKey), "DES"));
byte[] encryptedData = cipher.doFinal(encryptString.getBytes("UTF-8"));
return Base64.encodeBase64String(encryptedData);
}
/**
* key 不足8位補位
* @param string
*/
public static byte[] getKey(String keyRule) {
Key key = null;
byte[] keyByte = keyRule.getBytes();
// 建立一個空的八位陣列,預設情況下為0
byte[] byteTemp = new byte[8];
// 將使用者指定的規則轉換成八位陣列
for (int i = 0; i < byteTemp.length && i < keyByte.length; i++) {
byteTemp[i] = keyByte[i];
}
key = new SecretKeySpec(byteTemp, "DES");
return key.getEncoded();
}
/***
* 解密資料
* @param decryptString
* @param decryptKey
* @return
* @throws Exception
*/
public static String decryptDES(String decryptString, String decryptKey) throws Exception {
byte[] sourceBytes = Base64.decodeBase64(decryptString);
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(getKey(decryptKey), "DES"));
byte[] decoded = cipher.doFinal(sourceBytes);
return new String(decoded, "UTF-8");
}
public static void main(String[] args) throws Exception {
String clearText = "加密的文字";
String key = "123432";//金鑰
System.out.println("明文:"+clearText+"\n金鑰:"+key);
String encryptText = encryptDES(clearText, key);
System.out.println("加密後:"+encryptText);
String decryptText = decryptDES(encryptText, key);
System.out.println("解密後:"+decryptText);
}
}
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
/***
* DES ECB PKCS5Padding 對稱加密 解密
*
*
*/
public class DesECBUtil {
/**
* 加密資料
* @param encryptString
* @param encryptKey
* @return
* @throws Exception
*/
public static String encryptDES(String encryptString, String encryptKey) throws Exception {
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(getKey(encryptKey), "DES"));
byte[] encryptedData = cipher.doFinal(encryptString.getBytes("UTF-8"));
return Base64.encodeBase64String(encryptedData);
}
/**
* key 不足8位補位
* @param string
*/
public static byte[] getKey(String keyRule) {
Key key = null;
byte[] keyByte = keyRule.getBytes();
// 建立一個空的八位陣列,預設情況下為0
byte[] byteTemp = new byte[8];
// 將使用者指定的規則轉換成八位陣列
for (int i = 0; i < byteTemp.length && i < keyByte.length; i++) {
byteTemp[i] = keyByte[i];
}
key = new SecretKeySpec(byteTemp, "DES");
return key.getEncoded();
}
/***
* 解密資料
* @param decryptString
* @param decryptKey
* @return
* @throws Exception
*/
public static String decryptDES(String decryptString, String decryptKey) throws Exception {
byte[] sourceBytes = Base64.decodeBase64(decryptString);
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(getKey(decryptKey), "DES"));
byte[] decoded = cipher.doFinal(sourceBytes);
return new String(decoded, "UTF-8");
}
public static void main(String[] args) throws Exception {
String clearText = "加密的文字";
String key = "123432";//金鑰
System.out.println("明文:"+clearText+"\n金鑰:"+key);
String encryptText = encryptDES(clearText, key);
System.out.println("加密後:"+encryptText);
String decryptText = decryptDES(encryptText, key);
System.out.println("解密後:"+decryptText);
}
}