站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2EE 专题求des加密算法类,最好有例程

求des加密算法类,最好有例程

摘要: 求des加密算法类,最好有例程


想在web中实现des数据加密,谁有des算法类,谢谢.最好有例程


网上搜搜,多的是


import java.security.Security;
import java.security.spec.AlgorithmParameterSpec;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;

public class MainClass {
public static void main(String args[]) throws Exception {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
SecretKey key = KeyGenerator.getInstance("DES").generateKey();

// for CBC; must be 8 bytes
byte[] initVector = new byte[] { 0x10, 0x10, 0x01, 0x04, 0x01, 0x01, 0x01, 0x02 };

AlgorithmParameterSpec algParamSpec = new IvParameterSpec(initVector);
Cipher m_encrypter = Cipher.getInstance("DES/CBC/PKCS5Padding");
Cipher m_decrypter = Cipher.getInstance("DES/CBC/PKCS5Padding");

m_encrypter.init(Cipher.ENCRYPT_MODE, key, algParamSpec);
m_decrypter.init(Cipher.DECRYPT_MODE, key, algParamSpec);

byte[] clearText = "www.java2s.com".getBytes();

byte[] encryptedText = m_encrypter.doFinal(clearText);

byte[] decryptedText = m_decrypter.doFinal(encryptedText);

System.out.println(new String(clearText));
System.out.println(new String(encryptedText));
System.out.println(new String(decryptedText));

}

}


我写过DES的程序,给我发邮件,我给你代码和jar包。
attantao@21cn.com


import java.security.*;
import javax.crypto.*;
import sun.misc.*;

/**
* 使用DES加密与解密,可对byte[],String类型进行加密与解密
* 密文可使用String,byte[]存储.
* 方法:
* void getKey(String strKey)从strKey的字条生成一个Key
* String getEncString(String strMing)对strMing进行加密,返回String密文
* String getDesString(String strMi)对strMin进行解密,返回String明文
* byte[] getEncCode(byte[] byteS)byte[]型的加密
* byte[] getDesCode(byte[] byteD)byte[]型的解密
*/
public class DesEncrypt {
Key key;

public DesEncrypt(String str){
getKey(str);//生成密匙
}
/**
* 根据参数生成KEY
*/
public void getKey(String strKey){
try{
KeyGenerator _generator = KeyGenerator.getInstance("DES");
_generator.init(new SecureRandom(strKey.getBytes()));
this.key = _generator.generateKey();
_generator = null;
}catch (Exception e){
throw new RuntimeException("Error initializing SqlMap class. Cause: "+e);
}
}
/**
* 加密String明文输入,String密文输出
*/
public String getEncString(String strMing) {
byte[] byteMi = null;
byte[] byteMing = null;
String strMi = "";
BASE64Encoder base64en = new BASE64Encoder();
try {
byteMing = strMing.getBytes("UTF8");
byteMi = this.getEncCode(byteMing);
strMi = base64en.encode(byteMi);
} catch (Exception e) {
throw new RuntimeException("Error initializing SqlMap class. Cause: "+e);
} finally {
base64en = null;
byteMing = null;
byteMi = null;
}
return strMi;
}
/**
* 解密 以String密文输入,String明文输出
* @param strMi
* @return
*/
public String getDesString(String strMi) {
BASE64Decoder base64De = new BASE64Decoder();
byte[] byteMing = null;
byte[] byteMi = null;
String strMing = "";
try {
byteMi = base64De.decodeBuffer(strMi);
byteMing = this.getDesCode(byteMi);
strMing = new String(byteMing, "UTF8");
} catch (Exception e) {
throw new RuntimeException("Error initializing SqlMap class. Cause: "+e);
} finally {
base64De = null;
byteMing = null;
byteMi = null;
}
return strMing;
}
/**
* 加密以byte[]明文输入,byte[]密文输出
* @param byteS
* @return
*/
private byte[] getEncCode(byte[] byteS) {
byte[] byteFina = null;
Cipher cipher;
try {
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byteFina = cipher.doFinal(byteS);
} catch (Exception e) {
throw new RuntimeException("Error initializing SqlMap class. Cause: "+e);
} finally {
cipher = null;
}
return byteFina;
}
/**
* 解密以byte[]密文输入,以byte[]明文输出
* @param byteD
* @return
*/
private byte[] getDesCode(byte[] byteD) {
Cipher cipher;
byte[] byteFina = null;
try {
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byteFina = cipher.doFinal(byteD);
} catch (Exception e) {
throw new RuntimeException("Error initializing SqlMap class. Cause: "+e);
} finally {
cipher = null;
}
return byteFina;
}

public static void main(String args[]) {
DesEncrypt des = new DesEncrypt("你的密钥");
String str1="你的明文";
//DES加密
String str2=des.getEncString(str1);
System.out.println("根据密钥加密后的密文:"+str2);
//DES解密
System.out.println("根据密钥解密后的明文:"+des.getDesString(str2));
}
}


(一)
/*
* Created on 2005-2-28
*/
package org.me.crypto.des;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

/**
*

Title: DES加解密


*

Copyright: Copyright (c) 2005


*
* @author Anchor
* @version 1.0
*
* 这个类中提供了:
* 1)DES加密、解密的方法
* 2)设置、获取密钥的方法
* 3)设置、获取源文件的方法
* 4)设置、获取密文的方法
*/

public class DES{
private final String ALGORITHM = "DES";// 算法名称
private String key = " ";// 密钥
private String source;// 源信息
private byte[] cryptograph;// 密文

/**
* 构造器
*/
public DES(){
}

/**
* 构造器
* @param source-原文信息
*/
public DES(String source){
this.source = source;
}

/**
* 构造器
* @param source-原文信息
* @param key-密钥
*/
public DES(String source,String key){
this.source = source;
this.key = key;
}

/**
* 构造器
* @param source-原文信息
* @param key-密钥
*/
public DES(byte[] cryptograph,String key){
this.cryptograph = cryptograph;
this.key = key;
}

/**
* 构造器
* @param file-原文文件
*/
public DES(File file){
try{
InputStreamReader input = new InputStreamReader(new FileInputStream(file));
BufferedReader buffer = new BufferedReader(input);
String line;
while((line = buffer.readLine()) != null){
source = source+line;
}
buffer.close();
input.close();
}catch(Exception ex){
System.out.println("File has error!");
}
}

/**
* 设置密钥值
* @param key-密钥
*/
public void setKey(String key){
this.key = key;
}

/**
* 返回密钥值
* @return 密钥字符串
*/
public String getKey(){
return key;
}

/**
* 设置原文信息
* @param source-原文信息
*/
public void setSource(String source){
this.source = source;
}

/**
* 返回源文信息
* @return 原文字符串
*/
public String getSource(){
return source;
}

/**
* 设置密文信息
* @param cryptograph-密文信息
*/
public void setCryptograph(byte[] cryptograph){
this.cryptograph = cryptograph;
}

/**
* 返回密文信息
* @return 密文
*/
public byte[] getCryptograph(){
return cryptograph;
}

待续


(二)接上

/**
* DES加密算法。使用之前要定义好密钥和原文信息。
* @return 结果字符串
* @exception 异常则返回“DES encode error”及出错原因
*/
public String DESEncoder(){
String str = "";
if(source.length() < 1 || source == null){
str = "DES encode error.原文信息是空!";
}
else if(key.length() < 1){
str = "DES encode error.密钥是空!";
}
else if(key.length() < 8){
str = "DES encode error.密钥至少为8位!";
}
else{
try{
// 通过设置的KEY生成密钥
byte[] keyByte = key.getBytes();
DESKeySpec keySpec = new DESKeySpec(keyByte);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
SecretKey desKey = keyFactory.generateSecret(keySpec);

// 加密
Cipher cp = Cipher.getInstance(ALGORITHM);
cp.init(Cipher.ENCRYPT_MODE,desKey);
this.cryptograph = cp.doFinal(source.getBytes());

str = "OK";
}catch(Exception ex){
str = "DES encode error."+ex.getMessage();
}
}
return str;
}

/**
* DES加密算法。使用之前要定义好密钥。
* @param source-原文信息
* @return 结果字符串
* @exception 异常则返回“DES encode error”及出错原因
*/
public String DESEncoder(String source){
String str = "";
if(source.length() < 1 || source == null){
str = "DES encode error.原文信息是空!";
}
else if(key.length() < 1){
str = "DES encode error.密钥是空!";
}
else if(key.length() < 8){
str = "DES encode error.密钥至少为8位!";
}
else{
try{
// 通过设置的KEY生成密钥
byte[] keyByte = key.getBytes();
DESKeySpec keySpec = new DESKeySpec(keyByte);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
SecretKey desKey = keyFactory.generateSecret(keySpec);

// 加密
Cipher cp = Cipher.getInstance(ALGORITHM);
cp.init(Cipher.ENCRYPT_MODE,desKey);
this.cryptograph = cp.doFinal(source.getBytes());

str = "OK";
}catch(Exception ex){
str = "DES encode error."+ex.getMessage();
}
}
return str;
}

/**
* DES加密算法。
* @param source-原文信息
* @param key-密钥
* @return 结果字符串
* @exception 异常则返回“DES encode error”及出错原因
*/
public String DESEncoder(String source,String key){
String str = "";
if(source.length() < 1 || source == null){
str = "DES encode error.原文信息是空!";
}
else if(key.length() < 1){
str = "DES encode error.密钥是空!";
}
else if(key.length() < 8){
str = "DES encode error.密钥至少为8位!";
}
else{
try{
// 通过设置的KEY生成密钥
byte[] keyByte = key.getBytes();
DESKeySpec keySpec = new DESKeySpec(keyByte);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
SecretKey desKey = keyFactory.generateSecret(keySpec);

// 加密
Cipher cp = Cipher.getInstance(ALGORITHM);
cp.init(Cipher.ENCRYPT_MODE,desKey);
this.cryptograph = cp.doFinal(source.getBytes());

str = "OK";
}catch(Exception ex){
str = "DES encode error."+ex.getMessage();
}
}
return str;
}

/**
* DES解密算法。使用之前要定义好密钥和密文信息。
* @return 结果字符串
* @exception 异常则返回“DES decode error”及出错原因
*/
public String DESDecoder(){
String str = "";
if(cryptograph.length < 1){
str = "DES decode error.密文信息是空!";
}
else if(key.length() < 1){
str = "DES decode error.密钥是空!";
}
else if(key.length() < 8){
str = "DES decode error.密钥至少为8位!";
}
else{
try{
// 通过设置的KEY生成密钥
byte[] keyByte = key.getBytes();
DESKeySpec keySpec = new DESKeySpec(keyByte);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
SecretKey desKey = keyFactory.generateSecret(keySpec);

//DES解密
Cipher c1 = Cipher.getInstance(ALGORITHM);
c1.init(Cipher.DECRYPT_MODE,desKey);
byte[] deByte=c1.doFinal(cryptograph);
this.source = new String(deByte,"UTF-8");

str = "OK";
}catch(Exception ex){
str = "DES decode error."+ex.getMessage();
}
}
return str;
}

/**
* DES解密算法。使用之前要定义好密钥。
* @param cryptograph-密文信息
* @return 结果字符串
* @exception 异常则返回“DES decode error”及出错原因
*/
public String DESDecoder(byte[] cryptograph){
String str = "";
if(cryptograph.length < 1){
str = "DES decode error.密文信息是空!";
}
else if(key.length() < 1){
str = "DES decode error.密钥是空!";
}
else if(key.length() < 8){
str = "DES decode error.密钥至少为8位!";
}
else{
try{
// 通过设置的KEY生成密钥
byte[] keyByte = key.getBytes();
DESKeySpec keySpec = new DESKeySpec(keyByte);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
SecretKey desKey = keyFactory.generateSecret(keySpec);

//DES解密
Cipher c1 = Cipher.getInstance(ALGORITHM);
c1.init(Cipher.DECRYPT_MODE,desKey);
byte[] deByte=c1.doFinal(cryptograph);
this.source = new String(deByte,"UTF-8");

str = "OK";
}catch(Exception ex){
str = "DES decode error."+ex.getMessage();
}
}
return str;
}

/**
* DES解密算法。
* @param cryptograph-密文信息
* @param key -密钥
* @return 结果字符串
* @exception 异常则返回“DES decode error”及出错原因
*/
public String DESDecoder(byte[] cryptograph,String key){
String str = "";
if(cryptograph.length < 1){
str = "DES decode error.密文信息是空!";
}
else if(key.length() < 1){
str = "DES decode error.密钥是空!";
}
else if(key.length() < 8){
str = "DES decode error.密钥至少为8位!";
}
else{
try{
// 通过设置的KEY生成密钥
byte[] keyByte = key.getBytes();
DESKeySpec keySpec = new DESKeySpec(keyByte);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
SecretKey desKey = keyFactory.generateSecret(keySpec);

//DES解密
Cipher c1 = Cipher.getInstance(ALGORITHM);
c1.init(Cipher.DECRYPT_MODE,desKey);
byte[] deByte=c1.doFinal(cryptograph);
this.source = new String(deByte,"UTF-8");

str = "OK";
}catch(Exception ex){
str = "DES decode error."+ex.getMessage();
}
}
return str;
}
}


谢谢大家帮忙,问题现在解决了.马上结贴


↑返回目录
前一篇: javascript的一个应用问题。
后一篇: 后台运行程序,怎样才能在前台显示运行的进度条。。。。。