Encryption AES et RSA

masterbeta Messages postés 4 Date d'inscription samedi 5 mars 2011 Statut Membre Dernière intervention 11 mars 2011 - 10 mars 2011 à 17:27
 walid50825000 - 27 mars 2013 à 12:55
Bonjour! J'ai le code suivant:(encryption AES 128b)

package ca.umontreal.ift2830.security;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class AESEncryption {

//Generate random 128bits key for AES
public static SecretKey generateKeyAES128() {
SecretKey secretKey=null;
try {
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
SecureRandom rand = SecureRandom.getInstance("SHA1PRNG");
//COMPLETER CECI
System.out.println("AES secred key generated successfully.+ \n");
}
catch (NoSuchAlgorithmException e) {
e.printStackTrace();
throw new RuntimeException();
}
return secretKey;
}

//Encrypt a plain text from a file using the random 128bits AES key
public static byte[] cryptWithAES(String plainFile, byte[] secretKey, String cipherFile) {

byte[] plain_text=null;
byte[] encrypted_text=null;

try{
FileInputStream fIn=null;
fIn=new FileInputStream(plainFile);
plain_text = new byte[fIn.available()];
fIn.read(plain_text);
fIn.close();

try {
Cipher cipher = Cipher.getInstance("AES");
// COMPLETER CECI

// Write to file
FileOutputStream fOut = null;
fOut = new FileOutputStream(cipherFile);
fOut.write(encrypted_text);
fOut.close();

} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
throw new RuntimeException();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
throw new RuntimeException();
} catch (InvalidKeyException e) {
e.printStackTrace();
throw new RuntimeException();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
throw new RuntimeException();
} catch (BadPaddingException e) {
e.printStackTrace();
throw new RuntimeException();
}
}
catch (FileNotFoundException e) {
e.printStackTrace();
throw new RuntimeException();
}
catch (IOException e){
e.printStackTrace();
throw new RuntimeException();
}
return encrypted_text;
}

//Decrypt an encrypted text form a file using AES key
public static byte[] decryptInBytesWithAES(String cipherFile, byte[] secretKey, String plainFile) {

byte[] cipher_text=null;
byte[] decrypted_text=null;

try{
FileInputStream fIn=null;
fIn=new FileInputStream(cipherFile);

cipher_text = new byte[fIn.available()];
fIn.read(cipher_text);
fIn.close();

try {
Cipher cipher = Cipher.getInstance("AES");
// COMPLETER CECI

// Write to file
FileOutputStream fOut = null;
fOut = new FileOutputStream(plainFile);
fOut.write(decrypted_text);
fOut.close();

} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
throw new RuntimeException();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
throw new RuntimeException();
} catch (InvalidKeyException e) {
e.printStackTrace();
throw new RuntimeException();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
throw new RuntimeException();
} catch (BadPaddingException e) {
e.printStackTrace();
throw new RuntimeException();
}
}
catch (FileNotFoundException e) {
e.printStackTrace();
throw new RuntimeException();
}
catch (IOException e){
e.printStackTrace();
throw new RuntimeException();
}
return decrypted_text;
}
}

encryption RSA

package ca.umontreal.ift2830.security;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.security.InvalidParameterException;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

public class RSAEncryption {

public static KeyPair keyPair;

// Generate private and public keys and save them to files
public static void setKeyPair(String privateKeyFile, String publicKeyFile) {

KeyPair keyPair = null;
try {
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
keyPairGen.initialize(1024);

System.out.println("RSA keys generated successfully. \n");
keyPair = keyPairGen.generateKeyPair();

// Write out keys to files
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream(privateKeyFile);
fOut.write(keyPair.getPrivate().getEncoded());
fOut.close();

// COMPLETER CECI POUR LA CLE PUBLIQUE
}
catch (FileNotFoundException e) {
e.printStackTrace();
throw new RuntimeException();
}
catch (IOException e) {
e.printStackTrace();
throw new RuntimeException();
}
}
catch (NoSuchAlgorithmException e) {
e.printStackTrace();
throw new RuntimeException();
}
catch (InvalidParameterException e) {
e.printStackTrace();
throw new RuntimeException();
}
}

//Read RSA public key from .key file
public static RSAPublicKey getPublicKey(String fileName){

RSAPublicKey publicKey=null;

try {
FileInputStream fIn=null;
fIn=new FileInputStream(fileName);
byte[] encKey = new byte[fIn.available()];
fIn.read(encKey);

X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encKey);

KeyFactory keyFactory = KeyFactory.getInstance("RSA");
publicKey=(RSAPublicKey)(keyFactory.generatePublic(publicKeySpec));
}
catch (FileNotFoundException e) {
e.printStackTrace();
throw new RuntimeException();
}
catch (IOException e){
e.printStackTrace();
throw new RuntimeException();
}
catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
catch (InvalidKeySpecException e) {
e.printStackTrace();
}
return publicKey;
}

//Read RSA private key from a file
public static RSAPrivateKey getPrivateKey(String fileName){

RSAPrivateKey privateKey=null;

try {
FileInputStream fIn=null;
fIn=new FileInputStream(fileName);
byte[] encKey = new byte[fIn.available()];
fIn.read(encKey);

PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encKey);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");

privateKey=(RSAPrivateKey)(keyFactory.generatePrivate(privateKeySpec));

}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e){
e.printStackTrace();
}
catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
catch (InvalidKeySpecException e) {
e.printStackTrace();
}

return privateKey;
}

// Encrypt a plain text using the public RSA key
public static byte[] cryptWithRSAPublicKey(byte[] plainText,RSAPublicKey publicKey) {
//COMPLETER CECI
}

// Decrypt an encrypted text using RSA key
public static byte[] decryptWithRSAPrivateKey(byte[] cipherText,RSAPrivateKey privateKey) {
//COMPLETER CECI
}

}

et enfin le Main Class

package ca.umontreal.ift2830.security;

import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;

public class MainClass {

public static void main(String args[]){

// Alice generates public and private keys and saves them to "private_rsa.key" and "public_rsa.key"
RSAEncryption.setKeyPair("private_rsa.key","public_rsa.key");

// Get RSA public and private keys from files
RSAPublicKey rsaPublicKey=RSAEncryption.getPublicKey("public_rsa.key");
RSAPrivateKey rsaPrivateKey=RSAEncryption.getPrivateKey("private_rsa.key");

// Bob generates a secret AES key
byte[] aesSecretKey=AESEncryption.generateKeyAES128().getEncoded();

//Bob encrypts his file using his secret key
byte[] encrypted_text = AESEncryption.cryptWithAES("texteclair.txt",aesSecretKey,"cipherText_bob.txt");
System.out.println("ENCRYPTED TEXT :"+new String(encrypted_text)+" \n");

// Bob encryptes k_key using Alice RSA public key
byte[] k_encrypted = RSAEncryption.cryptWithRSAPublicKey(aesSecretKey,rsaPublicKey);
System.out.println("ENCRYPTED AES KEY :"+new String(k_encrypted)+" \n");

// Alice decrypts Bob AES symmetric key using her private RSA key
byte[] k_decrypted =RSAEncryption.decryptWithRSAPrivateKey(k_encrypted,rsaPrivateKey);
System.out.println("DECRYPTED AES KEY :"+new String(k_decrypted)+" \n");

// Alice decrypts Bob text using Bob AES symmetric key
byte[] decrypted_text= AESEncryption.decryptInBytesWithAES("cypherText_bob.txt", k_decrypted,"texteclair_alice.txt");
System.out.println("DECRYPTED TEXT :"+new String(decrypted_text)+" \n");
}
}

La probleme est que dans les places ou est ecrit //COMPLETER CECI, je doit completer le code mais pas beaucoup des idees comment je peux faire ca...

la condition est suivante: Il vous est demande d'ecrire un programme pour :
{ Generer une cle privee et une cle publique RSA de 2048 bits.
{ Generer de maniere (pseudo) aleatoire des cles symetriques de 128 bits.
{ Chi rer avec RSA en utilisant la cle publique.
{ Dechi rer avec RSA en utilisant la cle privee.
Partie 2 : Chi rement AES (4 pts)
Il vous est demande d'ecrire un programme pour :
{ Chi rer avec AES en utilisant une cle symetrique de 128 bits.
{ Dechi rer avec AES en utilisant une cle symetrique de 128 bits

SVP aide moi!

2 réponses

cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
14 mars 2011 à 17:45
Jette un oeil à cette source, il y a tout : http://www.javafr.com/codes/CRYPTOGRAPHIE_52760.aspx
0
walid50825000
27 mars 2013 à 12:55
Je veux votre aide s'il vous plaît
Au bord d'une image (converti en une tab octets puis crypt) image cryptée est obtenue. Processus normale

Le déchiffrement inverse ((converti en un tableau d'octets, puis décrypté) me montre une erreur. Outre le bloc inappropriée
J'espère que votre aide s'il vous plaît.
0
Rejoignez-nous