我在网上摘抄了一段关于DES加密算法的程序,运行正常,如下,但现在我想将其添加一个加密函数和一个解密函数,原型为:
public String encrypt(String encryptStr);
public String decrypt(String decryptStr);
请问怎样写这两个函数???/*
安全程序 DESede/DES测试
*/
import java.security.*;
import javax.crypto.*;public class testDES
{
public static void main(String[] args)
{
testDES my = new testDES();
my.run("要加密的信息");
} public String encrypt(String encryptStr)
{
} public String decrypt(String decryptStr)
{
}
public void run(String willDES)
{
//添加新安全算法,如果用JCE就要把它添加进去
Security.addProvider(new com.sun.crypto.provider.SunJCE());
String Algorithm = "DES"; //定义 加密算法,可用 DES,DESede,Blowfish
try
{
//生成密钥
KeyGenerator keygen = KeyGenerator.getInstance(Algorithm);
SecretKey deskey = keygen.generateKey();
//加密
System.out.println("加密前的二进串:" + byte2hex(willDES.getBytes()));
System.out.println("加密前的信息:" + willDES);
Cipher c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.ENCRYPT_MODE, deskey);
byte[] cipherByte = c1.doFinal(willDES.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();
}
}
private 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();
}
}
public String encrypt(String encryptStr);
public String decrypt(String decryptStr);
请问怎样写这两个函数???/*
安全程序 DESede/DES测试
*/
import java.security.*;
import javax.crypto.*;public class testDES
{
public static void main(String[] args)
{
testDES my = new testDES();
my.run("要加密的信息");
} public String encrypt(String encryptStr)
{
} public String decrypt(String decryptStr)
{
}
public void run(String willDES)
{
//添加新安全算法,如果用JCE就要把它添加进去
Security.addProvider(new com.sun.crypto.provider.SunJCE());
String Algorithm = "DES"; //定义 加密算法,可用 DES,DESede,Blowfish
try
{
//生成密钥
KeyGenerator keygen = KeyGenerator.getInstance(Algorithm);
SecretKey deskey = keygen.generateKey();
//加密
System.out.println("加密前的二进串:" + byte2hex(willDES.getBytes()));
System.out.println("加密前的信息:" + willDES);
Cipher c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.ENCRYPT_MODE, deskey);
byte[] cipherByte = c1.doFinal(willDES.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();
}
}
private 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();
}
}
解决方案 »
- swt调用Browser的疑问求助,谢谢
- 请问 这里 @version %I%, %G% 的 %I%, %G% 是什么意思啊?
- 关于翻译api方面的
- 在java1.6 JComboBox里只有一个元素的时候 为什么选中唯一的元素时ActionListener不响应呢? 如果有2个或2个以上元素选其中任何一个就可
- JTable如何在一列中显示多种组件
- 什么是回调函数?
- public static lockobject=new Object()为什么编译不过?
- 如何做一个界面的引导窗口??没有title的窗体,比如JB一开始的画面或者“请等待”那种窗体!
- !有没有谁在java中用acrobat view(for java)来打开PDF文件,中文问题请教
- java.awt 和java.util都有List,想调用awt的List而出现Ambiguous class错误怎么办?
- 如何在JTextField 或 JTextArea 中实现这样的功能?
- 回头看数据结构,写了个链表,...
{
private String algorithm; //加密算法
private KeyGenerator keygen; //密钥产生器
private SecretKey deskey; //密钥
public testDES (String algorithm) {
//添加新安全算法,如果用JCE就要把它添加进去
Security.addProvider(new com.sun.crypto.provider.SunJCE());
try {
this.keygen = KeyGenerator.getInstance(algorithm);
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.deskey = keygen.generateKey();
this.algorithm = algorithm;
}
public static void main(String[] args)
{
testDES my = new testDES("DES");
//my.run("要加密的信息");
String str = "要加密的信息";
byte[] makeCode = my.encrypt(str);
System.out.println("加密后的二进串:" + my.byte2hex(makeCode));
String breakCode = my.decrypt(makeCode);
System.out.println("解密后的信息:" + breakCode);
} public byte[] encrypt(String encryptStr)
{
byte[] cipherByte = null;
try {
Cipher c1 = Cipher.getInstance(algorithm);
c1.init(Cipher.ENCRYPT_MODE, this.deskey);
cipherByte = c1.doFinal(encryptStr.getBytes());
}
catch (java.security.NoSuchAlgorithmException e1)
{
e1.printStackTrace();
}
catch (javax.crypto.NoSuchPaddingException e2)
{
e2.printStackTrace();
}
catch (java.lang.Exception e3)
{
e3.printStackTrace();
}
return cipherByte;
}
//
public String decrypt(byte[] decryptBytes)
{
byte[] clearByte =null;
try{
Cipher c1 = Cipher.getInstance(algorithm);
c1 = Cipher.getInstance(algorithm);
c1.init(Cipher.DECRYPT_MODE,this.deskey);
clearByte = c1.doFinal(decryptBytes);
}
catch (java.security.NoSuchAlgorithmException e1)
{
e1.printStackTrace();
}
catch (javax.crypto.NoSuchPaddingException e2)
{
e2.printStackTrace();
}
catch (java.lang.Exception e3)
{
e3.printStackTrace();
}
return new String(clearByte);
}
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();
}
/**
* @return the algorithm
*/
public String getAlgorithm() {
return algorithm;
}
/**
* @param algorithm the algorithm to set
*/
public void setAlgorithm(String algorithm) {
this.algorithm = algorithm;
}
}
D:\JavaTest>java testDES
加密后的二进串:98:23:47:B7:EB:D2:84:B9:F4:E8:CD:6A:49:E3:FF:BB
javax.crypto.BadPaddingException: Given final block not properly padded
at com.sun.crypto.provider.SunJCE_h.b(DashoA6275)
at com.sun.crypto.provider.SunJCE_h.b(DashoA6275)
at com.sun.crypto.provider.DESCipher.engineDoFinal(DashoA6275)
at javax.crypto.Cipher.doFinal(DashoA6275)
at testDES.decrypt(testDES.java:106)
at testDES.main(testDES.java:15)
Exception in thread "main" java.lang.NullPointerException
at java.lang.String.<init>(Unknown Source)
at testDES.decrypt(testDES.java:120)
at testDES.main(testDES.java:15)
运行错误我就不知道了,我只能说我运行是对的