2017-02-28 86 views
0

我試圖在硬件支持的Keystore中生成的KeyPair中對我的Android應用進行加密/解密。這是我的密鑰生成代碼:嘗試使用RSA私鑰進行加密時,Android應用中的InvalidKeyException

public void createKeys() { 
    Context ctx = getApplicationContext(); 
    Calendar start = new GregorianCalendar(); 
    Calendar end = new GregorianCalendar(); 
    end.add(Calendar.YEAR, 1); 
    try { 
     KeyPairGenerator kpGenerator = KeyPairGenerator.getInstance("RSA", "AndroidKeyStore"); 
     AlgorithmParameterSpec spec = null; 

     if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1 && Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { 
      spec = new KeyPairGeneratorSpec.Builder(ctx) 
        .setAlias(mAlias) 
        .setSubject(new X500Principal("CN=" + mAlias)) 
        .setSerialNumber(BigInteger.valueOf(1337)) 
        .setStartDate(start.getTime()) 
        .setEndDate(end.getTime()) 
        .build(); 
     } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
      spec = new KeyGenParameterSpec.Builder(mAlias, 
        KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT) 
        .setDigests(KeyProperties.DIGEST_SHA256, KeyProperties.DIGEST_SHA512) 
        .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_PKCS1) 
        .build(); 
     } 
     kpGenerator.initialize(spec); 
     KeyPair kp = kpGenerator.generateKeyPair(); 
    } catch (NoSuchAlgorithmException e) { 
     Log.w(TAG, "RSA not supported", e); 
    } catch (NoSuchProviderException e) { 
     Log.w(TAG, "No such provider: AndroidKeyStore"); 
    } catch (InvalidAlgorithmParameterException e) { 
     Log.w(TAG, "No such provider: AndroidKeyStore"); 
    } 
} 

這裏是我的加密代碼:

public String encrypt(String challenge) { 
    try { 
     KeyStore mKeyStore = KeyStore.getInstance("AndroidKeyStore"); 
     mKeyStore.load(null); 
     KeyStore.PrivateKeyEntry entry = (KeyStore.PrivateKeyEntry) mKeyStore.getEntry(mAlias, null); 
     Cipher cip = null; 
     cip = Cipher.getInstance("RSA/ECB/PKCS1Padding"); 
     cip.init(Cipher.ENCRYPT_MODE, entry.getPrivateKey()); 
     byte[] encryptBytes = cip.doFinal(challenge.getBytes()); 
     String encryptedStr64 = Base64.encodeToString(encryptBytes, Base64.NO_WRAP); 
     return encryptedStr64; 
    } catch (NoSuchAlgorithmException e) { 
     Log.w(TAG, "No Such Algorithm Exception"); 
     e.printStackTrace(); 
    } catch (UnrecoverableEntryException e) { 
     Log.w(TAG, "Unrecoverable Entry Exception"); 
     e.printStackTrace(); 
    } catch (KeyStoreException e) { 
     Log.w(TAG, "KeyStore Exception"); 
     e.printStackTrace(); 
    } catch (InvalidKeyException e) { 
     Log.w(TAG, "Invalid Key Exception"); 
     e.printStackTrace(); 
    } catch (NoSuchPaddingException e) { 
     Log.w(TAG, "No Such Padding Exception"); 
     e.printStackTrace(); 
    } catch (BadPaddingException e) { 
     Log.w(TAG, "Bad Padding Exception"); 
     e.printStackTrace(); 
    } catch (IllegalBlockSizeException e) { 
     Log.w(TAG, "Illegal Block Size Exception"); 
     e.printStackTrace(); 
    } catch (CertificateException e) { 
     Log.w(TAG, "Certificate Exception"); 
    } catch (IOException e) { 
     Log.w(TAG, "IO Exception", e); 
    } 
    return null; 
} 

密鑰生成成功完成。我還使用KeyInfo.isInsideSecureHardware()驗證它是否是硬件支持的。但是,我一直在encrypt()中的cip.init(Cipher.ENCRYPT_MODE,entry.getPrivateKey())行上收到InvalidKeyException。確切的例外是

java.security.InvalidKeyException: Keystore operation failed 

有沒有人知道爲什麼?

回答

1

如果您使用公鑰,加密只有意義。如果你用私鑰「加密」,那麼你實際上是在創建一個簽名。 Java/Android有一個單獨的類。

+0

好吧,所以我嘗試用私鑰簽名,而我仍然收到無效密鑰例外:密鑰庫操作失敗。 Android中有沒有RSA簽名/驗證的示例代碼? – user1118764

+0

那麼,你解決了嗎?你可以添加你自己的答案和一些代碼。 –

+0

哦,是的,我做到了。你的回答是正確的。我的印象是我可以使用私鑰進行加密,因爲github中有一個示例代碼就是這樣做的。顯然它不起作用。 – user1118764

相關問題