AES加密算法在不同平台上引起的“血案”

产品经理:小凌,这里有个简单的需求,将用户的敏感信息加密保存起来,需要尽快实现。

程序猿:好,没有问题,半个小时就搞定。

说完以后,小凌就动手起来了,打开百度搜索“Java加密算法”,复制了如下代码:

//加密
public static byte[] encrypt(String content, String password) {
        try {
            //构造密钥生成器,指定为AES算法
            KeyGenerator kgen = KeyGenerator.getInstance("AES");
            //初始化密钥生成器,指定随机源
            kgen.init(128, new SecureRandom(password.getBytes()));
            //产生原始对称密钥
            SecretKey secretKey = kgen.generateKey();
            byte[] enCodeFormat = secretKey.getEncoded();
            //根据字节数组生成AES密钥
            SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
            //根据指定算法AES自成密码器
            Cipher cipher = Cipher.getInstance("AES");
            byte[] byteContent = content.getBytes("utf-8");
            //初始化密码器
            cipher.init(Cipher.ENCRYPT_MODE, key);
            //加密
            byte[] result = cipher.doFinal(byteContent);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
}

加密写好了,哦不,是复制好了,既然有加密,那必须有解密,总不能将加密的信息直接显示出来,解密如下:

//解密
public static byte[] decrypt(byte[] content, String password) {
        try {
            KeyGenerator kgen = KeyGenerator.getInstance("AES");
            kgen.init(128, new SecureRandom(password.getBytes()));
            SecretKey secretKey = kgen.generateKey();
            byte[] enCodeFormat = secretKey.getEncoded();
            SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.DECRYPT_MODE, key);
            byte[] result = cipher.doFinal(content);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
 }

加密和解密的代码实现没有太大的不同,嗯.....代码复制好,就是这么简单.



接下来就是进行测试了:

public static void main(String[] args) throws Exception {
        String content = "linghenzeng";
        String password = "12345678";
        //加密
        System.out.println("加密前1:" + content);
        byte[] encryptResult = encrypt(content, password); 
        String strEncryptResult = parseByte2HexStr(encryptResult);
        System.out.println("加密后1:" + strEncryptResult);
        //解密
        byte[] byteDecryptResult = parseHexStr2Byte(strEncryptResult);
        byte[] decryptResult = decrypt(byteDecryptResult, password);    
        System.out.println("解密后1:" + new String(decryptResult));
}

为了加密和解密显示正常,将加密生成的字节转换成为十六进制,再将十六进制转换为字符串,解密之前,将字符串转换为二进制的字节,二进制和十六进制的转换函数如下:

// 二进制转十六进制
public static String parseByte2HexStr(byte buf[]) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < buf.length; i++) {
            String hex = Integer.toHexString(buf[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex.toUpperCase());
        }
        return sb.toString();
}
//十六进制转二进制
public static byte[] parseHexStr2Byte(String hexStr) {
        if (hexStr.length() < 1)
            return null;
        byte[] result = new byte[hexStr.length() / 2];
        for (int i = 0; i < hexStr.length() / 2; i++) {
            int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
            int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2),
                    16);
            result[i] = (byte) (high * 16 + low);
        }
        return result;
}

在Window上测试一切正常,加密解密都好使,刚好半个小时就完成了这个小需求,跟产品确认下,发布上线。

燃鹅,现实和理想存在巨大的鸿沟,服务器报异常:

javax.crypto.BadPaddingException: Given final block not properly padded
    at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:966)
    at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:824)
    at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:436)
    at javax.crypto.Cipher.doFinal(Cipher.java:2165)
    at test.decrypt(file.java:48)
    at test.main(file.java:94)
Exception in thread "main" java.lang.NullPointerException
    at java.lang.String.<init>(String.java:554)
    at test.main(file.java:95)

晴天霹雳,线上差不多三千条的用户敏感信息加密了,但解密不了......,将版本回滚回来,联系DBA将数据恢复,在DAB深厚技术基础上,数据恢复回来了。

遇到问题,首先是Ctrl + C、Ctrl + V,然后Enter,最后在搜索出来的内容去找出解决问题的方法,根据广大网友的智慧提供的一系列方法中提炼出来的答案如下:
密钥生成器指定的随机源是操作系统本身的内部状态的,即SecureRandom 类在源码上实现是不一样的,在windows平台上每次生成的key都相同,但是在linux平台上则不同。
具体的解决办法为将如下代码:

KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128, new SecureRandom(password.getBytes()));

换成

KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(key.getBytes());
kgen.init(128, secureRandom);

新旧代码不同的地方是旧代码直接将解密密钥的字节初始化SecureRandom,而新代码则是指定“SHA1PRNG”随机生成种子算法初始化SecureRandom,然后再将解密密钥的字节设置为随机种子。

问题解决了,代码在Linux上可以正常加密解密。



但是为什么指定“SHA1PRNG”,代码就可以在window平台和Linux平台上运行?难道在Linux平台默认指定的是其它算法?

在程序员界中,女程序员以为男程序员,什么都会。男程序员中,初级程序员以为高级程序员,什么都会。而高级程序员,每次都在网上苦苦查找答案。

为了更进一步接近问题的本质,打算从源码层次上寻找答案,探究SecureRandom类在不同平台上采取的随机种子算法有什么不同。


上图是window平台上调试的代码截图,以字节数组初始化的SecureRandom的构造函数中,默认采用的是“SHA1PRNG”算法进行初始化对象。下图的代码截图是在Linux上调试的结果:

在Linux平台上,以字节数组初始化的SecureRandom的构造函数中,默认采用的是“NativePRNG”算法进行初始化对象。

经过一系列的分析,终于在源码层面上找到产生问题的原因了,但引发的疑问更多了,什么是“SHA1PRNG”?什么是“NativePRNG”?它们之间有什么不同?.......

在java文档中找到了“SHA1PRNG”的解释:
https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html

The name of the pseudo-random number generation (PRNG) algorithm supplied by the SUN provider. This algorithm uses SHA-1 as the foundation of the PRNG. It computes the SHA-1 hash over a true-random seed value concatenated with a 64-bit counter which is incremented by 1 for each operation. From the 160-bit SHA-1 output, only 64 bits are used.

翻译为:

SUN提供的伪随机数生成(PRNG)算法的名称。该算法以SHA-1作为PRNG的生成函数。它通过一个真随机种子值和一个64位计数器连接来计算SHA-1散列,每个操作增加1。在160位SHA-1输出中,只使用64位。

而在另外一篇博文中,找到有关“NativePRNG”的信息:
https://metebalci.com/blog/everything-about-javas-securerandom/

As you expect, NativePRNG is platform specific:
1、For Solaris/Linux/MacOS, it obtains seed and random numbers from /dev/random and /dev/urandom and reads securerandom.source Security property and java.security.egd System property. The default is to obtain seed from /dev/random and obtain random numbers from /dev/urandom.
2、For Windows, NativePRNG is not implemented, but Windows native implemetation is provided using SunMSCAPI provider.

总结上面的意思:

1、在Solaris/Linux/MacOS上,“NativePRNG”底层是从从/dev/random和/dev/urandom获取种子和随机数。默认是从/dev/random获取种子,从/dev/urandom获取随机数。

2、在window上,没有实现“NativePRNG”,但是Windows的实现是使用SunMSCAPI provider提供的。

至此,所有疑问都解决了。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容