package z1.sony;/** * @author kkk * * To change the template for this generated type comment go to * Window>Preferences>Java>Code Generation>Code and Comments */ public class TestDecode { public static void main(String[] args){ TestDecode td = new TestDecode(); td.decodeHex("31323334353637387172737475767778797a7b7c7d7e"); } private String decodeHex(String tmp) { StringBuffer sb = new StringBuffer(tmp); String[] strArrays = new String[sb.length() / 2]; String str[] = new String[sb.length() / 2]; StringBuffer tmpSb = new StringBuffer(); for (int i = 0; i < sb.length() / 2; i++) { strArrays[i] = sb.substring(2 * i, 2 * i + 2); String bb = Integer.valueOf(strArrays[i], 16).toString(); byte[] bytes = { new Byte(bb).byteValue()}; str[i] = new String(bytes); tmpSb.append(str[i]); } System.out.println(tmpSb); return tmpSb.toString(); } }
public static String generateKey(String info){
String hs="";
String stmp="";
byte[] b=info.getBytes();
for (int n=0;n<b.length;n++) {
System.out.println("b[" + n + "]:" + b[n] + ",Hex:" + Integer.toHexString(b[n]));
stmp=(java.lang.Integer.toHexString(b[n] & 0XFF));
System.out.println(stmp);
hs=hs+stmp;
}
return hs;
}
public static String recover(String info) { //还原
String result="";
String[] s=new String[info.length()/2];
for(int i=0;i<s.length;i++) {
s[i]=info.substring(i*2,i*2+2);
char b = (char)Integer.valueOf(s[i],16).intValue();
result+=b;
}
return result;
}
public static void main(String[] args) {
String out = generateKey("zxvcxnlihsd;afa'fkasdfkoeuA");
System.out.println(out + ", length:" + out.length());
System.out.println(recover(out));
}
}
String hs="";
String stmp="";
byte[] b=info.getBytes(); for (int n=0;n<b.length;n++)
{
stmp=(java.lang.Integer.toHexString(b[n] & 0XFF));
hs=hs+stmp;
System.out.println("k: " + stmp); }
return hs;
}
public static String crackKey(String hs) {
byte[] b = hs.getBytes();
String info = "";
for(int i=0;i<b.length;i=i+2)
{
byte[] bs = new byte[2];
bs[0] = b[i];
bs[1] = b[i+1];
System.out.println("c: " + new String(bs));
Integer temp = Integer.valueOf(new String(bs),16);
info += (new Character((char)temp.intValue())).charValue();
}
return info;
}public static void main(String[] args){
String t = "1234567890";
String m = Key.generateKey(t);
System.out.println("key :" + m);
System.out.println("carck:" + Key.crackKey(m));
}}
我测试通过了,但是没优化,自己干吧,呵呵
String hs="";
String stmp="";
byte[] b=info.getBytes(); for (int n=0;n<b.length;n++)
{
stmp=(java.lang.Integer.toHexString(b[n] & 0XFF));
hs=hs+stmp;
System.out.println("k: " + stmp); }
return hs;
}
public static String crackKey(String hs) {
byte[] b = hs.getBytes();
byte[] t = new byte[b.length/2];
String info = "";
for(int i=0;i<b.length;i=i+2)
{
byte[] bs = new byte[2];
bs[0] = b[i];
bs[1] = b[i+1];
System.out.println("c: " + new String(bs));
Integer temp = Integer.valueOf(new String(bs),16);
t[i/2] = temp.byteValue();
}
return new String(t);
}public static void main(String[] args){
String t = "ÎҵIJâÊÔ";
System.out.println(t);
String m = Key.generateKey(t);
System.out.println("key :" + m);
System.out.println("carck:" + Key.crackKey(m));
}}
一开始没有考虑中文问题,考虑不周啊,呵呵,现改之:public class GetKey {
public static String generateKey(String info){
String hs="";
String stmp="";
byte[] b=info.getBytes(); for (int n=0;n<b.length;n++) {
System.out.println("b[" + n + "]:" + b[n] + ",Hex:" + Integer.toHexString(b[n]));
stmp=(java.lang.Integer.toHexString(b[n] & 0XFF));
System.out.println(stmp);
hs=hs+stmp;
}
return hs;
}
public static String recover(String info) {
String[] s=new String[info.length()/2];
byte[] bt=new byte[info.length()/2];
for(int i=0;i<s.length;i++) {
s[i]=info.substring(i*2,i*2+2);
bt[i]=Integer.valueOf(s[i],16).byteValue(); //可以处理中文
}
return new String(bt);
}
public static void main(String[] args) {
String out = generateKey("新年快乐!");
System.out.println(out + ", length:" + out.length());
System.out.println(recover(out));
}
}
* @author kkk
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class TestDecode {
public static void main(String[] args){
TestDecode td = new TestDecode();
td.decodeHex("31323334353637387172737475767778797a7b7c7d7e");
}
private String decodeHex(String tmp) {
StringBuffer sb = new StringBuffer(tmp);
String[] strArrays = new String[sb.length() / 2];
String str[] = new String[sb.length() / 2];
StringBuffer tmpSb = new StringBuffer();
for (int i = 0; i < sb.length() / 2; i++) {
strArrays[i] = sb.substring(2 * i, 2 * i + 2);
String bb = Integer.valueOf(strArrays[i], 16).toString();
byte[] bytes = { new Byte(bb).byteValue()};
str[i] = new String(bytes);
tmpSb.append(str[i]);
}
System.out.println(tmpSb);
return tmpSb.toString();
}
}
客气了!呵呵:)