我的问题是:我正试图在将用户的敏感数据保存到我的数据库之前对其进行加密。加密不是问题,问题在于解密部分。
我得到了以下内容:
我已经尝试在Cipher.init中加入提供程序或不加提供程序,但是得到了相同的错误。我在网上搜索了一些解决方案,但没有任何结果。我错过了什么或者做错了什么?此外,我也尝试了这个链接(Link)中的示例,但同样没有任何结果。
我从这个链接(link)获得了代码示例,但必须根据我的目的进行一些修改。
以下是我的代码,以便更好地理解。
KeyStoreAdapter.kt
我得到了以下内容:
07-22 16:25:38.941 18035-18035/io.nourish.strongerasfukk E/AndroidRuntime: FATAL EXCEPTION: main
Process: io.nourish.strongerasfukk, PID: 18035
java.lang.RuntimeException: Unable to start activity ComponentInfo{io.nourish.strongerasfukk/io.nourish.strongerasfukk.Register}: java.security.InvalidKeyException: Keystore operation failed
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2450)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5466)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.security.InvalidKeyException: Keystore operation failed
at android.security.KeyStore.getInvalidKeyException(KeyStore.java:692)
at android.security.KeyStore.getInvalidKeyException(KeyStore.java:712)
at android.security.keystore.KeyStoreCryptoOperationUtils.getInvalidKeyExceptionForInit(KeyStoreCryptoOperationUtils.java:54)
at android.security.keystore.KeyStoreCryptoOperationUtils.getExceptionForCipherInit(KeyStoreCryptoOperationUtils.java:89)
at android.security.keystore.AndroidKeyStoreCipherSpiBase.ensureKeystoreOperationInitialized(AndroidKeyStoreCipherSpiBase.java:263)
at android.security.keystore.AndroidKeyStoreCipherSpiBase.engineInit(AndroidKeyStoreCipherSpiBase.java:108)
at javax.crypto.Cipher.tryTransformWithProvider(Cipher.java:612)
at javax.crypto.Cipher.tryCombinations(Cipher.java:532)
at javax.crypto.Cipher.getSpi(Cipher.java:437)
at javax.crypto.Cipher.init(Cipher.java:815)
at javax.crypto.Cipher.init(Cipher.java:774)
at **io.nourish.strongerasfukk.utilities.Encryption.decrypt(Encryption.kt:28)
at io.nourish.strongerasfukk.Register.onCreate(Register.kt:54)**
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5466)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.security.KeyStoreException: Incompatible padding mode
at android.security.KeyStore.getKeyStoreException(KeyStore.java:632)
at android.security.KeyStore.getInvalidKeyException(KeyStore.java:712)
at android.security.keystore.KeyStoreCryptoOperationUtils.getInvalidKeyExceptionForInit(KeyStoreCryptoOperationUtils.java:54)
at android.security.keystore.KeyStoreCryptoOperationUtils.getExceptionForCipherInit(KeyStoreCryptoOperationUtils.java:89)
at android.security.keystore.AndroidKeyStoreCipherSpiBase.ensureKeystoreOperationInitialized(AndroidKeyStoreCipherSpiBase.java:263)
at android.security.keystore.AndroidKeyStoreCipherSpiBase.engineInit(AndroidKeyStoreCipherSpiBase.java:108)
at javax.crypto.Cipher.tryTransformWithProvider(Cipher.java:612)
at javax.crypto.Cipher.tryCombinations(Cipher.java:532)
at javax.crypto.Cipher.getSpi(Cipher.java:437)
at javax.crypto.Cipher.init(Cipher.java:815)
at javax.crypto.Cipher.init(Cipher.java:774)
at io.nourish.strongerasfukk.utilities.Encryption.decrypt(Encryption.kt:28)
at io.nourish.strongerasfukk.Register.onCreate(Register.kt:54)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5466)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
如您所见,这个问题涉及以下两行代码:
Encryption.kt(第28行)
cipher.init(Cipher.DECRYPT_MODE, key)
&
Register.kt(第54行)
var decryptedData = encryption.decrypt(encryptedData,masterKey?.private)
我已经尝试在Cipher.init中加入提供程序或不加提供程序,但是得到了相同的错误。我在网上搜索了一些解决方案,但没有任何结果。我错过了什么或者做错了什么?此外,我也尝试了这个链接(Link)中的示例,但同样没有任何结果。
我从这个链接(link)获得了代码示例,但必须根据我的目的进行一些修改。
以下是我的代码,以便更好地理解。
KeyStoreAdapter.kt
class KeyStoreAdapter {
private val kS = createAndroidKeyStore()
fun checkIfKeyExists(key: String) {
val keyStore = KeyStore.getInstance("AndroidKeyStore").apply { load(null) }
val alias = keyStore.aliases().toList()
if(!alias.contains(key)) {
val kpg: KeyPairGenerator = KeyPairGenerator.getInstance(KeyProperties.KEY_ALGORITHM_RSA, "AndroidKeyStore")
val parameterSpec: KeyGenParameterSpec = KeyGenParameterSpec.Builder("MasterKeys", KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT)
.setBlockModes(KeyProperties.BLOCK_MODE_GCM)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_PKCS1)
.build()
kpg.initialize(parameterSpec)
kpg.generateKeyPair()
}
}
fun getKeys(alias: String): KeyPair? {
val privateKey = kS.getKey(alias, null) as PrivateKey?
val publicKey = kS.getCertificate(alias)?.publicKey
return if(privateKey != null && publicKey != null) {
KeyPair(publicKey, privateKey)
} else {
null
}
}
private fun createAndroidKeyStore(): KeyStore {
val keyStore = KeyStore.getInstance("AndroidKeyStore")
keyStore.load(null)
return keyStore
}
Encryption.kt 加密类
class Encryption {
val cipher = Cipher.getInstance(Config.TRANSFORMATION)
fun encrypt(data: String, key: Key?): String {
cipher.init(Cipher.ENCRYPT_MODE, key)
val bytes = cipher.doFinal(data.toByteArray())
return Base64.encodeToString(bytes, Base64.DEFAULT)
}
fun decrypt(data: String, key: Key?): String {
cipher.init(Cipher.DECRYPT_MODE, key)
val encryptedData = Base64.decode(data, Base64.DEFAULT)
val decodedData = cipher.doFinal(encryptedData)
return String(decodedData)
}
}
const val TRANSFORMATION = "RSA/ECB/PKCS1Padding"
感谢所有人提前的支持!