2017-02-28 11 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()를 사용하여 하드웨어 지원 여부를 확인합니다. 그러나, 나는 계속 cip.init (Cipher.ENCRYPT_MODE, entry.getPrivateKey())에서 Encrypt()의 InvalidKeyException을 얻는다. 정확한 예외는

java.security.InvalidKeyException: Keystore operation failed 

누구인지 알고 있습니까?

답변

1

암호화는 공개 키를 사용하는 경우에만 의미가 있습니다. 비공개 키로 "암호화"하면 실제로 서명을 만듭니다. Java/Android에는 별도의 클래스가 있습니다.

+0

좋아, 대신 개인 키로 서명을 시도했지만 잘못된 키 예외가 계속 발생합니다. 키 저장소 작업이 실패했습니다. Android에 RSA 서명/검증에 대한 예제 코드가 있습니까? – user1118764

+0

그럼 해결 했니? 일부 코드를 사용하여 직접 답변을 추가 할 수 있습니다. –

+0

아, 그래. 귀하의 대답은 정확합니다. github에 예제 코드가 있기 때문에 개인 키를 사용하여 암호화 할 수 있다는 인상을 받았습니다. 분명히 작동하지 않습니다. – user1118764