Y
yuanzi
Unregistered / Unconfirmed
GUEST, unregistred user!
下面是两个测试程序,(均是从网上找到的,^_^)。
测试程序一:
package encryption;
/**
* Title:
* Description:
* Copyright: Copyright (c) 2001
* Company:
* @author
* @version 1.0
*/
import java.io.*;
import java.math.BigInteger;
import java.security.*;
import java.security.spec.*;
import java.security.interfaces.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import javax.crypto.interfaces.*;
public class En1 {
public En1() {
KeyGenerator keygen;
SecretKey desKey;
Cipher desCipher;
try{
//DES,DESede,PBEWithMD5AndDES,Blowfish
//Generating a Key
// keygen = KeyGenerator.getInstance("DES");
// keygen = KeyGenerator.getInstance("DESede");
keygen = KeyGenerator.getInstance("Blowfish");
desKey = keygen.generateKey();
System.out.println(
desKey.getEncoded().toString()
);
// Create the cipher
// desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
// desCipher = Cipher.getInstance("DESede");
desCipher = Cipher.getInstance("Blowfish");
// Our cleartext
byte[] cleartext = "This is just an example:邢战军".getBytes();
StringBuffer StrBuf = new StringBuffer();
for(int i=0,j=cleartext.length;i<j;i++)
{
StrBuf.append(String.valueOf((char)cleartext));
}
System.out.println(" ");
System.out.println(StrBuf.toString());
//Initialize the cipher for encryption
desCipher.init(Cipher.ENCRYPT_MODE,desKey);
// Encrypt the cleartext
StrBuf = new StringBuffer();
byte[] ciphertext = desCipher.doFinal(cleartext);
// System.out.println(ciphertext.toString());
for(int i=0,j=ciphertext.length;i<j;i++)
{
StrBuf.append(String.valueOf((char)ciphertext));
}
System.out.println(" ");
System.out.println("Encrypt the cleartext");
System.out.println(StrBuf.toString());
SecretKeySpec seckey = new javax.crypto.spec.SecretKeySpec(
desKey.getEncoded(),"Blowfish");
System.out.println(
seckey.getEncoded()
);
seckey = new javax.crypto.spec.SecretKeySpec(
seckey.getEncoded(),"Blowfish");
System.out.println(
seckey.getEncoded()
);
// Initialize the same cipher for decryption
// desCipher.init(Cipher.DECRYPT_MODE, desKey);
desCipher.init(Cipher.DECRYPT_MODE, seckey);
// Decrypt the ciphertext
StrBuf = new StringBuffer();
byte[] cleartext1 = desCipher.doFinal(ciphertext);
// System.out.println(cleartext1.toString());
for(int i=0,j=cleartext1.length;i<j;i++)
{
StrBuf.append(String.valueOf((char)cleartext1));
}
System.out.println(" ");
System.out.println("Decrypt the ciphertext");
System.out.println(StrBuf.toString());
}
catch(java.security.InvalidKeyException e2)
{
e2.printStackTrace();
}
catch(java.security.NoSuchAlgorithmException e)
{
e.printStackTrace();
}
catch(javax.crypto.NoSuchPaddingException e1)
{
e1.printStackTrace();
}
catch(javax.crypto.BadPaddingException e3)
{
e3.printStackTrace();
}
catch(javax.crypto.IllegalBlockSizeException e4)
{
e4.printStackTrace();
}
}
public static void main(String[] args) {
En1 en11 = new En1();
}
}
测试程序二:作者:王辉 (ddxxkk@21cn.com)
/*
安全程序 DESede/DES测试
*/
import java.security.*;
import javax.crypto.*;
public class testdes {
public static void main(String[] args){
testdes my=new testdes();
my.run();
}
public void run() {
//添加新安全算法,如果用JCE就要把它添加进去
Security.addProvider(new com.sun.crypto.provider.SunJCE());
String Algorithm="DES";
//定义 加密算法,可用 DES,DESede,Blowfish
String myinfo="要加密的信息";
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());
System.out.println("加密后的二进串:"+byte2hex(cipherByte));
//解密
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();}
}
public 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();
}
}
我下载了JCE1.2.1版本,导入VAJ3.5,编译通过,遗憾的是上面两个程序运行时都报这
种错误:
Thread[main,5,main](活动):未捕捉到的异常(java.lang.ExceptionInInitializerError)
Thread[Reference Handler,10,system](守护程序活动)
通过分段测试和所报的异常来看,可能是在
keygen = KeyGenerator.getInstance("Blowfish");这个地方就开始出错了。
于是怀疑是JCE有问题:可是在jdk1.4下编译通过,运行时报这种错误:
Exception in thread "main" java.lang.NoClassDefFoundError: d:/En1
不知道是怎么回事了麻烦大家帮我看看这是怎么回事,
其实,我的需求很简单,就是对一个字符串进行加密,得到一段密文,然后对这短密
文可以进行解密,这个串可能很大,密文可能会在WEB浏览器上显示。帮帮忙,提示
得详细一点。
测试程序一:
package encryption;
/**
* Title:
* Description:
* Copyright: Copyright (c) 2001
* Company:
* @author
* @version 1.0
*/
import java.io.*;
import java.math.BigInteger;
import java.security.*;
import java.security.spec.*;
import java.security.interfaces.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import javax.crypto.interfaces.*;
public class En1 {
public En1() {
KeyGenerator keygen;
SecretKey desKey;
Cipher desCipher;
try{
//DES,DESede,PBEWithMD5AndDES,Blowfish
//Generating a Key
// keygen = KeyGenerator.getInstance("DES");
// keygen = KeyGenerator.getInstance("DESede");
keygen = KeyGenerator.getInstance("Blowfish");
desKey = keygen.generateKey();
System.out.println(
desKey.getEncoded().toString()
);
// Create the cipher
// desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
// desCipher = Cipher.getInstance("DESede");
desCipher = Cipher.getInstance("Blowfish");
// Our cleartext
byte[] cleartext = "This is just an example:邢战军".getBytes();
StringBuffer StrBuf = new StringBuffer();
for(int i=0,j=cleartext.length;i<j;i++)
{
StrBuf.append(String.valueOf((char)cleartext));
}
System.out.println(" ");
System.out.println(StrBuf.toString());
//Initialize the cipher for encryption
desCipher.init(Cipher.ENCRYPT_MODE,desKey);
// Encrypt the cleartext
StrBuf = new StringBuffer();
byte[] ciphertext = desCipher.doFinal(cleartext);
// System.out.println(ciphertext.toString());
for(int i=0,j=ciphertext.length;i<j;i++)
{
StrBuf.append(String.valueOf((char)ciphertext));
}
System.out.println(" ");
System.out.println("Encrypt the cleartext");
System.out.println(StrBuf.toString());
SecretKeySpec seckey = new javax.crypto.spec.SecretKeySpec(
desKey.getEncoded(),"Blowfish");
System.out.println(
seckey.getEncoded()
);
seckey = new javax.crypto.spec.SecretKeySpec(
seckey.getEncoded(),"Blowfish");
System.out.println(
seckey.getEncoded()
);
// Initialize the same cipher for decryption
// desCipher.init(Cipher.DECRYPT_MODE, desKey);
desCipher.init(Cipher.DECRYPT_MODE, seckey);
// Decrypt the ciphertext
StrBuf = new StringBuffer();
byte[] cleartext1 = desCipher.doFinal(ciphertext);
// System.out.println(cleartext1.toString());
for(int i=0,j=cleartext1.length;i<j;i++)
{
StrBuf.append(String.valueOf((char)cleartext1));
}
System.out.println(" ");
System.out.println("Decrypt the ciphertext");
System.out.println(StrBuf.toString());
}
catch(java.security.InvalidKeyException e2)
{
e2.printStackTrace();
}
catch(java.security.NoSuchAlgorithmException e)
{
e.printStackTrace();
}
catch(javax.crypto.NoSuchPaddingException e1)
{
e1.printStackTrace();
}
catch(javax.crypto.BadPaddingException e3)
{
e3.printStackTrace();
}
catch(javax.crypto.IllegalBlockSizeException e4)
{
e4.printStackTrace();
}
}
public static void main(String[] args) {
En1 en11 = new En1();
}
}
测试程序二:作者:王辉 (ddxxkk@21cn.com)
/*
安全程序 DESede/DES测试
*/
import java.security.*;
import javax.crypto.*;
public class testdes {
public static void main(String[] args){
testdes my=new testdes();
my.run();
}
public void run() {
//添加新安全算法,如果用JCE就要把它添加进去
Security.addProvider(new com.sun.crypto.provider.SunJCE());
String Algorithm="DES";
//定义 加密算法,可用 DES,DESede,Blowfish
String myinfo="要加密的信息";
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());
System.out.println("加密后的二进串:"+byte2hex(cipherByte));
//解密
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();}
}
public 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();
}
}
我下载了JCE1.2.1版本,导入VAJ3.5,编译通过,遗憾的是上面两个程序运行时都报这
种错误:
Thread[main,5,main](活动):未捕捉到的异常(java.lang.ExceptionInInitializerError)
Thread[Reference Handler,10,system](守护程序活动)
通过分段测试和所报的异常来看,可能是在
keygen = KeyGenerator.getInstance("Blowfish");这个地方就开始出错了。
于是怀疑是JCE有问题:可是在jdk1.4下编译通过,运行时报这种错误:
Exception in thread "main" java.lang.NoClassDefFoundError: d:/En1
不知道是怎么回事了麻烦大家帮我看看这是怎么回事,
其实,我的需求很简单,就是对一个字符串进行加密,得到一段密文,然后对这短密
文可以进行解密,这个串可能很大,密文可能会在WEB浏览器上显示。帮帮忙,提示
得详细一点。