I want to convert an private key to an String so that user can store it in hard state .
i tried to convert the private key using Base64 but it gives me error stating privateKey.getEncoded() is null
So i tried before Base64 and again i got null .
public String newKey() throws
NoSuchAlgorithmException,
NoSuchProviderException,
InvalidAlgorithmParameterException, KeyStoreException, CertificateEncodingException, UnrecoverableEntryException {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM, KEYSTORE);
KeyGenParameterSpec keyGenParameterSpec = new KeyGenParameterSpec
.Builder(alias, KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
.setBlockModes(KeyProperties.BLOCK_MODE_ECB)
.setEncryptionPaddings(PADDING)
.build();
keyPairGenerator.initialize(keyGenParameterSpec);
PrivateKey key = keyPairGenerator.generateKeyPair().getPrivate();
KeyStore.PrivateKeyEntry entry = (KeyStore.PrivateKeyEntry) keyStore.getEntry(alias,null);
String a = privateKeyEntryToString(entry,key);
// java.util.Base64.getDecoder().decode(entry)
// Log.d("tag",entry);
Log.d("tag", "done in new Key returning keypublic");
return a;
// return keyPairGenerator.generateKeyPair().getPublic();
}
private String privateKeyEntryToString(KeyStore.PrivateKeyEntry privateKeyEntry,PrivateKey key) throws CertificateEncodingException {
Certificate certificate = privateKeyEntry.getCertificate();
if (key.getEncoded().length == 0){ // but here we get exception that it is null.
return "its empty";
}
// Convert PrivateKey and Certificate to String
String privateKeyString = Base64.encodeToString(key.getEncoded(), Base64.DEFAULT);
String certificateString = Base64.encodeToString(certificate.getEncoded(), Base64.DEFAULT);
// Combine both strings with a separator for later use
return privateKeyString + "-----BEGIN CERTIFICATE-----\n" + certificateString;
Stack trace :
FATAL EXCEPTION: main
Process: com.leo.nopasswordforyou, PID: 9525
java.lang.NullPointerException: Attempt to get length of null array
privateKeyEntryToString()function come from. You can get a PrivateKey object from that keystore, but it contains no sensitive private key information andgetEncoded()called on it will returnnull.getEndcodedwill never work for such keys.AndroidKeystore. Just search Stackoverflow how TK generate an RSA key in Java without Android. The Java way will also work on Android.