日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

PBE加密

發布時間:2024/1/23 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PBE加密 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.


1 需要的jar包
commons-codec-1.9.jar




2 代碼


package com.cpcnet.util;




import java.security.Key; ? ?
import java.security.SecureRandom; ? ?
import java.text.SimpleDateFormat;
import java.util.Date;


import javax.crypto.Cipher; ? ?
import javax.crypto.SecretKey; ? ?
import javax.crypto.SecretKeyFactory; ? ?
import javax.crypto.spec.PBEKeySpec; ? ?
import javax.crypto.spec.PBEParameterSpec; ? ?


import org.apache.commons.codec.binary.Base64; ? ?
/** ?
?*對稱加密算法,基于口令加密-PBE算法實現
?* 使用java6提供的PBEWITHMD5andDES算法進行展示
?* @author kongqz ?
?* */ ? ?
public class PBECoder { ? ?
private static String password;
private static byte[] salt;
? ? ? ??
? ? /** ?
? ? ?* JAVA6支持一下任意一種算法
? ? ?* PBEWITHMD5ANDDES ?
? ? ?* PBEWITHMD5ANDTRIPLEDES ?
? ? ?* PBEWITHSHAANDDESEDE ?
? ? ?* PBEWITHSHA1ANDRC2_40 ?
? ? ?* PBKDF2WITHHMACSHA1 ?
? ? ?* */ ? ?
? ? public static final String ALGORITHM="PBEWITHMD5andDES"; ? ?
? ? ? ??
? ? /** ?
? ? ?*+迭代次數
? ? ?* */ ? ?
? ? public static final int ITERATION_COUNT=100; ? ?
? ? ? ??
? ? /** ?
? ? ?* 鹽初始化
? ? ?* 鹽長度必須為8字節
? ? ?* @return byte[] 鹽
? ? ?* */ ? ?
? ? public static byte[] initSalt() throws Exception{ ? ?
? ? ? ? //實例化安全隨機數
? ? ? ? SecureRandom random=new SecureRandom(); ??
? ? ? ? System.out.println();
? ? ? ? //產出鹽
? ? ? ? return random.generateSeed(8); ? ?
? ? } ? ?
? ? ? ??
? ? /** ?
? ? ?* 轉換秘鑰
? ? ?* @param password ?密碼
? ? ?* @return Key ?秘鑰
? ? ?* */ ? ?
? ? private static Key toKey(String password) throws Exception{ ? ?
? ? ? ? //秘鑰轉換
? ? ? ? PBEKeySpec keySpec=new PBEKeySpec(password.toCharArray()); ? ?
? ? ? ? //實例化
? ? ? ? SecretKeyFactory keyFactory=SecretKeyFactory.getInstance(ALGORITHM); ? ?
? ? ? ? //生成秘鑰
? ? ? ? SecretKey secretKey=keyFactory.generateSecret(keySpec); ? ?
? ? ? ? ? ??
? ? ? ? return secretKey; ? ?
? ? } ? ?
? ? /** ?
? ? ?*加密
? ? ?* @param data 待加密數據
? ? ?* @param password 密碼
? ? ?* @param salt 鹽
? ? ?* @return byte[] 加密數據
? ? ?* ??
? ? ?* */ ? ?
? ? public static byte[] encrypt(byte[] data) throws Exception{ ? ?
? ? ? ? //轉換秘鑰
? ? ? ? Key key=toKey(password); ? ?
? ? ? ? //實例化PBE參數材料
? ? ? ? PBEParameterSpec paramSpec=new PBEParameterSpec(salt,ITERATION_COUNT); ? ?
? ? ? ? //實例化
? ? ? ? Cipher cipher=Cipher.getInstance(ALGORITHM); ? ?
? ? ? ? //初始化
? ? ? ? cipher.init(Cipher.ENCRYPT_MODE, key,paramSpec); ? ?
? ? ? ? //執行操作
? ? ? ? return cipher.doFinal(data); ? ?
? ? } ? ?
? ? /** ?
? ? ?* 解密
? ? ?* @param data 待解密數據
? ? ?* @param password 密碼
? ? ?* @param salt 鹽
? ? ?* @return byte[] 解密數據
? ? ?* ??
? ? ?* */ ? ?
? ? public static byte[] decrypt(byte[] data) throws Exception{ ? ?
? ? ? ? //轉換秘鑰
? ? ? ? Key key=toKey(password); ? ?
? ? ? ? //實例化PBE參數材料
? ? ? ? PBEParameterSpec paramSpec=new PBEParameterSpec(salt,ITERATION_COUNT); ? ?
? ? ? ? //實例化
? ? ? ? Cipher cipher=Cipher.getInstance(ALGORITHM); ? ?
? ? ? ? //初始化
? ? ? ? cipher.init(Cipher.DECRYPT_MODE, key,paramSpec); ? ?
? ? ? ? //執行操作
? ? ? ? return cipher.doFinal(data); ? ?
? ? } ? ?
? ??
? ??
? ? //here is init the salt and password
? ? static{
? ? password = PropertiesUtil.getProp("PBEconfig.properties", "password");
? ? password = password + new Date();
? ? Date nowDate = new Date();
? ? ? ? SimpleDateFormat sdf = new SimpleDateFormat("MMddhhmm");
? ? ? ? String nowTime = sdf.format(nowDate);
? ? salt = nowTime.getBytes();
? ?
? ? }
? ??
? ??
? ??
? ??
? ? public static String getPassword() {
return password;
}


public static byte[] getSalt() {
return salt;
}

? public static String ?enCode(String soruce){
? ? byte[] data = null;
try {
data = PBECoder.encrypt(soruce.getBytes());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} ? ?
String result = Base64.encodeBase64String(data);
? ? ? return result;
? ?}
? ?
? ?public static String deCode(String source){
? ? ? ?byte[] jieMiByte = Base64.decodeBase64(source);
? ? ? ?byte[] jiemi = null;
try {
jiemi = PBECoder.decrypt(jieMiByte);
} catch (Exception e) {

e.printStackTrace();
}?
? ? return new String(jiemi);
? ?}




? ? public static void main(String[] args) throws Exception { ? ?
? ? ? ? //待加密數據
? ? ? ? String str="PBE"; ? ?
??
? ? ? ? //加密后
? ? ? ?String jiami = ?PBECoder.enCode(str);
? ? ? ?System.out.println("加密后"+jiami); ??
? ? ? ?
? ? ? ? ?
? ? ? ? //解密后
? ? ? ? String jiemi ?= PBECoder.deCode(jiami);
? ? ? ? System.out.println("解密后"+ jiemi); ? ?
? ? ?
? ? } ? ?
} ? ?

總結

以上是生活随笔為你收集整理的PBE加密的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。