1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| public static void main(String[] args) throws Exception {
DataEncrypt.createRSAKeys(); //call to generated RSA keys
DataEncrypt secure = new DataEncrypt();
// to encrypt a file
secure.makeKey(); // AES key symetric
secure.saveKey(new File("file1.txt"), "public1.key");
secure.encrypt(new File("file.txt"), new File("file2.txt"));
public void encrypt(File in, File out) throws IOException, InvalidKeyException {
if(!(in instanceof Serializable))
{
throw new IllegalArgumentException("Source object must implement Serializable.");
}
aesCipher.init(Cipher.ENCRYPT_MODE, aeskeySpec);
FileInputStream is = new FileInputStream(in);
CipherOutputStream os = new CipherOutputStream(new FileOutputStream(out), aesCipher);
copy(is, os);
os.flush();
os.close();
} |
Partager