js\java 通过算法加密后,再base64

来源:互联网 发布:telnet 80端口失败 编辑:程序博客网 时间:2024/05/20 16:33

java 通过算法加密后,再base64

   /**    * 授权播放    * @param request    * @param response    * @param map    * @throws Exception    */  @RequestMapping(value = "/get/player/sign", method = { RequestMethod.GET, RequestMethod.POST })  @ResponseBody   public String getPlayerSign(HttpServletRequest request, HttpServletResponse response,           Map<String, Object> map) throws Exception {       ParamUtil pu = new ParamUtil(request);       String vid = ServletRequestUtils.getStringParameter(request, "vid", "");       String code = ServletRequestUtils.getStringParameter(request, "code", "");       String username = ServletRequestUtils.getStringParameter(request, "username", "");       String status = ServletRequestUtils.getStringParameter(request, "status", "");       String t = ServletRequestUtils.getStringParameter(request, "t", "");              DOC doc = new DOC();       doc.put("error", "0");                    String userid=vid.substring(0,10);       CcUser customers=null;       customers = ccUserService.getByUserId(userid);       if (customers!=null) {           //通过writetoken验证           String s=vid+customers.getSecretkey()+username+code+status+t;           String sign=MD5Util.getMD5String(s);           logger.info("处理前的签名:"+sign);           //对签名进行处理           sign=signEncode(sign);           logger.info("encode后的签名:"+sign);           sign=new Base64Encoder().encode(sign.getBytes());                      doc.put("error", "0");//没有writetoken对应的用户           doc.put("sign", sign);       }else{           doc.put("error", "6");//没有writetoken对应的用户           doc.put("result", "没有writetoken对应的用户");       }                      outPrintStream(JsonUtil.docToJson(doc), response);       return null;   }      /**   * 对签名编码进行处理   */  public String signEncode(String sign){      String key = "lpmkenjibhuvgycftxdrzsoawq0126783459";      String alphabet, coded;        int index;        char ch;      alphabet = "abcdofghijklnmepqrstuvwxyz0123456789";      coded = "" + alphabet.charAt((int) (Math.random()*alphabet.length()));      for (int i = 0; i < sign.length(); i++) {        // for as many letters as there are          ch = sign.charAt(i);                   //   access the letter in the message          index = alphabet.indexOf(ch);                                                    //   find its position in alphabet          if (index == -1) {                                                    //   if it's not a letter,              coded = coded + ch;                   //     then leave it as is & add          }                                         //   otherwise,          else {                                    //     find the corresponding              coded = coded + key.charAt(index);    //     letter in the key & add          }      }      return coded;  }            public static void main(String args[]) throws UnsupportedEncodingException{      String sgin="bjZjbDYzbjM5ODBjMG0zNm5jazFrNzE1MjRtMTIzNTgw";      byte[] b=org.apache.commons.codec.binary.Base64.encodeBase64(sgin.getBytes());      byte[] a=org.apache.commons.codec.binary.Base64.decodeBase64(sgin);      System.out.println(signDecode(new String(a, "utf-8")));  }    public static String signDecode(String message){      String key="lpmkenjibhuvgycftxdrzsoawq0126783459";      String alphabet, coded;    char ch;    int index;    int i;      alphabet = "abcdofghijklnmepqrstuvwxyz0123456789";      message = message.substring(1);      coded = "";      for (i = 0; i < message.length(); i++) {        // for as many letters as there are          ch = message.charAt(i);                   //   access the letter in the message          index = key.indexOf(ch);             //   find its position in alphabet          if (index == -1) {                        //   if it's not a letter,              coded = coded + ch;                   //     then leave it as is & add          }                                         //   otherwise,          else {                                    //     find the corresponding              coded = coded + alphabet.charAt(index);    //     letter in the key & add          }      }      return coded;  }


js加密,解密

<p id="encrypt"></p><script>    function Encode(key, message)    // Given  : key is a string of the 26 letters in arbitrary order,    //          message is the string to be encoded using the key    // Returns: the coded version of message using the substitution key    {        var alphabet, coded, i, ch, index;        alphabet = "abcdofghijklnmepqrstuvwxyz0123456789";        coded = "" + alphabet.charAt(parseInt(Math.random()*alphabet.length));        for (i = 0; i < message.length; i++) {        // for as many letters as there are            ch = message.charAt(i);                   //   access the letter in the message            index = alphabet.indexOf(ch);                                                      //   find its position in alphabet            if (index == -1) {                                                      //   if it's not a letter,                coded = coded + ch;                   //     then leave it as is & add            }                                         //   otherwise,            else {                                    //     find the corresponding                coded = coded + key.charAt(index);    //     letter in the key & add            }        }        return coded;    }    function Decode(key,message){        var alphabet, coded, i, ch, index;        alphabet = "abcdofghijklnmepqrstuvwxyz0123456789";        message = message.substr(1);        coded = "";        for (i = 0; i < message.length; i++) {        // for as many letters as there are            ch = message.charAt(i);                   //   access the letter in the message            index = key.indexOf(ch);             //   find its position in alphabet            if (index == -1) {                        //   if it's not a letter,                coded = coded + ch;                   //     then leave it as is & add            }                                         //   otherwise,            else {                                    //     find the corresponding                coded = coded + alphabet.charAt(index);    //     letter in the key & add            }        }        return coded;    }    var encodeString = Encode("lpmkenjibhuvgycftxdrzsoawq0126783459","05714ecace14c04326f27aa638aa4c2e_0");    var decodeString = Decode("lpmkenjibhuvgycftxdrzsoawq0126783459",encodeString);    var explain = ">>>encodeString: " + encodeString +" >>decodeString: " + decodeString ;    console.log(explain);    var encrypt = document.getElementById("encrypt");    encrypt.innerHTML = explain;</script>


0 0