zoukankan      html  css  js  c++  java
  • 加密解密16进制用法:

      1.Base64 :  26个英文字母大小写+10个阿拉伯数字+“+ =”  一共64个;

      2.3DES:要求秘钥 为24位的

          

    public static byte[] encrypt(byte[] key,byte[] data){
    SecretKey secretKey=new SecretKeySpec(key, "DESede");
    try {
    Cipher cipher=Cipher.getInstance("DESede");
    cipher.init(Cipher.ENCRYPT_MODE, secretKey);
    return cipher.doFinal(data);

    } catch (NoSuchAlgorithmException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (NoSuchPaddingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (InvalidKeyException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (BadPaddingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return null;
    }
    public static byte[]decrypt(byte[]key,byte[]data){
    SecretKey secretKey=new SecretKeySpec(key, "DESede");
    try {
    Cipher cipher=Cipher.getInstance("DESede");
    cipher.init(Cipher.DECRYPT_MODE, secretKey);
    return cipher.doFinal(data);

    } catch (NoSuchAlgorithmException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (NoSuchPaddingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (InvalidKeyException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (BadPaddingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return null;
    }

        16进制转换:

    public static String toHexString(byte[] data, String seperator){
    StringBuilder sb = new StringBuilder(data.length * (2 + seperator.length()));

    int pb = 0;
    String pre = "";
    while(pb < data.length){
    byte b = data[pb++];

    int h = ((0xF0 & b) >> 4);
    int l = (0x0F & b);

    sb.append(pre);
    if(h < 10){
    sb.append((char)('0' + h));
    }else{
    sb.append((char)('a' + h - 10));
    }
    if(l < 10){
    sb.append((char)('0' + l));
    }else{
    sb.append((char)('a' + l - 10));
    }
    pre = seperator;

    }

    return sb.toString();
    }

    /**
    * 将十六进制字符串表示的字节数据还原成字节数组
    * @param text 被还原的字符串
    * @return 还原之后的字节数组
    */
    public static byte[] fromHexString(String text){
    if(text == null)
    return new byte[0];

    byte[] result = new byte[text.length() / 2];

    text = text.toLowerCase();
    int pb = 0;
    int ps = 0;
    while(pb < result.length && ps < text.length()){
    char ch = text.charAt(ps++);
    char cl = text.charAt(ps++);

    int a = 0;
    int b = 0;
    if('0' <= ch && ch <= '9'){
    a = ch - '0';
    }else{
    a = ch - 'a' + 10;
    }
    if('0' <= cl && cl <= '9'){
    b = cl - '0';
    }else{
    b = cl - 'a' + 10;
    }

    result[pb++] = (byte)((a << 4) + b);
    }

    return result;
    }

         

  • 相关阅读:
    前端工程师应该具备的三种思维
    7 个 Bootstrap 在线编辑器用于快速开发响应式网站
    js阻止浏览器的默认行为以及停止事件冒泡(用JQuery实现回车提交)
    JAVASCRIPT加密方法,JS加密解密综述(7种)
    JavaScript生成GUID的方法
    js判断是否为手机访问
    Jquery中parent()和parents()
    jQuery中ajax和post处理json的不同
    JQuery实现回车代替Tab键(按回车跳到下一栏)
    js中replace的用法
  • 原文地址:https://www.cnblogs.com/xxwn/p/4633792.html
Copyright © 2011-2022 走看看