byte[]数组和String的互换问题?为什么不能还原byte[]?????????

anruky 2004-12-06 05:46:17
String src = "2004-12-03";
byte[] s=src.getBytes();
byte[] des=encrypt(s);// 得到加密后数组
String sdes=new String(des); //将byte[]转换为string
s=sdes.getBytes(); //还原
for(int i=0;i<s.length;i++)
System.out.println((s[i]==des[i]));
运行结果为:
true;
true;
true;
true;
true;
true;
false;
false;
false;
为什么不能还原byte[]呢???应该如何才能还原?多谢
...全文
209 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
anruky 2004-12-07
  • 打赏
  • 举报
回复
to huhuanaiqing(呼唤爱情):我换了变量同样不对。

我再解释一下

我加密后得到一个byte数组,我想要把这个byte数组先转换为String来存档。
读的时候把string读出来,还原为byte,然后解密。
现在的问题是,byte1-〉String-〉byte2,还原后,byte2和byte1不相等了,不知道为什么?
这个过程和加密解密无关吧
yulchina 2004-12-07
  • 打赏
  • 举报
回复
楼上说的对!
fruitfull 2004-12-07
  • 打赏
  • 举报
回复
把s[i]和des[i]打印出来你就可以看到了!
huhuanaiqing 2004-12-07
  • 打赏
  • 举报
回复
你应该换一下变量s=sdes.getBytes(); ,可能变量s没清空!!
byte[] s1=sdes.getBytes();
whyxx 2004-12-07
  • 打赏
  • 举报
回复
你根本就没还原,当然不行了.如果加密后byte还和以前一样,那还加什么密啊.
anruky 2004-12-07
  • 打赏
  • 举报
回复
楼上各位大哥,你们也太不仔细了吧.加密只是为了产生一个源byte数组.比较和它没有任何关系.
chubbchubb 2004-12-07
  • 打赏
  • 举报
回复
哦,原来还是跟加密有关系,加密过的String,已经是byte了,用new String的时候,会根据默认的charset对其解码,这个byte里面应该有String无法表示的数据,这样得到的String是无法和byte等同的,因为有些东西没了。把这个String,再getBytes,也就和原来的不一样了

于是就。。。。

我是这样理解得了
zyg158 2004-12-07
  • 打赏
  • 举报
回复
zyg158 2004-12-07
  • 打赏
  • 举报
回复
同意anruky(anruky)的说法
我用DES加密后的byte数组比较也有这种情况,实在搞不清楚是怎么回事?
顶!!!

public class test000 {
public static void main(String[] args) {
DesAuth des = new DesAuth();
int[] f = { 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
int[] k = { 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
int[] m = des.Des(f,k,1);
byte[] before = new byte[8];
for(int i=0;i<8;i++)
before[i] = (byte)m[i];
String newA = new String(before);
byte[] after = newA.getBytes();
for(int i=0;i<after.length;i++){
System.out.println(before[i] == after[i]);
System.out.println("before: "+before[i]);
System.out.println("after: "+after[i]);
}
}
}

输出结果:
false
before: -60
after: 63
false
before: 50
after: 43
false
before: 43
after: -31
false
before: -31
after: -98
false
before: -98
after: -102
false
before: -102
after: 90
false
before: 90
after: 23
anruky 2004-12-07
  • 打赏
  • 举报
回复
to chubbchubb(长街) :
确实,这是一个非常奇怪的问题,一般的byte数组这样做的结果是相同的。我也试过,
但是如果byte数组是加密返回的byte就不对。我用的des加密。郁闷ing
chubbchubb 2004-12-07
  • 打赏
  • 举报
回复
public static void main(String[] args) {

String a = "2004-12-03";
byte[] before = a.getBytes();
String newA = new String (before);
byte[] after = newA.getBytes();
for(int i=0;i<before.length;i++){
System.out.println(before[i] == after[i]);
}

}
chubbchubb 2004-12-07
  • 打赏
  • 举报
回复
嗯?
我承认我看错了,s=sdes.getBytes();这一行给漏了。
我自己试了一下,没有false,不知道你的那个怎么出来的。

zyg158 2004-12-06
  • 打赏
  • 举报
回复
String src = "2004-12-03";
byte[] s=src.getBytes();
byte[] des=encrypt(s);// 得到加密后数组
String sdes=new String(des); //将byte[]转换为string
s=sdes.getBytes(); //还原
for(int i=0;i<s.length;i++)
System.out.println((s[i]==des[i]));
你加密了,当让不会一样,如果一样就奇怪了
加密,解密都执行后,再还原才能相同
chubbchubb 2004-12-06
  • 打赏
  • 举报
回复
encrypt()是做什么的?
经过这个,原来的字符串就比变了吧?

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧