我正在做 android 数据加密以保存在 SharedPreferences 中。GCMParameterSpec 是在我用于AES/GCM/NoPadding
加密的 API 19 中的 Android 中引入的。这就是我实现它的方式:
Cipher c = Cipher.getInstance("AES/GCM/NoPadding");
c.init(Cipher.ENCRYPT_MODE, getSecretKey(context),new GCMParameterSpec(128,Base64.decode(myGeneratedIV, Base64.DEFAULT)));
我的问题是,在 Android 4.4.2 (API 19) 中,我收到了提到的错误,但从 API 21 开始它可以工作。
关于异常,来自 Android 文档:
如果给定的算法参数不适合此密码,或者此密码需要算法参数并且 params 为空,或者给定的算法参数暗示的加密强度将超过法律限制(由配置的管辖权策略文件确定)。
我的问题是:这种行为是否有特定原因?为什么init
Cipher 中的方法不能识别参数?
我什至尝试在不提供特定 IV 的情况下进行加密:
c.init(Cipher.ENCRYPT_MODE, getSecretKey(context));
一旦我尝试以相同的方式解密:
c.init(Cipher.DECRYPT_MODE, getSecretKey(context));
它抛出相同的异常(InvalidAlgorithmParameterException),说GCMParameterSpec
解密需要 a。
我尝试给GCMParameterSpec
唯一的解密,我得到未知的参数类型异常。
任何帮助表示赞赏