高级加密标准(Advanced Encryption Standard,简写AES),是一种用来替代DES的对称加密算法,相比DES,AES安全性更高,加密速度更快,因此被广泛使用。

  理论上看,AES可被破解,但是就目前的硬件性能来看,目前AES还是安全的,在开发工程中,如果要使用对称加密算法,应该首选AES。

  下面使用介绍各语言中的DES加密解密实现:

    

    1、加密解密过程分别使用比较多的ECB和CBC两种方式来实现,ECB性能更快,但是安全性没有CBC好,所以目前CBC用的比较多
    2、加密解密填充方式采用PKCS7,或者PKCS5,这两者在这里的加密解密过程中的结果可以认为是一样的
3、CBC模式需要秘钥及初始向量,而ECB模式只需要秘钥即可,所以为了方便,下面封装的方法或者函数将通过判断是否存在初始向量来决定是使用CBC模式还是使用ECB模式

  Java

  首先,做一个封装:  

  
    import java.security.Key;
    
    import javax.crypto.Cipher;
    import javax.crypto.spec.IvParameterSpec;
    import javax.crypto.spec.SecretKeySpec;
    
    public class AesUtil {
    
        /**
        * DES加密
        *
        * @param value
        *            待机加密明文
        * @param secretKey
        *            秘钥
        * @param iv
        *            初始向量,如果未空,则将采用ECB模式,否则采用CBC模式
        *
        * @return 密文
        */
        public static String aesEncrypt(String value, String secretKey, String iv)
                throws Exception {
            if (value == null || value.length() == 0)
                return "";
    
            Cipher cipher = initCipher(Cipher.ENCRYPT_MODE, secretKey, iv);
            byte[] buffer = cipher.doFinal(value.getBytes("utf-8"));
            // 使用hex格式数据输出
            StringBuffer result = new StringBuffer();
            for (int i = 0; i < buffer.length; i++) {
                result.append(String.format("%02x", buffer[i]));
            }
            return result.toString();
    
        }
    
        /**
        * DES解密
        *
        * @param value
        *            密文
        * @param secretKey
        *            秘钥
        * @param iv
        *            初始向量,如果未空,则将采用ECB模式,否则采用CBC模式
        *
        * @return 解密后的明文
        */
        public static String aesDecrypt(String value, String secretKey, String iv)
                throws Exception {
            if (value == null || value.length() == 0)
                return "";
    
            // 转换hex格式数据未byte数组
            int length = value.length();
            byte[] buffer = new byte[length / 2];
            for (int i = 0; i < buffer.length; i++) {
                buffer[i] = (byte) Integer.parseInt(
                        value.substring(i * 2, i * 2 + 2), 16);
            }
    
            Cipher cipher = initCipher(Cipher.DECRYPT_MODE, secretKey, iv);
            buffer = cipher.doFinal(buffer);
            return new String(buffer, "utf-8");
        }
    
        private static Cipher initCipher(int mode, String secretKey, String iv)
                throws Exception {
    
            try {
                // 获取秘钥数组
                byte[] secretBytes = secretKey.getBytes("utf-8");
                byte[] keyBytes = new byte[16];
                System.arraycopy(secretBytes, 0, keyBytes, 0,
                        Math.min(secretBytes.length, keyBytes.length));
                Key key = new SecretKeySpec(keyBytes, "AES");
    
                if (iv == null || iv.length() == 0) {
                    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");// 采用ECB方式
                    cipher.init(mode, key);
                    return cipher;
                } else {
                    // 初始向量数组
                    byte[] ivBytes = iv.getBytes("utf-8");
                    byte[] ivKeyBytes = new byte[16];
                    System.arraycopy(ivBytes, 0, ivKeyBytes, 0,
                            Math.min(ivBytes.length, ivKeyBytes.length));
    
                    IvParameterSpec ivp = new IvParameterSpec(ivKeyBytes);
                    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");// 采用CBC方式
                    cipher.init(mode, key, ivp);
                    return cipher;
                }
            } catch (Exception e) {
                e.printStackTrace();
                throw e;
            }
        }
    }
AesUtil

相关文章:

  • 2022-02-06
  • 2021-12-04
  • 2022-12-23
  • 2021-06-27
  • 2021-12-19
  • 2021-12-07
  • 2022-01-18
  • 2022-02-16
猜你喜欢
  • 2021-12-20
  • 2022-02-18
  • 2023-02-21
  • 2022-12-23
  • 2021-11-18
  • 2021-08-08
  • 2022-01-08
相关资源
相似解决方案