(转)Java DES 与Base64

本文详细介绍了DES加密算法的实现过程,并通过示例展示了如何使用DES进行加密和解密操作;同时,文章还阐述了Base64编码的基本原理及应用,通过实例演示了如何将文本转换为Base64编码以及从Base64编码还原原始文本的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 原文地址http://blog.youkuaiyun.com/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);  

  

转载于:https://www.cnblogs.com/hhhh2010/p/4088255.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值