1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
| import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.security.GeneralSecurityException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
/**
*
* @author Absil Romain
*/
public class AESEncryptor
{
/**
* Generates a secret key for AES encrypting and write it in a file.
* @param outputFileNamethe file to write the generated key in.
**/
public static void generateKey(String outputFileName)
{
try
{
KeyGenerator keygen = KeyGenerator.getInstance("AES");
SecureRandom random = new SecureRandom();
keygen.init(random);
SecretKey key = keygen.generateKey();
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(outputFileName));
out.writeObject(key);
out.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
/**
* Encryts or decryts (depends on mode) the file inputFileName, and save
* the result in the file outputFileName.
* @param inputFileName the file to encryt or decrypt.
* @param outputFileName the file to save the result of encryting or
* decrypting.
* @param ode the mode : encryption (Cipher.ENCRYPT_MODE) or decryption
* (Cipher.DECRYPT_MODE).
* @see javax.crypto.Cipher
**/
public static void encryptFile(String inputFileName, String outputFileName, String keyFileName, int mode)
{
InputStream in = null;
OutputStream out = null;
try
{
ObjectInputStream keyIn = new ObjectInputStream(
new FileInputStream(keyFileName));
Key key = (Key)keyIn.readObject();
keyIn.close();
in = new FileInputStream(inputFileName);
out = new FileOutputStream(outputFileName);
Cipher cipher = Cipher.getInstance("AES");
cipher.init(mode, key);
crypt(in, out, cipher);
in.close();
out.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
private static void crypt(InputStream in, OutputStream out, Cipher cipher)
throws IOException, GeneralSecurityException
{
int blockSize = cipher.getBlockSize();
int outputSize = cipher.getOutputSize(blockSize);
byte[] inBytes = new byte[blockSize];
byte[] outBytes = new byte[outputSize];
int inLength = 0;
boolean done = false;
while(!done)
{
inLength = in.read(inBytes);
if(inLength == blockSize)
{
int outLength = cipher.update(inBytes, 0, blockSize, outBytes);
out.write(outBytes, 0, outLength);
}
else
done = true;
}
if(inLength > 0)
outBytes = cipher.doFinal(inBytes, 0, inLength);
else
outBytes = cipher.doFinal();
out.write(outBytes);
}
public static void main(String[] args){
AESEncryptor.generateKey("key/k.r2o");
//AESEncryptor.encryptFile("test/ch10-crypto.pdf","test/newFileAES","key/k.r2o", Cipher.ENCRYPT_MODE);
AESEncryptor.encryptFile("test/newFileAES", "test/ch10-cryptoN.pdf","key/k.r2o", Cipher.DECRYPT_MODE);
}
} |
Partager