我有这个问题与android> 8 我使用密钥包装器用rsa算法包装密钥。 这是课程的一部分
public byte[] wrap(SecretKey key) throws GeneralSecurityException {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1) {
OAEPParameterSpec sp = new OAEPParameterSpec("SHA-256",
"MGF1", new MGF1ParameterSpec("SHA-1"),
PSource.PSpecified.DEFAULT);
cipher.init(Cipher.WRAP_MODE, pair.getPublic(), sp);
} else {
cipher.init(Cipher.WRAP_MODE, pair.getPublic());
}
return cipher.wrap(key);
}
public SecretKey unwrap(byte[] blob) throws GeneralSecurityException {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1) {
OAEPParameterSpec sp = new OAEPParameterSpec("SHA-256",
"MGF1", new MGF1ParameterSpec("SHA-1"),
PSource.PSpecified.DEFAULT);
cipher.init(Cipher.UNWRAP_MODE, pair.getPrivate(), sp);
} else {
cipher.init(Cipher.UNWRAP_MODE, pair.getPrivate());
}
return (SecretKey) cipher.unwrap(blob, "AES", Cipher.SECRET_KEY);
}
我在android密钥库中生成对和存储。
问题是,当我尝试解开密钥时,我可以获得此异常
Unexpected parameters: javax.crypto.spec.OAEPParameterSpec@27572f4. No parameters supported
java.security.InvalidAlgorithmParameterException: Unexpected parameters: javax.crypto.spec.OAEPParameterSpec@27572f4. No parameters supported
at android.security.keystore.AndroidKeyStoreRSACipherSpi$PKCS1Padding.initAlgorithmSpecificParameters(AndroidKeyStoreRSACipherSpi.java:127)
at android.security.keystore.AndroidKeyStoreCipherSpiBase.engineInit(AndroidKeyStoreCipherSpiBase.java:147)
at javax.crypto.Cipher.tryTransformWithProvider(Cipher.java:2659)
at javax.crypto.Cipher.tryCombinations(Cipher.java:2570)
at javax.crypto.Cipher$SpiAndProviderUpdater.updateAndGetSpiAndProvider(Cipher.java:2475)
at javax.crypto.Cipher.chooseProvider(Cipher.java:566)
at javax.crypto.Cipher.init(Cipher.java:973)
at javax.crypto.Cipher.init(Cipher.java:908)
这种例外非常罕见。它可以每周或每月发射一次。所以主要的是钥匙被包裹一次并成功打开。一切正常,直到一个神奇的时刻,我无法打开关键异常。
有没有人遇到同样的事情或有任何线索?