Bonjour,

je viens de trouver un exemple de chiffrement RSA sous Android et j'aurais aimé savoir si c'était la bonne méthode comparé à la description faite sur ce lien :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
 
import javax.crypto.Cipher;
 
import android.app.Activity;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.widget.TextView;
 
public class AsymmetricAlgorithmRSA extends Activity {
	static final String TAG = "AsymmetricAlgorithmRSA";
 
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
 
		setContentView(R.layout.asym);
 
		// Original text
		String theTestText = "This is just a simple test!";
		TextView tvorig = (TextView)findViewById(R.id.tvorig);
		tvorig.setText("\n[ORIGINAL]:\n" + theTestText + "\n");
 
		// Generate key pair for 1024-bit RSA encryption and decryption
		Key publicKey = null;
		Key privateKey = null;
		try {
			KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
			kpg.initialize(1024);
			KeyPair kp = kpg.genKeyPair();
			publicKey = kp.getPublic();
			privateKey = kp.getPrivate();		
		} catch (Exception e) {
			Log.e(TAG, "RSA key pair error");
		}
 
		// Encode the original data with RSA public key
		byte[] encodedBytes = null;
		try {
			Cipher c = Cipher.getInstance("RSA");
			c.init(Cipher.ENCRYPT_MODE, publicKey);
			encodedBytes = c.doFinal(theTestText.getBytes());
		} catch (Exception e) {
			Log.e(TAG, "RSA encryption error");
		}		
		TextView tvencoded = (TextView)findViewById(R.id.tvencoded);
		tvencoded.setText("[ENCODED]:\n" + 
				Base64.encodeToString(encodedBytes, Base64.DEFAULT) + "\n");
 
		// Decode the encoded data with RSA private key
		byte[] decodedBytes = null;
		try {
			Cipher c = Cipher.getInstance("RSA");
			c.init(Cipher.DECRYPT_MODE, privateKey);
			decodedBytes = c.doFinal(encodedBytes);
		} catch (Exception e) {
			Log.e(TAG, "RSA decryption error");
		}		
		TextView tvdecoded = (TextView)findViewById(R.id.tvdecoded);
		tvdecoded.setText("[DECODED]:\n" + new String(decodedBytes) + "\n");
	}
}
D'autre part, j'aimerais ne pas générer des clés à chaque fois car j'aimerai envoyer ma clé publique à d'autre device pour qu'ils puissent m'envoyer des messages.
Du coup, j'aimerais stocker ces clés dans le téléphone, mais de manière sécurisée. J'ai entendu que des applications stockent des données dans la carte SIM pour les sécuriser, mais je n'ai pas trouvé le moyen de le faire. Une idée ?

Merci d'avance !