• java Des 加密解密技术


    package com.esb;

    import javax.crypto.Cipher;
    import javax.crypto.SecretKey;
    import javax.crypto.SecretKeyFactory;
    import javax.crypto.spec.DESKeySpec;
    import javax.crypto.spec.IvParameterSpec;
    import sun.misc.BASE64Decoder;
    import sun.misc.BASE64Encoder;

    public class Des {

        //
        //加密数据
        //
     public static String encrypt(String message, String key, String IV)
       throws Exception {
      Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
      BASE64Decoder base64De = new BASE64Decoder();
      BASE64Encoder base64En = new BASE64Encoder();
      DESKeySpec desKeySpec = new DESKeySpec(base64De.decodeBuffer(key));

      SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
      SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
      IvParameterSpec iv = new IvParameterSpec(base64De.decodeBuffer(IV));
      cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);

      byte[] result = cipher.doFinal(message.getBytes("utf-8"));
      return base64En.encodeBuffer(result);
     }
     
     // 解密数据
     public static String decrypt(String message,String key, String IV) throws Exception {
       BASE64Decoder base64De = new BASE64Decoder();
             byte[] bytesrc = base64De.decodeBuffer(message);
             Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");   
             DESKeySpec desKeySpec = new DESKeySpec(base64De.decodeBuffer(key));  
             SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");  
             SecretKey secretKey = keyFactory.generateSecret(desKeySpec);  
             IvParameterSpec iv = new IvParameterSpec(base64De.decodeBuffer(IV));
             cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);     
             byte[] retByte = cipher.doFinal(bytesrc);    
             return new String(retByte);
     }
     
     public static void main(String[] args) throws Exception {
      String key = "8SpOkb2LJcw=";
      String IV  = "I9RIqgOpVk4=";
      String value="loginName=admin&logintime=2010/03/15 15:01:01 ";
      String jiami=java.net.URLEncoder.encode(value, "utf-8");
      jiami = value;
      System.out.println("需要加密的数据:"+jiami);
      String a= encrypt(jiami, key, IV);
      System.out.println("加密后的数据为:"+a);
      
      String c="hDPqGpcGs4QHl-T5-58nSAAtAW35ZUCpuLXfcjReChzOrzdn9kfiQI1lVnqvvSHE";
             c="hDPqGpcGs4QHl+T5+58nSAAtAW35ZUCpuLXfcjReChzOrzdn9kfiQI1lVnqvvSHE";
      System.out.println("提供的加密后的数据:"+c);
      String b = decrypt(c,key,IV) ;
      System.out.println("解密后的数据:"+b);

     }
    }

  • 相关阅读:
    定制可引导的iso镜像
    镜像封装
    nginx知识及nginx集群搭建
    注册系统服务
    安装pip
    获取硬件信息
    查看网卡信息
    python paramiko模块
    python pexpect模块
    python-nmap的使用
  • 原文地址:https://www.cnblogs.com/anuoruibo/p/3324560.html
Copyright © 2020-2023  润新知