zoukankan      html  css  js  c++  java
  • JAVA 实现DES MD5加密

    package com.esailcar.finance.shenzhou.utils;
    
    import java.io.UnsupportedEncodingException;
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    import java.security.SecureRandom;
    
    import javax.crypto.Cipher;
    import javax.crypto.SecretKey;
    import javax.crypto.SecretKeyFactory;
    import javax.crypto.spec.DESKeySpec;
    
    import sun.misc.BASE64Decoder;
    import sun.misc.BASE64Encoder;
    
    public class SecretDigest {
            
            private final static String MD5="MD5";
            private final static String DES="DES";
            private final static String ENCODE="UTF-8";
            private final static String KEY="asdf1234";
            
            //MD5加密
            public static String encodeByMD5(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException
            {
                MessageDigest md5=MessageDigest.getInstance(MD5);
                StringBuffer md5buffer=new StringBuffer();
                md5.update(str.getBytes("UTF-8"));
                byte[] md5bytes=md5.digest();
                String hexString=null;
                for(byte md5byte : md5bytes)
                {
                     hexString=Integer.toHexString(0xff&md5byte);
                    if(hexString.length()==1)
                    {
                        md5buffer.append("0").append(hexString);
                    }
                    else
                    {
                        md5buffer.append(hexString);
                    }
                }
                return md5buffer.toString();        
            }
            
            /**
             * DES加密
             * @param str
             * @return
             * @throws Exception
             */
            public static String encodeByDES(String str)throws Exception
            {
                byte[] data=str.getBytes(ENCODE);
                
                byte[] key=KEY.getBytes(ENCODE);
               
                SecureRandom sr = new SecureRandom();
                // 通过key创建DESKeySpec对象
                DESKeySpec dks = new DESKeySpec(key);
                
                // 创建SecretKeyFactory,把DESKeySpec转换成SecretKey对象
                SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
                SecretKey securekey = keyFactory.generateSecret(dks);
    
                // Cipher对象实际完成加密操作
                Cipher cipher = Cipher.getInstance(DES);
    
                // 用securekey初始化Cipher对象
                cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
                
                //BASE64编码
                String message=new BASE64Encoder().encode(cipher.doFinal(data));
                return message;            
            }
            /**
             * DES解密
             * @param str
             * @return
             * @throws Exception
             */
            public static String decodeByDES(String str)throws Exception
            {
                  if (str == null)
                        return null;
               //BASE64解码
               BASE64Decoder decoder = new BASE64Decoder();
                
                byte[] data=decoder.decodeBuffer(str);
                byte[] key=KEY.getBytes(ENCODE);
                SecureRandom sr = new SecureRandom();
                // 通过key创建DESKeySpec对象
                DESKeySpec dks = new DESKeySpec(key);
    
                // 创建SecretKeyFactory,把DESKeySpec转换成SecretKey对象
                SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
                SecretKey securekey = keyFactory.generateSecret(dks);
    
                // Cipher对象实际完成解密操作
                Cipher cipher = Cipher.getInstance(DES);
    
                // 用securekey初始化Cipher对象
                cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
                return new String(cipher.doFinal(data), ENCODE);           
            }
    }
    
     
    
  • 相关阅读:
    浅析几种常用坐标系和坐标转换
    windows live message 无法安装
    解决英文版XP下的PL/SQL Developer的中文乱码问题
    C# static 用法
    用plsql登陆oracle,创建用户赋予权限
    Silverlight Map 技术点总结
    【OCP12c】CUUG 071题库考试原题及答案解析(20)
    【OCP12c】CUUG 071题库考试原题及答案解析(15)
    【OCP12c】CUUG 071题库考试原题及答案解析(14)
    【OCP12c】CUUG 071题库考试原题及答案解析(17)
  • 原文地址:https://www.cnblogs.com/temporary/p/7280075.html
Copyright © 2011-2022 走看看