81,092
社区成员
发帖
与我相关
我的任务
分享
public static void main(String[] args) {
String str = "苹果";
System.out.println(encrypt(str));
System.out.println(decrypt(encrypt(str)));
}
//加密
private static String encrypt(String str){
byte[] b = null;
StringBuffer sb = new StringBuffer();
try {
b = str.getBytes("utf-8");
} catch (UnsupportedEncodingException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
for(byte bb : b){
sb.append(((int)bb + 256)+".");
}
return sb.toString().substring(0, sb.toString().lastIndexOf("."));
}
//解密
private static String decrypt(String num){
String str = null;
String[] bytes = num.split("\\.");
byte[] b = new byte[bytes.length];
for(int i=0;i<bytes.length;i++){
b[i] = (byte)(Integer.parseInt(bytes[i])-256);
}
try {
str = new String(b, "utf-8");
} catch (UnsupportedEncodingException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
return str;
}