3、AES加密算法

AES加密算法是密码学中的高级加密标准,该加密算法采用对称分组密码体制,密钥长度的最少支持为128、192、256,分组长度128位,算法应易于各种硬件和软件实现。这种加密算法是美国联邦政府采用的区块加密标准,这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。

过多原理不做解释,可以参考(AES加密算法原理http://www.jiamisoft.com/blog/858-aesjiamisuanfa.html

java中的具体使用如下:

public class SecurityAESUtils {
    private static Logger logger = LogManager.getLogger(SecurityAESUtils.class);

    private static final String ENCODEING = "UTF-8";
    private static final String ALGORITHM = "AES";//加密算法

    private static final String CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";//算法/工作模式/填充方式 

    /**
     * 加密
     * @param plaintext 明文
     * @param secureKey 16位长度的密钥
     * @return
     */
    public static String encrypt(String plaintext, String secureKey) throws Exception{
        SecretKeySpec sks = getSecretKeySpec(secureKey);
        Cipher encryptCipher = getCipher(Cipher.ENCRYPT_MODE, sks);
        byte[] result = encryptCipher.doFinal(plaintext.getBytes(ENCODEING));
        return  Base64.encodeBase64String(result);
    }

    /**
     * 加密
     * @param bytes 
     * @param secureKey 16位长度的密钥
     * @return
     */
    public static String encryptBytes(byte[] bytes, String secureKey) throws Exception{
        SecretKeySpec sks = getSecretKeySpec(secureKey);
        Cipher encryptCipher = getCipher(Cipher.ENCRYPT_MODE, sks);
        byte[] result = encryptCipher.doFinal(bytes);
        return  Base64.encodeBase64String(result);
    }

    /**
     * 解密
     * @param ciphertext 密文
     * @return secureKey 16位长度的密钥
     * @throws Exception
     */
    public static String decrypt(String ciphertext, String secureKey) throws Exception {
        SecretKeySpec sks = getSecretKeySpec(secureKey);
        Cipher decryptCiphe = getCipher(Cipher.DECRYPT_MODE, sks);//initDecryptCipher(secureKey);
        byte[] result =  decryptCiphe.doFinal(Base64.decodeBase64(ciphertext));
        return new String(result, ENCODEING);
    }

    private static Cipher getCipher(int cipherMode, SecretKeySpec sks) throws Exception{
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
        cipher.init(cipherMode, sks);
        return cipher;
    }

    private static SecretKeySpec getSecretKeySpec(String secureKey) throws Exception{
        if(secureKey == null || secureKey.trim().equals("") || secureKey.length() != 16){
            throw new Exception("密钥不能为空或密钥长度不对");
        }
        byte[] raw = secureKey.getBytes(ENCODEING);
        SecretKeySpec skeySpec = new SecretKeySpec(raw, ALGORITHM);
        return skeySpec;
    }

    /**
     * @Comment 加密不限制密码长度
     * @Author Ron
     * @Date 2017年9月12日 下午3:21:59
     * @return
     */
    public static String encryptNotLimit(String plaintext, String secureKey) throws Exception{
        SecretKeySpec sks = getSecretKeySpecByRandomSeed(secureKey);
        Cipher encryptCipher = getCipher(Cipher.ENCRYPT_MODE, sks);
        byte[] result = encryptCipher.doFinal(plaintext.getBytes(ENCODEING));
        return Hex.encodeHexString(result);
    }

    /**
     * @Comment 解密不限制密码长度
     * @Author Ron
     * @Date 2017年9月12日 下午3:22:30
     * @return
     */
    public static String decryptNotLimit(String ciphertext, String secureKey) throws Exception {
        SecretKeySpec sks = getSecretKeySpecByRandomSeed(secureKey);
        Cipher decryptCiphe = getCipher(Cipher.DECRYPT_MODE, sks);
        byte[] result =  decryptCiphe.doFinal(Hex.decodeHex(ciphertext.toCharArray()));
        return new String(result, ENCODEING);
    }

    /**
     * @Comment 构造私钥
     * @Author Ron
     * @Date 2017年9月12日 下午3:22:59
     * @return
     */
    private static SecretKeySpec getSecretKeySpecByRandomSeed(String secureKey){
        SecretKeySpec sks = null;
        try {
            KeyGenerator kgen = KeyGenerator.getInstance(ALGORITHM);
            //安全随机数生成器 
            SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");//使用默认的SHA1PRNG算法
            secureRandom.setSeed(secureKey.getBytes(ENCODEING));
            kgen.init(128, secureRandom); 
            SecretKey secretKey = kgen.generateKey();
            byte[] secretKeyEncoded = secretKey.getEncoded();
            sks = new SecretKeySpec(secretKeyEncoded, ALGORITHM);
        } catch (Exception e) {
            logger.error("",e);
        }
        return sks;
    }

    public static void main(String[] args) throws Exception {
        String key = "ron.zheng@tfscha";
        String src = "我是Ron";
        String enString = encrypt(src,key);
        System.out.println("加密后的数据:"+enString);
        System.out.println("解密后的数据:"+decrypt(enString,key));

        //不限制密钥长度
        String nkey = "ron.zheng@tfschang.com";
        enString = encryptNotLimit(src, nkey);
        System.out.println("加密后的数据:"+enString);
        System.out.println("解密后的数据:"+decryptNotLimit(enString,nkey));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

RonTech

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值