加密


此加密算法是Z变幻,然后我对该加密方法进行了改变和解析。下面的是一个完整的例子,您可以直接跑。

加密

此加密算法是Z变幻,然后我对该加密方法进行了改变和解析。

解密

 public String decode(String codeString) {
        /**
         * 解密规则:
         * 1.字符串倒数第3和第4位是字符串长度的标识位
         * 2.字符串倒数第1和第2位是字符串箱子大小的标识位
         */
        if (codeString == null || codeString.trim().length() <= 4) {
            throw new RRException("无效的请求");
        }
        int boxSize = 0;
        int length = 0;
        String strBoxSize = codeString.substring(codeString.length() - 2, codeString.length());
        String strLength = codeString.substring(codeString.length() - 4, codeString.length() - 2);
        try {
            boxSize = Integer.parseInt(strBoxSize);
            length = Integer.parseInt(strLength);
        } catch (Exception ex) {
            throw new RRException("无效的请求");
        }
        //长度不对 无效的请求
        if (codeString.length() != length + 4 || boxSize < 2) {
            throw new RRException("无效的请求");
        }
//        String sr=codeString.substring(0, codeString.length() - 4);
        return dConvert(codeString, boxSize);
    }
    /**
     * 加密解密逻辑完善
     * @param s2
     * @param numRows
     * @return
     */
    public static String dConvert(String s2, int numRows) {
        if (s2.length() <= numRows || numRows == 1)
            return s2;

        String s = s2.substring(0, s2.length() - 4);
        int index = 0;
        List<String> result = new ArrayList<>();
        for (int i = 0; i < s.length(); ++i) {
            result.add("");
        }
        for (int i = 0; i < numRows; ++i) {
            if (i == 0) { //读取第一行
                for (int j = 0; j < s.length(); j += 2 * numRows - 2) {
                    String tag = s.charAt(index) + "";
                    System.out.println(String.format("%d-%s-%d", index, tag, j));
                    result.set(j, tag);
                    index++;
                }
            } else if (i == numRows - 1) { //读取最后
                for (int j = numRows - 1; j < s.length(); j += 2 * numRows - 2) {
                    String tag = s.charAt(index) + "";
                    result.set(j, tag);
                    System.out.println(String.format("%d-%s-%d", index, tag, j));
                    index++;
                }
            } else { //读取中间行
                for (int j = i; j < s.length(); j += 2 * numRows - 2) {
                    // result.append(s.charAt(j));
                    String tag = s.charAt(index) + "";
                    result.set(j, tag);
                    System.out.println(String.format("%d-%s-%d", index, tag, j));
                    index = index + 1;
                    if (j + 2 * numRows - 2 - 2 * i < s.length()) {
                        int z = j + 2 * numRows - 2 - 2 * i;
                        String tag2 = s.charAt(index) + "";
                        result.set(z, tag2);
                        index++;
                    }

                }
            }
        }
        StringBuffer stringBuffer = new StringBuffer();
        result.forEach(s1 -> stringBuffer.append(s1));
        return stringBuffer.toString();
    }
    public static void main(String[] args) {
        VerificationServiceImple VerificationServiceImple = new VerificationServiceImple();
        //算法的最后一位是模
        System.out.println("原始串:" + "202004307552726016");
        String aa = VerificationServiceImple.convert("202004307552726016", 4);
        System.out.println("加密后:" + aa);
        String sou = VerificationServiceImple.dConvert(aa, 4);
        System.out.println("解密后:" + sou);
    }

解密

    /**
     * s实现对核销码的加密
     *
     * @param s
     * @param numRows 箱子的大小
     * @return 加密原理=原始码加密+原始码长度
     */
    public static String convert(String s, int numRows) {
        if (s.length() <= numRows || numRows == 1)
            return s;
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < numRows; ++i) {
            if (i == 0) { //读取第一行
                for (int j = 0; j < s.length(); j += 2 * numRows - 2)
                    result.append(s.charAt(j));
            } else if (i == numRows - 1) { //读取最后
                for (int j = numRows - 1; j < s.length(); j += 2 * numRows - 2)
                    result.append(s.charAt(j));
            } else { //读取中间行
                for (int j = i; j < s.length(); j += 2 * numRows - 2) {
                    result.append(s.charAt(j));
                    if (j + 2 * numRows - 2 - 2 * i < s.length())
                        result.append(s.charAt(j + 2 * numRows - 2 - 2 * i));
                }
            }
        }
        if (numRows < 10) {
            return result.toString() + result.length() + "0" + numRows;
        } else {
            //不考虑大于100
            return result.toString() + result.length() + "" + numRows;
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值