/* * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template */ package com.megatim.module.encryption.cipher.asymetric; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.nio.file.Files; import java.security.GeneralSecurityException; import java.security.InvalidKeyException; import java.security.KeyFactory; import java.security.NoSuchAlgorithmException; import java.security.PrivateKey; import java.security.PublicKey; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import java.util.logging.Level; import java.util.logging.Logger; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import org.apache.commons.codec.binary.Base64; /** * * @author STEPHANIE */ public class AsymetricCryptography { private Cipher cipher; public AsymetricCryptography() throws NoSuchAlgorithmException, NoSuchPaddingException { // this.cipher = Cipher.getInstance("RSA"); this.cipher = Cipher.getInstance("RSA"); } private PrivateKey getPrivate(String filename) throws Exception { byte[] keyBytes = Files.readAllBytes(new File(filename).toPath()); PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory kf = KeyFactory.getInstance("RSA"); return kf.generatePrivate(spec); } private PublicKey getPublic(String filename) throws Exception { byte[] keyBytes = Files.readAllBytes(new File(filename).toPath()); X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes); KeyFactory kf = KeyFactory.getInstance("RSA"); return kf.generatePublic(spec); } private void encryptFile(byte[] input, File output, PrivateKey key) throws IOException, GeneralSecurityException { this.cipher.init(Cipher.ENCRYPT_MODE, key); writeToFile(output, this.cipher.doFinal(input)); } private void decryptFile(byte[] input, File output, PublicKey key) { try { this.cipher.init(Cipher.DECRYPT_MODE, key); writeToFile(output, this.cipher.doFinal(input)); } catch (InvalidKeyException | IllegalBlockSizeException | BadPaddingException | IOException ex) { Logger.getLogger(AsymetricCryptography.class.getName()).log(Level.SEVERE, ex.getMessage(), ex); } } private void writeToFile(File output, byte[] toWrite) throws IllegalBlockSizeException, BadPaddingException, IOException { FileOutputStream fos = new FileOutputStream(output); fos.write(toWrite); fos.flush(); fos.close(); } private String encryptText(String msg, PrivateKey key) throws NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException { this.cipher.init(Cipher.ENCRYPT_MODE, key); return Base64.encodeBase64String(cipher.doFinal(msg.getBytes("UTF-8"))); } private String decryptText(String msg, PublicKey key) throws InvalidKeyException, UnsupportedEncodingException, IllegalBlockSizeException, BadPaddingException { this.cipher.init(Cipher.DECRYPT_MODE, key); return new String(cipher.doFinal(Base64.decodeBase64(msg)), "UTF-8"); } public static String encryptText(String msg, String privateKeyPath) { try { AsymetricCryptography ac = new AsymetricCryptography(); PrivateKey privateKey = ac.getPrivate(privateKeyPath); return ac.encryptText(msg, privateKey); } catch (NoSuchAlgorithmException ex) { Logger.getLogger(AsymetricCryptography.class.getName()).log(Level.SEVERE, ex.getMessage(), ex); return null; } catch (NoSuchPaddingException ex) { Logger.getLogger(AsymetricCryptography.class.getName()).log(Level.SEVERE, ex.getMessage(), ex); return null; } catch (Exception ex) { Logger.getLogger(AsymetricCryptography.class.getName()).log(Level.SEVERE, ex.getMessage(), ex); return null; } } public static String decryptText(String msg, String publicKeyPath) { try { AsymetricCryptography ac = new AsymetricCryptography(); PublicKey publicKey = ac.getPublic(publicKeyPath); return ac.decryptText(msg, publicKey); } catch (NoSuchAlgorithmException ex) { Logger.getLogger(AsymetricCryptography.class.getName()).log(Level.SEVERE, ex.getMessage(), ex); return null; } catch (NoSuchPaddingException ex) { Logger.getLogger(AsymetricCryptography.class.getName()).log(Level.SEVERE, ex.getMessage(), ex); return null; } catch (Exception ex) { Logger.getLogger(AsymetricCryptography.class.getName()).log(Level.SEVERE, ex.getMessage(), ex); return null; } } public byte[] getFileInBytes(File f) throws IOException { FileInputStream fis = new FileInputStream(f); byte[] fbytes = new byte[(int) f.length()]; fis.read(fbytes); fis.close(); return fbytes; } /** * Fonction qui crypte un fichier * @param inputPath : chemin du fichier à crypter * @param outputPath : chemin vers les fichier crypté * @param privateKeyPath : chemin vers le fichier contenant la clé privée */ public static void encryptFile(String inputPath, String outputPath, String privateKeyPath) { try { AsymetricCryptography ac = new AsymetricCryptography(); PrivateKey privateKey = ac.getPrivate(privateKeyPath); ac.encryptFile(ac.getFileInBytes(new File(inputPath)), new File(outputPath), privateKey); } catch (Exception ex) { Logger.getLogger(AsymetricCryptography.class.getName()).log(Level.SEVERE, ex.getMessage(), ex); } } /** * Fonction qui décrypte un fichier * @param inputPath : chemin vers le fichier à décrypter * @param outputPath : chemin du fichier contenant les données décryptées * @param publicKeyPath : chemin vers le fichier contenant la clé publique */ public static void decryptFile(String inputPath, String outputPath, String publicKeyPath) { try { AsymetricCryptography ac = new AsymetricCryptography(); PublicKey publicKey = ac.getPublic(publicKeyPath); ac.decryptFile(ac.getFileInBytes(new File(inputPath)), new File(outputPath), publicKey); } catch (Exception ex) { Logger.getLogger(AsymetricCryptography.class.getName()).log(Level.SEVERE, ex.getMessage(), ex); } } }