我有一DES解密程序,当我们解密时需要从加密后的2进制信息进行解密,而加密后的2进制信息是字符串形式,解密的doFinal方法却需要BYTE[]类型,怎样把字符串转化为BYTE[]类型呢?我用getBytes()方法试过却没有能把程序跑起来,哪位达人帮帮忙啊,教我怎样把字符串转化为BYTE[]/*
安全程序 DESede/DES测试
*/
import java.security.*;
import javax.crypto.*;
public class jiami {
public static void main(String[] args){
jiami my=new jiami();
my.run();
}
public void run() {
// 添加新安全算法,如果用JCE就要把它添加进去
Security.addProvider(new com.sun.crypto.provider.SunJCE());
String Algorithm="DES"; //定义 加密算法,可用 DES,DESede,Blowfish
String myinfo="hello my baby";
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());
String str = byte2hex(cipherByte);
System.out.println("str:"+str.substring(5,6)+"''''");
System.out.println("加密后的二进串:"+str);
Byte b = Byte.valueOf(str);
byte[] b2= str.getBytes();
String str2 = byte2hex(b2);
System.out.println("二进串:"+str2);
//解密
c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.DECRYPT_MODE,deskey);
byte[] clearByte=c1.doFinal( str.getBytes() );
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 jiami {
public static void main(String[] args){
jiami my=new jiami();
my.run();
}
public void run() {
// 添加新安全算法,如果用JCE就要把它添加进去
Security.addProvider(new com.sun.crypto.provider.SunJCE());
String Algorithm="DES"; //定义 加密算法,可用 DES,DESede,Blowfish
String myinfo="hello my baby";
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());
String str = byte2hex(cipherByte);
System.out.println("str:"+str.substring(5,6)+"''''");
System.out.println("加密后的二进串:"+str);
Byte b = Byte.valueOf(str);
byte[] b2= str.getBytes();
String str2 = byte2hex(b2);
System.out.println("二进串:"+str2);
//解密
c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.DECRYPT_MODE,deskey);
byte[] clearByte=c1.doFinal( str.getBytes() );
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();
} }
解决方案 »
- 想知道hashCode()的一些知识
- M000001到 M999999
- 求救!!如何通过点击按钮打开一个指定路径的*.TXT文件
- 怎么在创建一个Calendar类之后,可以使用之来获取当前的时间?
- 郁闷:刚毕业上班几个月就不爽了,难道那些有经验的程序员都是这样的!???大家发表哈意见???
- Swing用的真的好艰难啊!!! JTable里的数据排序后,添加删除一行后又是排序前的顺序了.
- java2se win2000professional 下安装问题
- 一个java 新手的问题,在线等待。
- 请教各位一个问题
- Just in time?
- 求救:如何在java中调用SQLServer中的bcp这个工具啊?
- 初学JAVA,该往哪个方向走??
Byte[] b = new Byte[a.length];
for(int i = 0; i < a.length; ++i) {
b[i] = new Byte(a[i]);
}
byte[] clearByte=c1.doFinal( cipherByte );如上修改后,这个程序可以通过,完成加解密:
/*
安全程序 DESede/DES测试
*/
import java.security.*;
import javax.crypto.*;
public class jiami {
public static void main(String[] args){
jiami my=new jiami();
my.run();
}
public void run() {
// 添加新安全算法,如果用JCE就要把它添加进去
Security.addProvider(new com.sun.crypto.provider.SunJCE());
String Algorithm="DES"; //定义 加密算法,可用 DES,DESede,Blowfish
String myinfo="hello my baby";
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());
String str = byte2hex(cipherByte);
System.out.println("str:"+str.substring(5,6)+"''''");
System.out.println("加密后的二进串:"+str);
//Byte b = Byte.valueOf(str);
byte[] b2= str.getBytes();
String str2 = byte2hex(b2);
System.out.println("二进串:"+str2);
//解密
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();
} }
04:37:3C:CD:8F:40:3A:3F:F3:38:31:40:D3:F9:97:6C:32:6B:B9:F0:B8:A8:49:C5:06:6F:C8:C1:E9:84:D7:69:74:23:B7:CD:53:67:40:77:2E:6D:64:E3:1B:7A:9E:F1:28:F2:4E:CF:3B:67:B7:48:AF:75:64:41:50:40:4F:88:FC:5A:1B:5D:FF:CB:77:22:59:34:87:13:64:8F:C3:D2:0B:2A:AB:27:9C:9E:60:79:FC:5A:1B:5D:FF:CB:77:22:4B:B1:92:09:E6:73:CC:43:5A:49:2C:05:61:FF:68:9A:A0:61:4A:53:B6:21:7C:64:DD:5C:16:27:78:C2:B3:E8:F4:36:3E:4F:13:07:FD:BF:D4:E5:45:E7:61:21:C3:87:66:69:52:BA:D9:9B:4D:0D:C7:F8:B6:1E:F5:03:92:9A:43:BD:38:A6:00:BF:D2:06:20:B2:01:09:5C:54:39:F1:0C:6E:43:BD:2D:33:D7:DB:C4:88:BD:87:64:93:26:DF:51:1B:4D:B3:B5:B1:1F:D3:8E:62:5F:41:2B:56:59:7B:29:B2:EB:8B:14:36:2C:8E:69:8F:69:59:17:50:E1:E1:E9:4B:77:FE:2E:D1:6B:67:06:20:93:19:0A:55:1E:15:E3:A9:69:F4:15:2D:17:5E:0F:20:A5:57:5F:56:8A:B7:4E:17:82:21:C1:BA:E3:4F
写入了文本文件,解密的时候我从文本文件读出的是这样的字符串啊.必须要转化为byte[] 啊.否则以上程序的cipherByte是无法获得的
加密前: "abc", 加密后: ???, 送给doFinal():???
String[] d = "C3:D2:0B:2A:AB:27".split(":");
byte[] data = new byte[d.length];
for(int i = 0; i < d.length; ++i) {
data[i] = Byte.parseByte(d[i],16);
}
有一个字符串加密前是"hello world"我将它赋给newstr, 然后我对newstr进行了newstr.getBytes().
此刻就将它转化为加密前的2进制串
68:65:6C:6C:6F:20:77:6F:72:6C:64
然后我对它进行加密
Cipher c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.ENCRYPT_MODE,deskey);
byte[] cipherByte=c1.doFinal(newstr.getBytes());
得到了加密后的2进制串 B8:BA:02:E3:FD:DE:1B:8D:9D:8C:F7:E5:E2:23:96:FE
我将它存到了文本文件.文本文件里的信息就是B8:BA:02:E3:FD:DE:1B:8D:9D:8C:F7:E5:E2:23:96:FE解密的时候我读出B8:BA:02:E3:FD:DE:1B:8D:9D:8C:F7:E5:E2:23:96:FE这个字符串,将它赋给oldstr
而解密的方法却是
c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.DECRYPT_MODE,deskey);
byte[] clearByte=c1.doFinal( cipherByte );
其中 cipherByte 是byte[]形式,
其实oldstr就是cipherByte用我上面的byte2hex(byte[] b)这个方法将2进制转化为字符串类型得来的.
但要想解密,需要的是将oldstr重新转化为byte[]类型,因为doFinal支持byte[]类型.大哥们,拜托帮我想想啦
at java.lang.Byte.parseByte(Unknown Source)
at com.mybussiness.common.bytestr.main(bytestr.java:39)报错
if (v > 127) //这里的算法不知是否对,要查查书了. :-)
v=127-v;
data[i] = (byte)v;
import java.security.*;
import javax.crypto.*;public class jiami { public static void main(String[] args) { jiami my = new jiami ();
my.run ();
} public void run() { // 添加新安全算法,如果用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 (); Cipher c1 = Cipher.getInstance (Algorithm);
c1.init (Cipher.ENCRYPT_MODE, deskey);
FileInputStream in = new FileInputStream ("c:\\12.jlm");
byte b[] = new byte[in.available ()];
in.read (b); String str = byte2hex (b);
System.out.println ("加密后的二进串:" + str); System.out.println ("---------------------"); //解密
c1 = Cipher.getInstance (Algorithm);
c1.init (Cipher.DECRYPT_MODE, deskey);
byte[] clearByte = c1.doFinal (b);
String dd = new String (clearByte);
System.out.println ("解密后的信息:" + dd); }
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 ();
}}
大虾们,为什么它在 byte[] clearByte = c1.doFinal (b);这句报 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(DashoA12275)
at com.tengtu.license.jiami.run(jiami.java:114)
at com.tengtu.license.jiami.main(jiami.java:44)
的错误啊