加密解密一段字串
阿新 • • 發佈:2022-02-17
-
public class Mima {
/**
* 加密,把一個字串在原有的基礎上+1
* @param data 需要解密的原字串
* @return 返回解密後的新字串
*/
public static String encode(String data) {
//把字串轉為位元組陣列
byte[] b = data.getBytes();
//遍歷
for(int i=0;i<b.length;i++) {
b[i] += 1;//在原有的基礎上+1
}
return new String(b);
}
/**
* 解密:把一個加密後的字串在原有基礎上-1
* @param data 加密後的字串
* @return 返回解密後的新字串
*/
public static String decode(String data) {
//把字串轉為位元組陣列
byte[] b = data.getBytes();
//遍歷
for(int i=0;i<b.length;i++) {
b[i] -= 1;//在原有的基礎上-1
}
return new String(b);
}
public static void main(String[] args) {
String data = "i";
String result = encode(data);
System.out.println("加密後:"+result);
String str1 = decode(data);
System.out.println("解密後:"+str1);
}
} - 密碼加密後儲存,就不知道這是密碼了;