bonjour tout le monde
j'ai problème que je trouve pas d'abord de solution et je besoin de votre aide
voici mon problème j'ai écrit un programme sous android qui me permet
d'envoyer de sms et sa marche bien et aussi un programme en java qui crypte les message et je demande comment je peu appeler ma class java
MyRSA sous pour que mes soit crypter
pour le moment voici mes deux codes Sms et crypto
code pour crypter un message en java
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115 /*code pour envoyer des Sms sous android*/ package com.abdour.android.sms; import android.app.Activity; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.telephony.gsm.SmsManager; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class SMS extends Activity { Button btnSendSMS; EditText txtPhoneNo; EditText txtMessage; /** Appelé lorsque l'activité est d'abord crée . */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btnSendSMS = (Button) findViewById(R.id.btnSendSMS); txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo); txtMessage = (EditText) findViewById(R.id.txtMessage); btnSendSMS.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { String phoneNo = txtPhoneNo.getText().toString(); String message = txtMessage.getText().toString(); if (phoneNo.length()>0 && message.length()>0) sendSMS(phoneNo, message); else Toast.makeText(getBaseContext(), "desole.", Toast.LENGTH_SHORT).show(); } }); } /*Envoiyer un sms a un autre appareil*/ private void sendSMS(String phoneNumber, String message) { String SENT = "SMS_SENT"; String DELIVERED = "SMS_DELIVERED"; PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0); PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0); //---quand le sms a été envoyé --- registerReceiver(new BroadcastReceiver(){ @Override public void onReceive(Context arg0, Intent arg1) { switch (getResultCode()) { case Activity.RESULT_OK: Toast.makeText(getBaseContext(), "SMS Envoyé", Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_GENERIC_FAILURE: Toast.makeText(getBaseContext(), "", Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_NO_SERVICE: Toast.makeText(getBaseContext(), "pas de service", Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_NULL_PDU: Toast.makeText(getBaseContext(), "Null PDU", Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_RADIO_OFF: Toast.makeText(getBaseContext(), "Radio off", Toast.LENGTH_SHORT).show(); break; } } }, new IntentFilter(SENT)); //---si le sms a été livré--- registerReceiver(new BroadcastReceiver(){ @Override public void onReceive(Context arg0, Intent arg1) { switch (getResultCode()) { case Activity.RESULT_OK: Toast.makeText(getBaseContext(), "SMS delivered", Toast.LENGTH_SHORT).show(); break; case Activity.RESULT_CANCELED: Toast.makeText(getBaseContext(), "SMS not delivered", Toast.LENGTH_SHORT).show(); break; } } }, new IntentFilter(DELIVERED)); SmsManager sms = SmsManager.getDefault(); sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI); } }
j'ai besoin de votre aide merci
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175 //import java.io.*; import java.math.*; //import javax.crypto.*; import java.security.*; import java.security.spec.*; import java.security.interfaces.*; /** * Cette classe propose des méthodes permettant de crypter et décrypter des * messages avec l'algorithme RSA. Le message doit cependant être plus petit * que KEY_SIZE. */ public class MyRSA { public final static int KEY_SIZE = 1024; // [512..2048] private RSAPublicKey publicKey; private RSAPrivateKey privateKey; public MyRSA() { } public RSAPublicKey getPublicKey() { return publicKey; } public byte[] getPublicKeyInBytes() { return publicKey.getEncoded(); } public RSAPrivateKey getPrivateKey() { return privateKey; } public byte[] getPrivateKeyInBytes() { return privateKey.getEncoded(); } public void setPublicKey(RSAPublicKey publicKey) { this.publicKey = publicKey; } public void setPublicKey(byte[] publicKeyData) { try { X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicKeyData); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); publicKey = (RSAPublicKey)keyFactory.generatePublic(publicKeySpec); } catch (Exception e) {System.out.println(e);} } public void setPrivateKey(RSAPrivateKey privateKey) { this.privateKey = privateKey; } public void setPrivateKey(byte[] privateKeyData) { try { PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(privateKeyData); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); privateKey = (RSAPrivateKey)keyFactory.generatePrivate(privateKeySpec); } catch (Exception e) {System.out.println(e);} } public void generateKeyPair() { try { KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA"); keyPairGen.initialize(KEY_SIZE, new SecureRandom()); KeyPair kp = keyPairGen.generateKeyPair(); publicKey = (RSAPublicKey)kp.getPublic(); privateKey = (RSAPrivateKey)kp.getPrivate(); } catch (Exception e) {System.out.println(e);} } public byte[] crypt(byte[] plaintext) { return crypt(new BigInteger(addOneByte(plaintext))).toByteArray(); } public byte[] crypt(String plaintext) { return crypt(plaintext.getBytes()); } public byte[] decryptInBytes(byte[] ciphertext) { return removeOneByte(decrypt(new BigInteger(ciphertext)).toByteArray()); } public String decryptInString(byte[] ciphertext) { return new String(decryptInBytes(ciphertext)); } /** * Cette méthode permet de tester le bon fonctionnement des autres. */ public static void main(String[] args) { String plaintext ="slt" ;//args[0]; System.out.println("plaintext = " + plaintext); MyRSA rsa = new MyRSA(); rsa.generateKeyPair(); byte[] publicKey = rsa.getPublicKeyInBytes(); byte[] privateKey = rsa.getPrivateKeyInBytes(); byte[] ciphertext = rsa.crypt(plaintext); System.out.println("ciphertext = " + new BigInteger(ciphertext)); rsa.setPublicKey(publicKey); rsa.setPrivateKey(privateKey); String plaintext2 = rsa.decryptInString(ciphertext); System.out.println("plaintext2 = " + plaintext2); if (!plaintext2.equals(plaintext)) System.out.println("Error: plaintext2 != plaintext"); } private BigInteger crypt(BigInteger plaintext) { return plaintext.modPow(publicKey.getPublicExponent(), publicKey.getModulus()); } private BigInteger decrypt(BigInteger ciphertext) { return ciphertext.modPow(privateKey.getPrivateExponent(), privateKey.getModulus()); } /** * Ajoute un byte de valeur 1 au début du message afin d'éviter que ce dernier * ne corresponde pas à un nombre négatif lorsqu'il sera transformé en * BigInteger. */ private static byte[] addOneByte(byte[] input) { byte[] result = new byte[input.length+1]; result[0] = 1; for (int i = 0; i < input.length; i++) { result[i+1] = input[i]; } return result; } /** * Retire le byte ajouté par la méthode addOneByte. */ private static byte[] removeOneByte(byte[] input) { byte[] result = new byte[input.length-1]; for (int i = 0; i < result.length; i++) { result[i] = input[i+1]; } return result; } }
Partager