密钥库操作失败:填充模式不兼容

时间:2019-03-07 14:54:00

标签: java android encryption kotlin android-keystore

我正在使用AndroidKeystore生成RSA密钥对,并使用该密钥对进行加密和解密

我的密钥对生成代码如下:

        var keypairGen:KeyPairGenerator = KeyPairGenerator.getInstance(KeyProperties.KEY_ALGORITHM_RSA, "AndroidKeyStore")
        var keyPairGeneratorSpec: KeyGenParameterSpec = KeyGenParameterSpec
        .Builder(this.Key_Name, KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT)
        .setDigests(KeyProperties.DIGEST_SHA256,  KeyProperties.DIGEST_SHA512)
        .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_PKCS1)
        .setRandomizedEncryptionRequired(false)
        .build()
    keypairGen.initialize(keyPairGeneratorSpec, SecureRandom())
    keypairGen.genKeyPair()

我的加密代码看起来像

    val keyStore:KeyStore = KeyStore.getInstance("AndroidKeyStore")
    keyStore.load(null)

    val String:String = "Test_String"

    val publicKey:PublicKey = keyStore.getCertificate(this.Key_Name).publicKey
    val privateKeyEntry:KeyStore.PrivateKeyEntry = keyStore.getEntry(this.Key_Name,null) as KeyStore.PrivateKeyEntry
    val privateKey:PrivateKey = privateKeyEntry.privateKey

    val cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding")
    cipher.init(Cipher.ENCRYPT_MODE,publicKey)
    val encryptedByte = cipher.doFinal(privateKeyString.toByteArray())
    val stringEncrypt:String = Base64.encodeToString(encryptedByte,Base64.DEFAULT)

还有我的解密代码

    val cipher1=Cipher.getInstance("RSA/ECB/PKCS1Padding")
    cipher1.init(Cipher.DECRYPT_MODE, privateKey);
    val decryptedBytes = cipher1.doFinal(Base64.decode(stringEncrypt,Base64.DEFAULT));
    val decrypted = String(decryptedBytes);

但是当我运行此加密时,工作正常,但是在解密中出现以下错误

    java.lang.RuntimeException: Unable to create service com.example.myapplication.service.Myservice: java.security.InvalidKeyException: Keystore operation failed
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:3201)
    at android.app.ActivityThread.-wrap5(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6119)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
 Caused by: java.security.InvalidKeyException: Keystore operation failed
    at android.security.KeyStore.getInvalidKeyException(KeyStore.java:727)
    at android.security.KeyStore.getInvalidKeyException(KeyStore.java:748)
    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:265)
    at android.security.keystore.AndroidKeyStoreCipherSpiBase.engineInit(AndroidKeyStoreCipherSpiBase.java:109)
    at javax.crypto.Cipher.tryTransformWithProvider(Cipher.java:2977)
    at javax.crypto.Cipher.tryCombinations(Cipher.java:2884)
    at javax.crypto.Cipher$SpiAndProviderUpdater.updateAndGetSpiAndProvider(Cipher.java:2789)
    at javax.crypto.Cipher.chooseProvider(Cipher.java:956)
    at javax.crypto.Cipher.init(Cipher.java:1199)
    at javax.crypto.Cipher.init(Cipher.java:1143)
    at com.example.myapplication.component.Component.encryptKey(Component.kt:82)
    at com.example.myapplication.service.Myservice.keypairGenerator(Myservice.kt:92)
    at com.example.myapplication.service.Myservice.onCreate(Myservice.kt:55)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:3191)
    at android.app.ActivityThread.-wrap5(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6119) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
 Caused by: android.security.KeyStoreException: Incompatible padding mode
    at android.security.KeyStore.getKeyStoreException(KeyStore.java:666)
    at android.security.KeyStore.getInvalidKeyException(KeyStore.java:748) 
    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:265) 
    at android.security.keystore.AndroidKeyStoreCipherSpiBase.engineInit(AndroidKeyStoreCipherSpiBase.java:109) 
    at javax.crypto.Cipher.tryTransformWithProvider(Cipher.java:2977) 
    at javax.crypto.Cipher.tryCombinations(Cipher.java:2884) 
    at javax.crypto.Cipher$SpiAndProviderUpdater.updateAndGetSpiAndProvider(Cipher.java:2789) 
    at javax.crypto.Cipher.chooseProvider(Cipher.java:956) 
    at javax.crypto.Cipher.init(Cipher.java:1199) 
    at javax.crypto.Cipher.init(Cipher.java:1143) 
    at com.example.myapplication.component.Component.encryptKey(Component.kt:82) 
    at com.example.myapplication.service.Myservice.keypairGenerator(Myservice.kt:92) 
    at com.example.myapplication.service.Myservice.onCreate(Myservice.kt:55) 
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:3191) 
    at android.app.ActivityThread.-wrap5(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6119) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

请帮我解决这个问题。我尝试了大多数填充和摘要的组合。

1 个答案:

答案 0 :(得分:0)

在解密之前尝试放置

 stringEncrypt = stringEncrypt.replace("/n","");
val decryptedBytes = cipher1.doFinal(Base64.decode(stringEncrypt,Base64.DEFAULT));
val decrypted = String(decryptedBytes);

如果您的密钥太长,则密钥是错误的,或者是Base64编码正在添加内容。因此看到大小不匹配。