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 키를 생성하고 개인 키로 해독하는 예를 제공 할 수 있습니까?
많은 분들께 미리 감사드립니다.
'BigInteodeger'이해야 'BigInteger'가되어야하지만 6 자 미만으로 변경하여 편집 할 수는 없습니다. : –
Mark 감사합니다. 게시물을 업데이트했습니다. – Jcs
SpongyCastle을 사용하여 이전 버전과의 호환성을 유지할 수있는 방법이 있습니까? – zubietaroberto