Vous êtes sur la page 1sur 2

Decryption of file using java

package com; import java.io.*; import java.security.*; import javax.crypto.*; import javax.crypto.spec.*; import java.util.*;

public class FileDecryptor { private static String filename; private static String password; private static FileInputStream inFile; private static FileOutputStream outFile; public static void main(String[] args) throws Exception { filename = "c:/poi-Order.xls.des"; String password = "super_secret"; inFile = new FileInputStream(filename); outFile = new FileOutputStream(filename); PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray()); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");

SecretKey passwordKey = keyFactory.generateSecret(keySpec); // Read in the previouly stored salt and set the iteration count. byte[] salt = new byte[8]; inFile.read(salt); int iterations = 100; PBEParameterSpec parameterSpec = new PBEParameterSpec(salt, iterations); // Create the cipher and initialize it for decryption. Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES"); cipher.init(Cipher.DECRYPT_MODE, passwordKey, parameterSpec); byte[] input = new byte[(int)new File("c:/poi-Order.xls").length()];//new byte[64]; int bytesRead; while ((bytesRead = inFile.read(input)) != -1) { byte[] output = cipher.update(input, 0, bytesRead); if (output != null) outFile.write(output); } byte[] output = cipher.doFinal(); if (output != null) outFile.write(output); inFile.close(); outFile.flush(); outFile.close(); }}

Vous aimerez peut-être aussi