DES加密工具类,用于网络参数加密

public class DESTool {  
     // 密钥 ,至少24位
     private final static String secretKey = "78c5davff03a8ddb2fd921f5@747/c#" ;
     // 向量  
     private final static String iv = "ff2a535c" ;
     // 加解密统一使用的编码方式  
     private final static String encoding = "utf-8" ;  
        
     /** 
      * DESTool加密 
      *  
      * @param plainText 普通文本 
      * @return 
      * @throws Exception  
      */ 
     public static String encode(String plainText) throws Exception {  
         Key deskey = null ;  
         DESedeKeySpec spec = new DESedeKeySpec(secretKey.getBytes());  
         SecretKeyFactory keyfactory = SecretKeyFactory.getInstance( "DESede" );  
         deskey = keyfactory.generateSecret(spec);  
        
         Cipher cipher = Cipher.getInstance( "DESede/CBC/PKCS5Padding" );  
         IvParameterSpec ips = new IvParameterSpec(iv.getBytes());  
         cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);  
         byte [] encryptData = cipher.doFinal(plainText.getBytes(encoding));  
         return new BASE64Encoder().encode(encryptData);  
     }  
        
     /** 
      * DESTool解密 
      *  
      * @param encryptText 加密文本 
      * @return 
      * @throws Exception 
      */ 
     public static String decode(String encryptText) throws Exception {  
         Key deskey = null ;  
         DESedeKeySpec spec = new DESedeKeySpec(secretKey.getBytes());   
         SecretKeyFactory keyfactory = SecretKeyFactory.getInstance( "desede" );  
         deskey = keyfactory.generateSecret(spec);  
         Cipher cipher = Cipher.getInstance( "desede/CBC/PKCS5Padding" );  
         IvParameterSpec ips = new IvParameterSpec(iv.getBytes());  
         cipher.init(Cipher.DECRYPT_MODE, deskey, ips);  
        
         byte [] decryptData = cipher.doFinal(new BASE64Decoder().decodeBuffer(encryptText));  
        
         return new String(decryptData, encoding);  
     }  
     
   public static String padding(String str) {
      byte[] oldByteArray;
      try {
         oldByteArray = str.getBytes("UTF8");
         int numberToPad = 8 - oldByteArray.length % 8;
         byte[] newByteArray = new byte[oldByteArray.length + numberToPad];
         System.arraycopy(oldByteArray, 0, newByteArray, 0,
               oldByteArray.length);
         for (int i = oldByteArray.length; i < newByteArray.length; ++i) {
            newByteArray[i] = 0;
         }
         return new String(newByteArray, "UTF8");
      } catch (UnsupportedEncodingException e) {
         System.out.println("Crypter.padding UnsupportedEncodingException");
      }
      return null;
   }
     
     public static void main(String[] args) throws Exception{
        // Round 1
        String plainText = " 测试";
        String encryptText = DESTool.encode(plainText);
        System.out.println(encryptText);
        System.out.println(DESTool.decode(encryptText));

        // Round 2
        plainText = "Life is like a box of chocolate, you never know what you are going to get";
        encryptText = DESTool.encode(plainText);
        System.out.println(encryptText);
        System.out.println(DESTool.decode(encryptText));
        // Round 3
        plainText = "热烈欢迎19大胜利召开";
        encryptText = DESTool.encode(plainText);
        System.out.println(encryptText);
        System.out.println(DESTool.decode(encryptText));
     }
}

   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值