public String encryptString(String myinfo) {
//添加新安全算法,如果用JCE就要把它添加进去
Security.addProvider(new com.sun.crypto.provider.SunJCE());
String Algorithm="DES"; //定义 加密算法,可用 DES,DESede,Blowfish
String ss="";
try {
//生成密钥
KeyGenerator keygen = KeyGenerator.getInstance(Algorithm);
SecretKey deskey = keygen.generateKey();
//加密
// System.out.println("加密前的二进串:"+byte2hex(myinfo.getBytes()));
// System.out.println("加密前的信息:"+myinfo);
Cipher c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.ENCRYPT_MODE,deskey);
byte[] cipherByte=c1.doFinal(myinfo.getBytes());
ss=byte2hex(cipherByte);
// System.out.println("加密后的二进串:"+ss);
//解密
c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.DECRYPT_MODE,deskey);
byte[] clearByte=c1.doFinal(cipherByte);
// System.out.println("解密后的二进串:"+byte2hex(clearByte));
// System.out.println("解密后的信息:"+(new String(clearByte)));
}
catch (java.security.NoSuchAlgorithmException e1) {e1.printStackTrace();}
catch (javax.crypto.NoSuchPaddingException e2) {e2.printStackTrace();}
catch (java.lang.Exception e3) {e3.printStackTrace();}
return ss;
}
//添加新安全算法,如果用JCE就要把它添加进去
Security.addProvider(new com.sun.crypto.provider.SunJCE());
String Algorithm="DES"; //定义 加密算法,可用 DES,DESede,Blowfish
String ss="";
try {
//生成密钥
KeyGenerator keygen = KeyGenerator.getInstance(Algorithm);
SecretKey deskey = keygen.generateKey();
//加密
// System.out.println("加密前的二进串:"+byte2hex(myinfo.getBytes()));
// System.out.println("加密前的信息:"+myinfo);
Cipher c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.ENCRYPT_MODE,deskey);
byte[] cipherByte=c1.doFinal(myinfo.getBytes());
ss=byte2hex(cipherByte);
// System.out.println("加密后的二进串:"+ss);
//解密
c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.DECRYPT_MODE,deskey);
byte[] clearByte=c1.doFinal(cipherByte);
// System.out.println("解密后的二进串:"+byte2hex(clearByte));
// System.out.println("解密后的信息:"+(new String(clearByte)));
}
catch (java.security.NoSuchAlgorithmException e1) {e1.printStackTrace();}
catch (javax.crypto.NoSuchPaddingException e2) {e2.printStackTrace();}
catch (java.lang.Exception e3) {e3.printStackTrace();}
return ss;
}
解决方案 »
- java web
- 什么是jsp。
- js循环取checkbox中的value问题,请看写的代码,运行的时候有问题,请大伙帮忙看下,谢~
- urlrewritefilter 用了这个东西后为什么不能显示所有图片了.
- 自动插入表格,怎样定义插入个各列文字的颜色?
- [求助]有关select中onchange(和JSP,MYSQL有一定关系)
- 关于Eclipse3.1中使用JSF标签库时遇到的问题(使用myEclipse4.0GA)
- 如何将一个SQL结果集以文件的形式保存到客户机上?
- jsp后缀名大小写问题(急,在线等!)
- RSa.getString的中文显示问题(在线给分)
- servlet和applet的通信问题
- weblogic和IIS整合之后的servlet的问题。这都困扰我好多天了。
private static byte[] TrippleEncrypt(byte[] data,String key )
{
try{
System.out.println("加密前的二进串:"+byte2hex(data));
System.out.println("加密前的信息:"+new String(data)); byte[] rawkey = new sun.misc.BASE64Decoder().decodeBuffer(key);//这个地方重要
DESedeKeySpec keyspec = new DESedeKeySpec(rawkey);
SecretKeyFactory keyfactory = SecretKeyFactory.getInstance(algorithm);
Key enkey = keyfactory.generateSecret(keyspec);
Cipher c1 = Cipher.getInstance(algorithm);
//Cipher c1 = Cipher.getInstance("DESede/ECB/PKCS5Padding");
c1.init(Cipher.ENCRYPT_MODE,enkey);
byte[] cipherByte=c1.doFinal(data);
return cipherByte;
}
catch(Exception e)
{
return null;
}
}
import java.io.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import javax.crypto.spec.DESedeKeySpec;
import java.security.spec.KeySpec;
import java.security.spec.AlgorithmParameterSpec;
import java.security.*;public class DesEdeEncrypter
{
private static String Des = "Des";
private static String Desede = "DESede";
private static String DesBlowfish = "Blowfish";
private static String algorithm =Desede;
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();
} private static byte[] TrippleEncrypt(byte[] data,String key )
{
try{
System.out.println("加密前的二进串:"+byte2hex(data));
System.out.println("加密前的信息:"+new String(data)); //byte[] rawkey = key.getBytes();
byte[] rawkey = new sun.misc.BASE64Decoder().decodeBuffer(key);
DESedeKeySpec keyspec = new DESedeKeySpec(rawkey);
SecretKeyFactory keyfactory = SecretKeyFactory.getInstance(algorithm);
Key enkey = keyfactory.generateSecret(keyspec);
Cipher c1 = Cipher.getInstance(algorithm);
c1.init(Cipher.ENCRYPT_MODE,enkey);
byte[] cipherByte=c1.doFinal(data);
System.out.println("加密后的二进串:"+byte2hex(cipherByte));
return cipherByte;
}
catch(Exception e)
{
return null;
}
} private static byte[] TrippleDecrypt(byte[] data,String key )
{ try{
System.out.println("解密前的二进串:"+byte2hex(data)); //byte[] rawkey = key.getBytes();
byte[] rawkey = new sun.misc.BASE64Decoder().decodeBuffer(key);
DESedeKeySpec keyspec = new DESedeKeySpec(rawkey);
SecretKeyFactory keyfactory = SecretKeyFactory.getInstance(algorithm);
Key enkey = keyfactory.generateSecret(keyspec);
Cipher c1 = Cipher.getInstance(algorithm);
c1.init(Cipher.DECRYPT_MODE,enkey);
byte[] cipherByte=c1.doFinal(data);
System.out.println("解密后的二进串:"+byte2hex(cipherByte));
System.out.println("解密后的信息:"+(new String(cipherByte)));
return cipherByte;
}catch(Exception e){
return null;
}
}
public static void main(String[] args) {
String info = "9000$13013890874$100201$2004-04-13 10:40:22";
byte[] x = TrippleEncrypt(info.getBytes(),"cf7FjFMnafiiK40vnuTf9vsWnXpBo6qR");
//byte[] x = DESEncrypt(info.getBytes(),"cf7FjFMnafiiK40vnuTf9vsWnXpBo6qR");
System.out.print(new sun.misc.BASE64Encoder().encode(x));
byte[] y = TrippleDecrypt(x,"cf7FjFMnafiiK40vnuTf9vsWnXpBo6qR");
}
}