1.å¦ä½ç¨Javaè¿è¡3DESå å¯è§£å¯
2.ç¨javaå®ç°deså å¯å解å¯
3.java deså å¯ä¸ºä»ä¹åç½ç«
å¦ä½ç¨Javaè¿è¡3DESå å¯è§£å¯
è¿éæ¯ä¾åï¼ç´æ¥æ¿æ¥ç¨å°±å¯ä»¥äºã
package com.nnff.des;
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
/*å符串 DESede(3DES) å å¯
* ECB模å¼/使ç¨PKCS7æ¹å¼å¡«å ä¸è¶³ä½,加解密s解竞拍红酒积分源码ç®åç»çå¯é¥æ¯ä½
* 3DESï¼å³Triple DESï¼æ¯DESåAESè¿æ¸¡çå å¯ç®æ³ï¼å¹´ï¼NISTå°3-DESæå®ä¸ºè¿æ¸¡ç
* å å¯æ åï¼ï¼æ¯DESçä¸ä¸ªæ´å®å ¨çåå½¢ãå®ä»¥DES为åºæ¬æ¨¡åï¼éè¿ç»ååç»æ¹æ³è®¾è®¡åºåç»å
* å¯ç®æ³ï¼å ¶å ·ä½å®ç°å¦ä¸ï¼è®¾Ek()åDk()代表DESç®æ³çå å¯å解å¯è¿ç¨ï¼K代表DESç®æ³ä½¿ç¨ç
* å¯é¥ï¼P代表ææï¼C代表å¯è¡¨ï¼è¿æ ·ï¼
* 3DESå å¯è¿ç¨ä¸ºï¼C=Ek3(Dk2(Ek1(P)))
* 3DES解å¯è¿ç¨ä¸ºï¼P=Dk1((EK2(Dk3(C)))
* */
public class ThreeDes {
/**
* @param argså¨javaä¸è°ç¨sunå ¬å¸æä¾ç3DESå å¯è§£å¯ç®æ³æ¶ï¼éè¦ä½¿
* ç¨å°$JAVA_HOME/jre/lib/ç®å½ä¸å¦ä¸ç4个jarå ï¼
*jce.jar
*security/US_export_policy.jar
*security/local_policy.jar
*ext/sunjce_provider.jar
*/
private static final String Algorithm = "DESede"; //å®ä¹å å¯ç®æ³,å¯ç¨ DES,DESede,Blowfish
//keybyte为å å¯å¯é¥ï¼é¿åº¦ä¸ºåè
//src为被å å¯çæ°æ®ç¼å²åºï¼æºï¼
public static byte[] encryptMode(byte[] keybyte,byte[] src){
try {
//çæå¯é¥
SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);
//å å¯
Cipher c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.ENCRYPT_MODE, deskey);
return c1.doFinal(src);//å¨åä¸æ¹é¢çå å¯æ解å¯
} catch (java.security.NoSuchAlgorithmException e1) {
// TODO: handle exception
e1.printStackTrace();
}catch(javax.crypto.NoSuchPaddingException e2){
e2.printStackTrace();
}catch(java.lang.Exception e3){
e3.printStackTrace();
}
return null;
}
//keybyte为å å¯å¯é¥ï¼é¿åº¦ä¸ºåè
//src为å å¯åçç¼å²åº
public static byte[] decryptMode(byte[] keybyte,byte[] src){
try {
//çæå¯é¥
SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);
//解å¯
Cipher c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.DECRYPT_MODE, deskey);
return c1.doFinal(src);
} catch (java.security.NoSuchAlgorithmException e1) {
// TODO: handle exception
e1.printStackTrace();
}catch(javax.crypto.NoSuchPaddingException e2){
e2.printStackTrace();
}catch(java.lang.Exception e3){
e3.printStackTrace();
}
return null;
}
//转æ¢æåå è¿å¶å符串
public static String byte2Hex(byte[] b){
String hs="";
String stmp="";
for(int n=0; n<b.length; n++){
stmp = (java.lang.Integer.toHexString(b[n]& 0XFF));
if(stmp.length()==1){
hs = hs + "0" + stmp;
}else{
hs = hs + stmp;
}
if(n<b.length-1)hs=hs+":";
}
return hs.toUpperCase();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
//æ·»å æ°å®å ¨ç®æ³,å¦æç¨JCEå°±è¦æå®æ·»å è¿å»
Security.addProvider(new com.sun.crypto.provider.SunJCE());
final byte[] keyBytes = { 0x, 0x, 0x4F, 0x,
(byte)0x, 0x, 0x, 0x, 0x, 0x, 0x, 0x,
(byte)0xCB,
(byte)0xDD, 0x, 0x, 0x, 0x, 0x,
(byte)0x, 0x, 0x, 0x,
(byte)0xE2
}; //åèçå¯é¥
String szSrc = "This is a 3DES test. æµè¯";
System.out.println("å å¯åçå符串:" + szSrc);
byte[] encoded = encryptMode(keyBytes,szSrc.getBytes());
System.out.println("å å¯åçå符串:" + new String(encoded));
byte[] srcBytes = decryptMode(keyBytes,encoded);
System.out.println("解å¯åçå符串:" + (new String(srcBytes)));
}
}
ç¨javaå®ç°deså å¯å解å¯
ä¸ä¸ªç¨DESæ¥å å¯ã解å¯çç±»
/java/1/.html
import java.security.*;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
/**
* åç¬¦ä¸²å·¥å ·éå
* @author Liudong
*/
public class StringUtils {
private static final String PASSWORD_CRYPT_KEY = "__jDlog_";
private final static String DES = "DES";
/**
* å å¯
* @param src æ°æ®æº
* @param key å¯é¥ï¼é¿åº¦å¿ é¡»æ¯8çåæ°
* @return è¿åå å¯åçæ°æ®
* @throws Exception
*/
public static byte[] encrypt(byte[] src, byte[] key)throws Exception {
//DESç®æ³è¦æ±æä¸ä¸ªå¯ä¿¡ä»»çéæºæ°æº
SecureRandom sr = new SecureRandom();
// ä»åå§å¯åæ°æ®å建DESKeySpec对象
DESKeySpec dks = new DESKeySpec(key);
// å建ä¸ä¸ªå¯åå·¥åï¼ç¶åç¨å®æDESKeySpec转æ¢æ
// ä¸ä¸ªSecretKey对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
SecretKey securekey = keyFactory.generateSecret(dks);
// Cipher对象å®é å®æå å¯æä½
Cipher cipher = Cipher.getInstance(DES);
// ç¨å¯ååå§åCipher对象
cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
// ç°å¨ï¼è·åæ°æ®å¹¶å å¯
// æ£å¼æ§è¡å å¯æä½
return cipher.doFinal(src);
}
/**
* 解å¯
* @param src æ°æ®æº
* @param key å¯é¥ï¼é¿åº¦å¿ é¡»æ¯8çåæ°
* @return è¿å解å¯åçåå§æ°æ®
* @throws Exception
*/
public static byte[] decrypt(byte[] src, byte[] key)throws Exception {
// DESç®æ³è¦æ±æä¸ä¸ªå¯ä¿¡ä»»çéæºæ°æº
SecureRandom sr = new SecureRandom();
// ä»åå§å¯åæ°æ®å建ä¸ä¸ªDESKeySpec对象
DESKeySpec dks = new DESKeySpec(key);
// å建ä¸ä¸ªå¯åå·¥åï¼ç¶åç¨å®æDESKeySpec对象转æ¢æ
// ä¸ä¸ªSecretKey对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
SecretKey securekey = keyFactory.generateSecret(dks);
// Cipher对象å®é å®æ解å¯æä½
Cipher cipher = Cipher.getInstance(DES);
// ç¨å¯ååå§åCipher对象
cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
// ç°å¨ï¼è·åæ°æ®å¹¶è§£å¯
// æ£å¼æ§è¡è§£å¯æä½
return cipher.doFinal(src);
}
/**
* å¯ç 解å¯
* @param data
* @return
* @throws Exception
*/
public final static String decrypt(String data){
try {
return new String(decrypt(hex2byte(data.getBytes()),
PASSWORD_CRYPT_KEY.getBytes()));
}catch(Exception e) {
}
return null;
}
/**
* å¯ç å å¯
* @param password
* @return
* @throws Exception
*/
public final static String encrypt(String password){
try {
return byte2hex(encrypt(password.getBytes(),PASSWORD_CRYPT_KEY.getBytes())); }catch(Exception e) {
}
return null;
}
æ¯è¾é¿, 转äºä¸é¨å.
java deså å¯ä¸ºä»ä¹åç½ç«
JAVAå®ç°
å å¯
代ç æ详ç»è§£éï¼ä¸å¤åºè¯ã
注æï¼DESå å¯å解å¯è¿ç¨ä¸ï¼å¯é¥é¿åº¦é½å¿ é¡»æ¯8çåæ°
[java] view plain copy
public byte[] desCrypto(byte[] datasource, String password) {
try{
SecureRandom random = new SecureRandom();
DESKeySpec desKey = new DESKeySpec(password.getBytes());
//å建ä¸ä¸ªå¯åå·¥åï¼ç¶åç¨å®æDESKeySpec转æ¢æ
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey securekey = keyFactory.generateSecret(desKey);
//Cipher对象å®é å®æå å¯æä½
Cipher cipher = Cipher.getInstance("DES");
//ç¨å¯ååå§åCipher对象
cipher.init(Cipher.ENCRYPT_MODE, securekey, random);
//ç°å¨ï¼è·åæ°æ®å¹¶å å¯
//æ£å¼æ§è¡å å¯æä½
return cipher.doFinal(datasource);
}catch(Throwable e){
e.printStackTrace();
}
return null;
}
解å¯
代ç æ详ç»æ³¨éï¼ä¸å¤åºè¯
[java] view plain copy
private byte[] decrypt(byte[] src, String password) throws Exception {
// DESç®æ³è¦æ±æä¸ä¸ªå¯ä¿¡ä»»çéæºæ°æº
SecureRandom random = new SecureRandom();
// å建ä¸ä¸ªDESKeySpec对象
DESKeySpec desKey = new DESKeySpec(password.getBytes());
// å建ä¸ä¸ªå¯åå·¥å
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
// å°DESKeySpec对象转æ¢æSecretKey对象
SecretKey securekey = keyFactory.generateSecret(desKey);
// Cipher对象å®é å®æ解å¯æä½
Cipher cipher = Cipher.getInstance("DES");
// ç¨å¯ååå§åCipher对象
cipher.init(Cipher.DECRYPT_MODE, securekey, random);
// çæ£å¼å§è§£å¯æä½
return cipher.doFinal(src);
}
æµè¯åºæ¯
ä¾å¦ï¼æ们å¯ä»¥å©ç¨å¦ä¸å½æ°å¯¹å符串è¿è¡å å¯è§£å¯ï¼ä¹å¯ä»¥å¯¹æ件è¿è¡å å¯è§£å¯ï¼å¦ï¼
[java] view plain copy
//å¾ å å¯å 容
String str = "æµè¯å 容";
//å¯ç ï¼é¿åº¦è¦æ¯8çåæ°
String password = "";
byte[] result = DESCrypto.desCrypto(str.getBytes(),password);
System.out.println("å å¯åå 容为ï¼"+new String(result));
//ç´æ¥å°å¦ä¸å 容解å¯
try {
byte[] decryResult = des.decrypt(result, password);
System.out.println("å å¯åå 容为ï¼"+new String(decryResult));
} catch (Exception e1) {
e1.printStackTrace();
}
android源码推荐
股票捉妖源码_股票捉妖源码是什么
生鲜系统源码_生鲜配送系统源码
屏幕识图源码_屏幕识图源码是什么
app源码维护
墨香源码乱码