zoukankan      html  css  js  c++  java
  • (转)Java DES 与Base64

     原文地址http://blog.csdn.net/tomatozq/article/details/20773559

    1,DES

        /** 
                 * 解密 
                 * @param message 
                 * @param key 
                 * @return 
                 * @throws Exception 
                 */  
                public static String decrypt(String message,String encoding) throws Exception {  
          
                    byte[] bytesrc = convertHexString(message);  
                    Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");  
                    DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(encoding));  
                    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");  
                    SecretKey secretKey = keyFactory.generateSecret(desKeySpec);  
                    IvParameterSpec iv = new IvParameterSpec(key.getBytes(encoding));  
          
                    cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);  
          
                    byte[] retByte = cipher.doFinal(bytesrc);  
                    return new String(retByte,encoding);  
                }  
          
                /** 
                 * 加密 
                 * @param message 
                 * @param key 
                 * @return 
                 * @throws Exception 
                 */  
                public static String encrypt(String message,String encoding) throws Exception {  
                    Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");  
          
                    DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(encoding));  
          
                    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");  
                    SecretKey secretKey = keyFactory.generateSecret(desKeySpec);  
                    IvParameterSpec iv = new IvParameterSpec(key.getBytes(encoding));  
                    cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);  
          
                    byte[] buf = cipher.doFinal(message.getBytes(encoding));  
                      
                    String a = toHexString(buf).toUpperCase();  
                      
                    return a;  
                }  
          
                /** 
                 * 字符串转换为16进制数组 
                 * @param ss 
                 * @return 
                 */  
                public static byte[] convertHexString(String ss) {  
                    byte digest[] = new byte[ss.length() / 2];  
                    for (int i = 0; i < digest.length; i++) {  
                        String byteString = ss.substring(2 * i, 2 * i + 2);  
                        int byteValue = Integer.parseInt(byteString, 16);  
                        digest[i] = (byte) byteValue;  
                    }  
          
                    return digest;  
                }  
                  
                /** 
                 * 16进制数组转换为字符串 
                 * @param b 
                 * @return 
                 */  
                public static String toHexString(byte b[]) {  
                    StringBuffer hexString = new StringBuffer();  
                    for (int i = 0; i < b.length; i++) {  
                        String plainText = Integer.toHexString(0xff & b[i]);  
                        if (plainText.length() < 2)  
                            plainText = "0" + plainText;  
                        hexString.append(plainText);  
                    }  
          
                    return hexString.toString();  
                }  
    

    2,Base64

        byte[] enc = Base64.encode(encText.getBytes(encoding), Base64.DEFAULT);  
        byte[] bytesrc = Base64.decode(encText.getBytes(encoding), Base64.DEFAULT);  
    

      

  • 相关阅读:
    MinGW GCC下sleep()函数问题
    memcached内存分配及回收初探
    fastcgi重启
    window wamp下xhprof的安装使用,Graphviz配置
    Bootstrap研究3-基础html元素
    sql之left join、right join、inner join的区别
    【Asp.net入门03】第一个ASP.NET 应用程序-创建ASP.NET项目
    【Asp.net入门02】搭建Asp.net开发环境
    【Asp.net入门01】动态网站基础知识
    Android Studio 安装在Windows10中的陷阱
  • 原文地址:https://www.cnblogs.com/hhhh2010/p/4088255.html
Copyright © 2011-2022 走看看