看看这个吧,希望能对你有所帮助。import java.security.*;import javax.crypto.*;
import javax.crypto.spec.*;import java.util.*;
import java.io.*;
public class DES {
/**
* DES 加密算法
* @param data 处理数据
* @param key 密钥
* @param mode =0加密 =1解密
*/
public static String des(String data,String key,int mode)
{
if(mode == 0 )
{
mode = Cipher.ENCRYPT_MODE;
}else{
mode = Cipher.DECRYPT_MODE;
}
try{
Cipher cipher = Cipher.getInstance("DES");
SecretKeySpec spec = new SecretKeySpec(key.getBytes(),"DES");
Key deskey = spec;
cipher.init(mode, deskey);
return crypt(new ByteArrayInputStream(data.getBytes()),cipher);
}catch(Exception e)
{
e.printStackTrace();
return "";
}
}
private static String crypt(InputStream in,Cipher cipher)throws IOException, GeneralSecurityException
{
int blockSize = cipher.getBlockSize();
int outputSize = cipher.getOutputSize(blockSize);
byte[] inBytes = new byte[blockSize];
byte[] outBytes = new byte[outputSize];
ByteArrayOutputStream out = new ByteArrayOutputStream();
int inLength = 0;
boolean more = true;
while (more)
{
inLength = in.read(inBytes);
if (inLength == blockSize)
{
int outLength
= cipher.update(inBytes, 0, blockSize, outBytes);
out.write(outBytes, 0, outLength);
System.out.println(outLength);
}
else more = false;
}
if (inLength > 0)
outBytes = cipher.doFinal(inBytes, 0, inLength);
else
outBytes = cipher.doFinal();
System.out.println(outBytes.length);
out.write(outBytes);
return new String(out.toByteArray());
}
public static void main(String[] args) {
DES des = new DES();
System.out.println(des.des("123","wer",1));
}
}
import javax.crypto.spec.*;import java.util.*;
import java.io.*;
public class DES {
/**
* DES 加密算法
* @param data 处理数据
* @param key 密钥
* @param mode =0加密 =1解密
*/
public static String des(String data,String key,int mode)
{
if(mode == 0 )
{
mode = Cipher.ENCRYPT_MODE;
}else{
mode = Cipher.DECRYPT_MODE;
}
try{
Cipher cipher = Cipher.getInstance("DES");
SecretKeySpec spec = new SecretKeySpec(key.getBytes(),"DES");
Key deskey = spec;
cipher.init(mode, deskey);
return crypt(new ByteArrayInputStream(data.getBytes()),cipher);
}catch(Exception e)
{
e.printStackTrace();
return "";
}
}
private static String crypt(InputStream in,Cipher cipher)throws IOException, GeneralSecurityException
{
int blockSize = cipher.getBlockSize();
int outputSize = cipher.getOutputSize(blockSize);
byte[] inBytes = new byte[blockSize];
byte[] outBytes = new byte[outputSize];
ByteArrayOutputStream out = new ByteArrayOutputStream();
int inLength = 0;
boolean more = true;
while (more)
{
inLength = in.read(inBytes);
if (inLength == blockSize)
{
int outLength
= cipher.update(inBytes, 0, blockSize, outBytes);
out.write(outBytes, 0, outLength);
System.out.println(outLength);
}
else more = false;
}
if (inLength > 0)
outBytes = cipher.doFinal(inBytes, 0, inLength);
else
outBytes = cipher.doFinal();
System.out.println(outBytes.length);
out.write(outBytes);
return new String(out.toByteArray());
}
public static void main(String[] args) {
DES des = new DES();
System.out.println(des.des("123","wer",1));
}
}
解决方案 »
- 求高手指点,在线等~~~~~~~
- 一个java导出数据的小问题,希望高手解决!
- 我在windows2000中map了一个网络的地址到E:盘,有没有办法用java把这个路径转化为网络路径啊!
- 求教高手,关于二进制的操作
- could you help me?高分求助
- 心情不错,升五个三角,散分庆祝!
- 帮忙,我的IE被恶意改掉了,重装了也没用
- 請問,哪位大俠知道msdn.microsoft.com/code/default.asp里的樹形菜單是怎么做的呀,哪里有它的原碼下載啊!
- 谁能给我介绍一下JBuilder?
- 请教JAVA小问题:如何将含数字的String类型的对象转化为int或long类型
- 新手求助:J2SE1.5中java源程序的编译命令是什么?
- 问一个低级的概念
DES des = new DES();
String testStr = des.des("中国", "12345678", 0);
String returnStr = des.des(testStr, "12345678", 1);
System.out.println(returnStr);
测试出错。
楼主看下这个贴子吧
http://community.csdn.net/Expert/topic/3925/3925725.xml?temp=7.879275E-02