加密工具类

昨天的笔记中,包含了CryptoUtils和RSAUtils,今天顺便贴一下两个对称加密的工具类:

DESUtils

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.Provider;
import java.security.spec.InvalidKeySpecException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

public class DESUtils extends CryptoUtils {

    private final static String KEY_ALGORITHM = "DES";

    public static String encrypt(String plainText, String key) {
        try {
            SecretKey securekey = getSecretKey(key);
            Cipher cipher = getCipher(KEY_ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, securekey, random);
            byte[] bt = cipher.doFinal(plainText.getBytes());
            return encodeToString(bt);
        } catch (InvalidKeyException e) {
            throw new RuntimeException(e);
        } catch (IllegalBlockSizeException e) {
            throw new RuntimeException(e);
        } catch (BadPaddingException e) {
            throw new RuntimeException(e);
        }
    }

    public static String decrypt(String encryptText, String key) {
        try {
            SecretKey securekey = getSecretKey(key);
            Cipher cipher = getCipher(KEY_ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, securekey, random);
            byte[] datas = cipher.doFinal(decodeFromString(encryptText));
            return new String(datas);
        } catch (InvalidKeyException e) {
            throw new RuntimeException(e);
        } catch (IllegalBlockSizeException e) {
            throw new RuntimeException(e);
        } catch (BadPaddingException e) {
            throw new RuntimeException(e);
        }
    }

    private static SecretKey getSecretKey(String key) throws InvalidKeyException {
        try {
            byte[] datas = key.getBytes();
            DESKeySpec dks = new DESKeySpec(datas);
            Provider provider = getProvider();
            SecretKeyFactory keyFactory = null;
            if (null == provider) {
                keyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM);
            } else {
                keyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM, provider);
            }
            SecretKey securekey = keyFactory.generateSecret(dks);
            return securekey;
        } catch (InvalidKeyException e) {
            throw new RuntimeException(e);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (InvalidKeySpecException e) {
            throw new RuntimeException(e);
        }
    }
}

AESUtils

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.Provider;
import java.security.SecureRandom;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class AESUtils extends CryptoUtils {

    private static final String KEY_ALGORITHM = "AES";

    public static String encrypt(String plantText, String key) {
        try {
            Cipher cipher = getCipher(KEY_ALGORITHM);
            SecretKey secretKey = getSecretKey(key);
            byte[] data = plantText.getBytes();
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            byte[] result = cipher.doFinal(data);
            return encodeToString(result);
        } catch (InvalidKeyException e) {
            throw new RuntimeException(e);
        } catch (IllegalBlockSizeException e) {
            throw new RuntimeException(e);
        } catch (BadPaddingException e) {
            throw new RuntimeException(e);
        }
    }

    public static String decrypt(String encryptText, String key) {
        try {
            Cipher cipher = getCipher(KEY_ALGORITHM);
            SecretKey secretKey = getSecretKey(key);
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            byte[] data = decodeFromString(encryptText);
            byte[] result = cipher.doFinal(data);
            return new String(result);
        } catch (InvalidKeyException e) {
            throw new RuntimeException(e);
        } catch (IllegalBlockSizeException e) {
            throw new RuntimeException(e);
        } catch (BadPaddingException e) {
            throw new RuntimeException(e);
        }
    }

    private static SecretKey getSecretKey(final String key) {
        try {
            Provider provider = getProvider();
            KeyGenerator kg = null;
            if (null == provider) {
                kg = KeyGenerator.getInstance(KEY_ALGORITHM);
            } else {
                kg = KeyGenerator.getInstance(KEY_ALGORITHM, provider);
            }
            kg.init(128, new SecureRandom(key.getBytes()));
            SecretKey secretKey = kg.generateKey();
            return new SecretKeySpec(secretKey.getEncoded(), KEY_ALGORITHM);// 转换为AES专用密钥
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }
}

原文地址:https://www.cnblogs.com/linjisong/p/8759060.html

时间: 2024-10-28 22:00:06

加密工具类的相关文章

Java AES 加密工具类

package com.microwisdom.utils; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.util.logging.Level; import java.util.logging.Logger; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import jav

android开发MD5加密工具类(一)

MD5加密工具类整理: 1 package com.gzcivil.utils; 2 3 import java.io.UnsupportedEncodingException; 4 import java.security.MessageDigest; 5 import java.security.NoSuchAlgorithmException; 6 7 public class MD5Tool { 8 9 public static String md5(String string) {

加密工具类 - CryptoUtils.java

加密工具类,包含MD5,BASE64,SHA,CRC32的加密与解密方法. 源码如下:(点击下载  - CryptoUtils.java.commons-io-2.4.jar.commons-codec-1.9.jar ) import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java

android加密工具类

import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; /** * 加密工具类 * Created by Administrator on 2015/10/21 0021. */ public class EncryptUtils { /** * 字符串加密使用MD5算法 */ public final static String encryptMD5(String source) {

wemall app商城源码android开发MD5加密工具类

wemall-mobile是基于WeMall的android app商城,只需要在原商城目录下上传接口文件即可完成服务端的配置,客户端可定制修改.本文分享android开发MD5加密工具类主要代码,供技术员参考学习. package com.gzcivil.utils; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgori

md5加密工具类

md5加密工具类: package cn.sniper.encrypt.util; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; /**  * 加密工具类  *   * md5加密出来的长度是32位  *   * sha加密出来的长度是40位  *   * @author sniper  

Md5Util加密工具类

/** * MD5加密工具类 * @author john * */ public class Md5Util { private static final String HEX_NUMS_STR = "0123456789ABCDEF"; // 16进制字符串 private static final Integer SALT_LENGTH = 12; // 盐数据长度 /**  * 将16进制字符串转换成字节数组  * @param hex  * @return  */ publi

Java MD5,base64,AES加密工具类

import java.io.UnsupportedEncodingException; import java.math.BigInteger; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.util.regex.Matcher; import java.util.regex.Pat

Android初级教程:对文件和字符串进行MD5加密工具类

转载请注明出处:http://blog.csdn.net/qq_32059827/article/details/52200008   点击打开链接 之前写过一篇博文,是针对字符串进行md5加密的.今天对其进行改进,加入针对某个文件,进行md5加密,并获取加密后的值.并把两个功能封装成了工具类,如果有需要这个算法的,可直接使用. 直接上算法封装的工具类代码: package com.itydl.utils; import java.io.File; import java.io.FileInpu

JAVA中使用MD5加密工具类实现对数据的加密处理

1.MD5工具类 package com.ssm.util; import java.security.MessageDigest; public class MD5Util { //将字节数组转成十六进制字符串 private static String byteArrayToHexString(byte b[]) { StringBuffer resultSb = new StringBuffer(); for (int i = 0; i < b.length; i++) resultSb.