Je suis actuellement stocker une clé RSA dans mon application Android, il doit être utilisé pour le cryptage in-app des données de l'application utilisateur.Où se trouve le fichier de clés?
Je Générez la clé et le stocker de l'enregistrer sous forme de fichier dans le répertoire clés comme ceci:
public RSA(char[] password) throws Exception
{
private static final String filePath = System.getProperty("user.dir") + "/keys/";
mCurrentPassword = password;
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
File possibleKeyFile = new File(filePath + "/" + keyAlias);
//FileInputStream keyFile = new FileInputStream(filePath + "/" + keyAlias);
if(!possibleKeyFile.exists()) //if keystore is empty, create a key
{
mCurrentCertificate = generateCert();
//Store the new keypair
ks.load(null, password);
KeyStore.ProtectionParameter protParam =
new KeyStore.PasswordProtection(password);
java.security.cert.Certificate[] myCert =
new java.security.cert.Certificate[] {
(java.security.cert.Certificate) mCurrentCertificate
};
KeyStore.PrivateKeyEntry pkEntry =
new KeyStore.PrivateKeyEntry(mCurrentRSAKeyPair.getPrivate(),
myCert);
ks.setEntry(keyAlias, pkEntry, protParam);
OutputStream os = new FileOutputStream(new File(filePath + "/" + keyAlias));
ks.store(os , password);
}
else
{
//retrieve keypair and assign it to mCurrentKeyPair
mCurrentRSAKeyPair = getKey(keyAlias, password);
}
}
Quand je vais récupérer le fichier de clés sur mon android en mode débogage je reçois une erreur indiquant le fichier n'existe pas (répertoire non valide). Je me demande où la clé est réellement stockée si le chemin de stockage est défini comme ça?
Codepour récupérer keypair:
private static KeyPair getKey(String alias, char[] password) throws Exception
{
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
FileInputStream keyFile = new FileInputStream(filePath + "/" + alias);
ks.load(keyFile, password);
KeyStore.ProtectionParameter protParam =
new KeyStore.PasswordProtection(password);
KeyStore.PrivateKeyEntry privateKeyEntry =
(KeyStore.PrivateKeyEntry) ks.getEntry(alias, protParam);
RSAPrivateKey privateKey = (RSAPrivateKey) privateKeyEntry.getPrivateKey();
//get public key from private key
KeyFactory kf = KeyFactory.getInstance("RSA");
RSAPrivateKeySpec priv = kf.getKeySpec(privateKey, RSAPrivateKeySpec.class);
RSAPublicKeySpec keySpec = new RSAPublicKeySpec(priv.getModulus(), BigInteger.valueOf(65537));
PublicKey publicKey = kf.generatePublic(keySpec);
KeyPair kp = new KeyPair(publicKey, privateKey);
return kp;
}
A quoi sert le paramètre filePath? C'est ce que vous stockez la clé à – dweebo