Signer et chiffrer
mayanis26
Messages postés
14
Date d'inscription
Statut
Membre
Dernière intervention
-
mayanis26 Messages postés 14 Date d'inscription Statut Membre Dernière intervention -
mayanis26 Messages postés 14 Date d'inscription Statut Membre Dernière intervention -
bonjour svp j'ai un souci jai créer un client et un serveur je travaille avec les socket bref c pa ca le probleme jai cree pour les 2 des clés RSA ,je voudrais envoyé du serveur au client un Na(nombre aleatoire) signer et chiffrer avec la clé publique de client ca marche tres bien je le recupere chez le client bien mais des que jesseye de designer pour dechiffrer il me recupere du chinois donc si quelqun a deja eu ca please help me ! jattend vos reponses .
A voir également:
- Signer et chiffrer
- Signer un document word - Guide
- Clavier iphone chiffre et lettre - Guide
- Signer pdf gratuit sans inscription - Guide
- Signer pdf iphone - Guide
- Comment signer un mail sur téléphone - Guide
rsaServer.setPublicKey(publicKeyServer);
byte[] yan= rsaServer.crypt(may);
System.out.println("crypté 1 est : "+Arrays.toString(yan));
rsaServer.setPublicKey(publicKeyServer);
byte[] maya= rsaServer.crypt(yan);
System.out.println("crypté 2 est : "+Arrays.toString(maya));
rsaServer.setPrivateKey(privateKeyServer);
String p= converterByteToString(maya);
byte[] lyl=rsaServer.decryptInBytes(converterStringToByte(p));
System.out.println("decrypté 2 est : "+Arrays.toString(lyl));
rsaServer.setPrivateKey(privateKeyServer);
String lyl1=rsaServer.decryptInString(lyl);
System.out.println("decrypté 1 est : "+lyl1
);
private static byte[] converterStringToByte(String chaine){
String[] yaya = chaine.split(",");
byte[] entier = new byte[yaya.length];
for (int i = 0; i < yaya.length; i++) {
entier[i] = (byte) Integer.parseInt(yaya[i]);
}
return entier;
}
private static String converterByteToString(byte[] element){
String pkServer = Arrays.toString(element);
String pkSrv =pkServer.replace("[", "");
String pkSrv2 = pkSrv.replace("]", "");
String pk = pkSrv2.replace(" ", "");
return pk;
}
package badache;
import java.math.*;
import java.security.*;
import java.security.spec.*;
import java.security.interfaces.*;
public class MyRSA {
public final static int KEY_SIZE = 1024;
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));
}
public BigInteger crypt(BigInteger plaintext) {
return plaintext.modPow(publicKey.getPublicExponent(), publicKey.getModulus());
}
BigInteger decrypt(BigInteger ciphertext) {
return ciphertext.modPow(privateKey.getPrivateExponent(), privateKey.getModulus());
}
public 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;
}
public 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;
}
}