Signer et chiffrer
Fermé
mayanis26
Messages postés
14
Date d'inscription
vendredi 1 avril 2016
Statut
Membre
Dernière intervention
21 février 2017
-
7 mai 2016 à 17:40
mayanis26 Messages postés 14 Date d'inscription vendredi 1 avril 2016 Statut Membre Dernière intervention 21 février 2017 - 14 mai 2016 à 21:16
mayanis26 Messages postés 14 Date d'inscription vendredi 1 avril 2016 Statut Membre Dernière intervention 21 février 2017 - 14 mai 2016 à 21:16
A voir également:
- Signer et chiffrer
- Signer un document word - Guide
- Comment signer un pdf sans l'imprimer - Guide
- Clavier iphone chiffre et lettre - Guide
- Comment signer un mail sur téléphone - Guide
- Signer pdf mac - Guide
1 réponse
greg6614
Messages postés
592
Date d'inscription
vendredi 7 août 2009
Statut
Membre
Dernière intervention
3 juin 2017
107
13 mai 2016 à 17:41
13 mai 2016 à 17:41
Salut, sans code on ne peut rien faire.
Fais voir ce qu tu as et où ça coince.
Fais voir ce qu tu as et où ça coince.
14 mai 2016 à 21:12
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
);
14 mai 2016 à 21:13
14 mai 2016 à 21:14
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;
}
14 mai 2016 à 21:14
14 mai 2016 à 21:16
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;
}
}