zhugewuhou 2016-08-18 07:29 采纳率: 0%
浏览 2257

Java编写数字加密解密

/*
整数加密/解密算法

设置一套针对非负整数的加密,解密算法

例如: 一个简单的映射算法,0->a,1->b, 2->c, 3->d, 4->e,5->f, 6->g, 7->h, 8->i,9->j
encode(2756)->”chfg”
decode(“bcea”)->1240

加分项:实现示例之外的其他加密/解密算法

//number>=0
public String encode(int number);

public int decode(String numberStr);

*/

  • 写回答

2条回答

  • qq_23660243 2016-08-18 08:25
    关注

    import java.util.HashMap;
    import java.util.Map;

    /**

    • Created by mahuichao on 16/8/18.
      */
      public class Test {
      public static final int number = 2756;
      public static final String str = "bcea";

      public static void main(String[] args) {

      String enStr = encode(number);
      System.out.println("加密的结果为:" + enStr);
      int deNum = decode(str);
      System.out.println("解密后的结果为:" + deNum);
      

      }

      public static String encode(int number) {
      Map map = encode_rules();

      String strNum = String.valueOf(number);
      String result = "";
      char[] chNum = strNum.toCharArray();
      for (int i = 0; i < chNum.length; i++) {
          result += map.get(Integer.parseInt(chNum[i] + ""));
      }
      return result;
      

      }

      public static int decode(String number) {
      Map map = decode_rules();
      String result = "";
      char[] chNum = number.toCharArray();
      for (int i = 0; i < chNum.length; i++) {
      result += map.get(chNum[i] + "");
      }
      int finalResult = Integer.parseInt(result);
      return finalResult;
      }

      public static Map encode_rules() {
      Map map = new HashMap();
      map.put(0, "a");
      map.put(1, "b");
      map.put(2, "c");
      map.put(3, "d");
      map.put(4, "e");
      map.put(5, "f");
      map.put(6, "g");
      map.put(7, "h");
      map.put(8, "i");
      map.put(9, "j");

      return map;
      

      }

      public static Map decode_rules() {
      Map map = new HashMap();
      map.put("a", "0");
      map.put("b", "1");
      map.put("c", "2");
      map.put("d", "3");
      map.put("e", "4");
      map.put("f", "5");
      map.put("g", "6");
      map.put("h", "7");
      map.put("i", "8");
      map.put("j", "9");

      return map;
      

      }

    }

    评论

报告相同问题?

悬赏问题

  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?