AES、DES、RAS、MD5加密工具类——java实现

最近使用了几个加密的工具类,特此整理,方便使用。

MD5 :使用哈希算法,具有不可逆,压缩性
DES : 对称加密算法,加密和解密使用的是同一个私钥,key是56位
AES :对称加密算法,DES的升级版,高级加密标准,key可以是128、192和256位
RAS :非对称加密算法,使用公钥、私钥进行加密或解密

下面直接贴出代码:

1. MD5加密
import java.security.MessageDigest;

/**
 * MD5加密的工具类
 * @author alan chen
 */
public class MD5Utils {
   

    /**
     * md5 加密
     * @param ss
     * @return
     */
    public String encryptByMD5(String content){
   //MD5加密算法
        String s = content == null ? "":content;//如果为空,则返回""
        char hexDigists[] = {
   '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};//字典

        try {
   
            byte[] strTemp =s.getBytes();//获取二进制
            MessageDigest mdTemp =MessageDigest.getInstance("MD5");
            mdTemp.update(strTemp);//执行加密
            byte[] md = mdTemp.digest();//加密结果
            int j = md.length;//结果长度
            char str[] = new char[j*2];//字符数组
            int k = 0;
            for (int i = 0; i < j; i++) {
    //将二进制加密结果转化为字符
                byte byte0 = md[i];
                str[k++] = hexDigists[byte0 >>> 4 &0xf];
                str[k++] = hexDigists[byte0 & 0xf];

            }
            return new String(str);//输出加密后的字符
        } catch (Exception e) {
   
            e.printStackTrace();
            return null;
        }
    }

}



2. DES加密/解密
import org.apache.commons.codec.CharEncoding;
import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.net.URLDecoder;
import java.security.SecureRandom;

/**
 * DES加密/解密
 * @time 2019/8/1 10:30 PM
 */
public class DesEncrypt {
   


	/**
	 * des 加密
	 * @param content 需要加密的内容
	 * @param key 自定义key, 必须是8位
	 * @return
	 */
	public static String encrypt(String content, String key) throws Exception {
   
		SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
		DESKeySpec keySpec = new DESKeySpec(key.getBytes());
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
		SecretKey secretKey = keyFactory.generateSecret(keySpec);

		Cipher cipher = Cipher.getInstance("DES");
		cipher.init(Cipher.ENCRYPT_MODE, secretKey, random);
		byte[] cipherData = cipher.doFinal(content.getBytes(CharEncoding.UTF_8));
		String cipherText = Base64.encodeBase64String(cipherData);
		return cipherText;
	}


	/**
	 * des解密
	 * @param encodeStr 加密的字符串
	 * @param key 自定义key,加密使用的key
	 */
	public static String decrypt(String encodeStr, String key) throws Exception {
   
		SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
		DESKeySpec keySpec = new DESKeySpec(key.getBytes());
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
		SecretKey secretKey = keyFactory.generateSecret(keySpec);

		Cipher cipher = Cipher.getInstance("DES");
		String decodeStr = URLDecoder.decode(encodeStr, CharEncoding.UTF_8);

		cipher.init(Cipher.DECRYPT_MODE, secretKey, random);
		byte[] decodeBytes = cipher.doFinal(Base64.decodeBase64(decodeStr));
		return new String(decodeBytes
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值