Java DES解密代碼


import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.security.Key;

public class DESUtils {

/**
* 密鑰算法
*/
private static final String ALGORITHM = "DES";
/**
* 加密/解密算法-工作模式-填充模式
*/
private static final String CIPHER_ALGORITHM = "DES/CBC/PKCS5Padding";
/**
* 默認編碼
*/
private static final String CHARSET = "utf-8";

public static void main(String[] args) {
//偏移變量,固定占8位字節
String iv = "54a6cc76";
//密碼
String password = "54a6cc76-e700-a1b2-fa3d-fb966efb7578";
//加密
String data = "1234567890";
String encrypt = encrypt(password, iv, data);
System.out.println(encrypt);
//解密
String decrypt = decrypt(password, iv, encrypt);
System.out.println(decrypt);

}

/**
* 生成key
*
* @param password
* @return
* @throws Exception
*/
private static Key generateKey(String password) throws Exception {
DESKeySpec dks = new DESKeySpec(password.getBytes(CHARSET));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
return keyFactory.generateSecret(dks);
}


/**
* DES加密字符串
*
* @param password 加密密碼,長度不能夠小於8位
* @param data 待加密字符串
* @param ivParam 偏移向量
* @return 加密后內容
*/
public static String encrypt(String password, String ivParam, String data) {
if (password== null || password.length() < 8) {
throw new RuntimeException("加密失敗,key不能小於8位");
}
if (data == null){
return null;
}
try {
Key secretKey = generateKey(password);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
IvParameterSpec iv = new IvParameterSpec(ivParam.getBytes(CHARSET));
cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
byte[] bytes = cipher.doFinal(data.getBytes(CHARSET));

BASE64Encoder encoder = new BASE64Encoder();
return new String(encoder.encode(bytes));

} catch (Exception e) {
e.printStackTrace();
return data;
}
}

/**
* DES解密字符串
*
* @param password 解密密碼,長度不能夠小於8位
     * @param ivParam 偏移向量
     * @param data 待解密字符串
* @return 解密后內容
*/
public static String decrypt(String password, String ivParam, String data) {
if (password== null || password.length() < 8) {
throw new RuntimeException("加密失敗,key不能小於8位");
}
if (data == null){
return null;
}
try {
Key secretKey = generateKey(password);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
IvParameterSpec iv = new IvParameterSpec(ivParam.getBytes(CHARSET));
cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
BASE64Decoder decoder = new BASE64Decoder();
return new String(cipher.doFinal(decoder.decodeBuffer(data)), CHARSET);
} catch (Exception e) {
e.printStackTrace();
return data;
}
}

}


免責聲明!

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



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