加密解密一段字符串

  • 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);
    }
    }
  • 密码加密后存储,就不知道这是密码了;
posted @ 2022-02-16 21:42  subtlman  阅读(86)  评论(1)    收藏  举报