用到 JCE 1.2.1 我也是从别人那里抄来的。可以运行。package csdn;/*
安全程序 DESede/DES测试
*/
import java.security.*;
import javax.crypto.*;
public class MyClass4 {
public static void main(String[] args){
MyClass4 my=new MyClass4();
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();
}}
安全程序 DESede/DES测试
*/
import java.security.*;
import javax.crypto.*;
public class MyClass4 {
public static void main(String[] args){
MyClass4 my=new MyClass4();
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();
}}
解决方案 »
- 关于java位元数组和ByteArrayInputStream的两个小问题
- 那只有服务名SERVICE_NAME,没有sid怎么办?
- JAVA 小问题
- 呼救!关于UTF-8和GB23012之间的转换!!!
- 如何给窗口加载背景图片和用RGB值来给背景颜色
- short s1 = 1; s1 = s1 + 1;有什么错? short s1 = 1; s1 += 1;有什么错?
- 大家好,小弟有个关于HashMap的问题请教,多谢了
- 一JAVA笔试题,牛人都来哈!方面越简单人越牛。
- 求个算法:给定的多叉树 从中随机找出n个连续的节点
- 關於 prepareStatement 的一些簡單問題
- 急于寻求帮助
- 什么叫“删除一个元素,就必须移到另一个元素“?
我到sun准备下载。好像不然下载,免费的只有说明书。
同时看到说jsdk的标准版(jse:Standard Edition1.4)包含有改文件。
我用的就是jsdk1。4,
那为什么还会报错呢?
注:我编写的其他程序可以运行,classpath,path设置无误。