2013-12-12 2 views
9

Android Keystore에서 RSA 키 쌍을 생성하고 싶습니다. Android 4.3은 Android 시스템 Keystore에서 RSA 키를 생성 할 수 있어야합니다.Android 키 스토어 비공개 지수를 추출 할 수 없습니다.

public static byte[] RSAEncrypt(final byte[] plain) 
     throws NoSuchAlgorithmException, NoSuchPaddingException, 
     InvalidKeyException, IllegalBlockSizeException, BadPaddingException { 

    Cipher cipher = Cipher.getInstance("RSA"); 
    System.out.println("RSA Encryption key: " + publicKey.getAlgorithm()); 
    System.out.println("RSA Encryption key: " + publicKey.getEncoded()); 

    cipher.init(Cipher.ENCRYPT_MODE, publicKey); 
    byte[] encryptedBytes = cipher.doFinal(plain); 
    return encryptedBytes; 
} 

해독 :

public static byte[] RSADecrypt(final byte[] encryptedBytes) 
     throws NoSuchAlgorithmException, NoSuchPaddingException, 
     InvalidKeyException, IllegalBlockSizeException, BadPaddingException { 

    Cipher cipher1 = Cipher.getInstance("RSA"); 

    System.out.println("RSA Encryption key: " + privateKey.getAlgorithm()); 
    System.out.println("RSA Encryption key: " + privateKey.getEncoded()); 

    cipher1.init(Cipher.DECRYPT_MODE, privateKey); 
    byte[] decryptedBytes = cipher1.doFinal(encryptedBytes); 
    return decryptedBytes; 
} 

암호 해독에 (잘 작동) (또한 작동)처럼

 Calendar notBefore = Calendar.getInstance(); 
     Calendar notAfter = Calendar.getInstance(); 
     notAfter.add(1, Calendar.YEAR); 
     KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder(ctx) 
       .setAlias("key") 
       .setSubject(
         new X500Principal(String.format("CN=%s, OU=%s", 
           "key", ctx.getPackageName()))) 
       .setSerialNumber(BigInteger.ONE) 
       .setStartDate(notBefore.getTime()) 
       .setEndDate(notAfter.getTime()).build(); 
      KeyPairGenerator kpg; 
      kpg = KeyPairGenerator.getInstance("RSA", "AndroidKeyStore"); 
      kpg.initialize(spec); 
      KeyPair kp = kpg.genKeyPair(); 
      publicKey = kp.getPublic(); 
      privateKey = kp.getPrivate(); 

내 RSA 암호화 보이는 의해

나는 나의 RSA 키를 생성 함수는 다음과 같은 오류 메시지가 나타납니다 (privateKey가 인코딩되고 cipher1.init()에있는 경우) :

12-12 21:49:40.338: E/AndroidRuntime(20423): FATAL EXCEPTION: main 
12-12 21:49:40.338: E/AndroidRuntime(20423): java.lang.UnsupportedOperationException: private exponent cannot be extracted 
12-12 21:49:40.338: E/AndroidRuntime(20423): at org.apache.harmony.xnet.provider.jsse.OpenSSLRSAPrivateKey.getPrivateExponent(OpenSSLRSAPrivateKey.java:143) 

나는 그것을 얻지 못한다. Android KeyStore에서 RSA 키를 생성 할 수 있습니까? 누구나 Android KeyStore에서 RSA 키를 생성하고 개인 키로 해독하는 예를 제공 할 수 있습니까?

많은 분들께 미리 감사드립니다.

답변

14

the code에 따르면, I에는 OpenSSL 공급자가 키가 장치에 생성 된 때 내보낼 비공개 지수를 방지한다고 생각합니다.

@Override 
public final BigInteger getPrivateExponent() { 
    if (key.isEngineBased()) { 
     throw new UnsupportedOperationException("private exponent cannot be extracted"); 
    } 

    ensureReadParams(); 
    return privateExponent; 
} 

따라서, 당신은 아마 당신이 암호 인스턴스를 검색 할 때 동일한 암호화 공급자를 사용하도록 지정해야합니다. 이 업체 supports these RSA ciphers :

  • RSA/ECB 당신은 암호의 인스턴스이 방법을 만들어야합니다

  • RSA/ECB/PKCS1Padding/NoPadding
  • 가 :

    Cipher cipher1 = Cipher.getInstance("RSA/ECB/PKCS1Padding", "AndroidOpenSSL"); 
    
    +1

    'BigInteodeger'이해야 'BigInteger'가되어야하지만 6 자 미만으로 변경하여 편집 할 수는 없습니다. : –

    +1

    Mark 감사합니다. 게시물을 업데이트했습니다. – Jcs

    +2

    SpongyCastle을 사용하여 이전 버전과의 호환성을 유지할 수있는 방법이 있습니까? – zubietaroberto

    관련 문제