Commit f5f731d3 authored by Romain PASSEREL's avatar Romain PASSEREL

Initial commit

parents
Pipeline #169 failed with stages
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>PMS-projetJava</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.7
File added
Le mot de passe administrateur est motdepasse123.
\ No newline at end of file
eP7}:ņ2OpZ7U7ʙ!Q2uf#RF0^Y] u,X
\ No newline at end of file
Le mot de passe administrateur est motdepasse123.
\ No newline at end of file
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.security.*;
import java.util.Arrays;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.io.File;
public class main {
public static void main(String[] args) {
// Gnration des deux utilisateurs Bob et alice
utilisateur Bob = new utilisateur();
utilisateur Alice = new utilisateur(); //lors de l'initialisation leurs cls RSA sont gnres automatiquement
//Alice veut envoyer le fichier secret.txt Bob
//Alice gnre donc une cl de chiffrement AES
byte[] AESkey = generateKey();
//Alice chiffre alors le fichier l'aide de la cl
File inputFile = new File("secret.txt");
File encryptedFile = new File("message.encrypted");
encryptFile(inputFile,encryptedFile,AESkey);
//Test pour vrifier le fonctionnement des fonctions encrypt et decrypt
// Rappel Ctrl + Shift + / pour uncomment une zone
//
// File decryptedFile = new File("decoded.txt");
// decryptFile(encryptedFile,decryptedFile,AESkey);
//Alice va maintenant chiffrer la cl l'aide de la cl public de Bob
byte[] encryptedKey = Bob.encryptKey(AESkey);
//Test pour vrifier si la encryptKey et decryptKey fonctionne bien
//
// System.out.println(encodeHex(AESkey));
// System.out.println(encryptedKey);
// System.out.println(encodeHex(Bob.decryptKey(encryptedKey)));
//Envoi du fichier et de la cl Bob
//Il aurait fallut mettre en place des fonctions d'envois mais elles ont peu d'intrt dans le cadre du cours
//Bob dchiffre la cl AES l'aide de sa cl prive
byte[] decryptedKey = Bob.decryptKey(encryptedKey);
// Bob va maintenant dchiffrer le fichier l'aide de la cl
File outputFile = new File("decoded.txt");
decryptFile(encryptedFile,outputFile,decryptedKey);
//pour vrifier si les deux fichier sont identiques on peut vrifier leurs Hashs
byte[] hashInput = checksum(inputFile);
byte[] hashOutput = checksum(outputFile);
if (Arrays.equals(hashInput, hashOutput)) {
System.out.println("La manipulation s'est droul correctement les hashs sont identiques");
} else {
System.out.println("Il y a du avoir une erreur quelque part...");
}
}
static byte[] generateKey() {
try {
KeyGenerator keyGen;
keyGen = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = new SecureRandom();
keyGen.init(256, secureRandom);
SecretKey key = keyGen.generateKey();
return key.getEncoded();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
static void encryptFile(File inputFile,File outputFile,byte[] key) {
try {
Key secretKey = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
FileInputStream inputStream = new FileInputStream(inputFile);
byte[] inputBytes = new byte[(int) inputFile.length()];
inputStream.read(inputBytes);
byte[] outputBytes = cipher.doFinal(inputBytes);
FileOutputStream outputStream = new FileOutputStream(outputFile);
outputStream.write(outputBytes);
inputStream.close();
outputStream.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
static void decryptFile(File inputFile,File outputFile,byte[] key){
try {
Key secretKey = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
FileInputStream inputStream = new FileInputStream(inputFile);
byte[] inputBytes = new byte[(int) inputFile.length()];
inputStream.read(inputBytes);
byte[] outputBytes = cipher.doFinal(inputBytes);
FileOutputStream outputStream = new FileOutputStream(outputFile);
outputStream.write(outputBytes);
inputStream.close();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static String encodeHex(byte[] bytes) {
StringBuilder buf = new StringBuilder(bytes.length * 2);
int i;
for (i = 0; i < bytes.length; i++) {
if (((int)bytes[i] & 0xff) < 0x10) {
buf.append("0");
}
buf.append(Long.toString((int)bytes[i] & 0xff, 16));
}
return buf.toString();
}
public static byte[] checksum(File input) {
try (InputStream in = new FileInputStream(input)) {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] block = new byte[4096];
int length;
while ((length = in.read(block)) > 0) {
digest.update(block, 0, length);
}
return digest.digest();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
import java.security.*;
import javax.crypto.Cipher;
public class utilisateur {
private PrivateKey privateKey;
private PublicKey publicKey;
utilisateur() {
KeyPairGenerator keyGen;
try {
keyGen = KeyPairGenerator.getInstance("RSA");
SecureRandom secureRandom = new SecureRandom();
keyGen.initialize(2048,secureRandom);
KeyPair pair = keyGen.generateKeyPair();
this.privateKey = pair.getPrivate();
this.publicKey = pair.getPublic();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
PublicKey getPublicKey() {
return publicKey;
}
byte[] getBytePublicKey() {
return publicKey.getEncoded();
}
byte[] encryptKey(byte[] key) {
try {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(key);
}
catch (Exception e) {
e.printStackTrace();
return null;
}
}
byte[] decryptKey(byte[] encryptedKey) {
try {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return cipher.doFinal(encryptedKey);
}
catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment