2 dl0246 dl0246 于 2017.09.13 22:36 提问

考考你的Coding思维,如下编码(加密)代码的解码(解密)代码是?

考考你的Coding思维,如下编码(加密)代码的解码(解密)代码是?

      /**
       * 简单的字符串编码
       **/
      public static String encode(String ss,String enc)
      {
        StringBuffer sb=new StringBuffer("");
        int x=0;
        for (int i=0;i<ss.length();i++)
        {
          int z=ss.charAt(i)^enc.charAt(x);
          x++;
          if (x>=enc.length()) x=0;
          if (i>0) sb.append('|');
          sb.append(Long.toHexString(z));
        }
        return sb.toString();
      }

1个回答

out_of_it_farmer
out_of_it_farmer   2017.09.14 11:29
    public static String decode(String ss, String enc) {
        if (ss == null) return null;
        StringBuilder sb = new StringBuilder();
        int x = 0;
        try {
            String[] split = ss.split("\\|");
            for (int j = 0; j < split.length; j++) {
                String s = split[j];
                if (s.length() == 0) break;
                int z = Long.decode("#" + s).intValue() ^ enc.charAt(x);
                x++;
                if (x >= enc.length()) x = 0;
                sb.append((char) z);
            }
        } catch (Exception e) {
            System.err.print("Exception!");
        }
        return sb.toString();
    }

How many points to give?LOL -_-

Csdn user default icon
上传中...
上传图片
插入图片