Encrypt a message in Java:
import java.io.*;
import javax.crypto.*;
import javax.crypto.spec.*;
public class encrypt {
public static void main(String args[]) throws Exception {
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
SecretKeySpec keySpec =
new SecretKeySpec("01234567890abcde".getBytes(), "AES");
IvParameterSpec ivSpec =
new IvParameterSpec("fedcba9876543210".getBytes());
cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
FileInputStream fis =
new FileInputStream(new File("plain.pdf"));
CipherInputStream cis = new CipherInputStream(fis, cipher);
FileOutputStream fos =
new FileOutputStream(new File("encrypted.pdf"));
byte[] b = new byte[8];
int i;
while ((i = cis.read(b)) != -1) {
fos.write(b, 0, i);
fos.flush(); fos.close();
cis.close(); fis.close();
The above Java program will encrypted PDF file. It takes the unencrypted PDF file (plain.pdf) and create
encrypted.pdf (the encrypted file).
Now with the encrypted.pdf, we use a PHP program to decrypt it:
<?php
$secret_key = "01234567890abcde";
$iv = "fedcba9876543210";
$infile = "encrypted.pdf";
$outfile = "decrypted.pdf";
$crypttext = file_get_contents($infile);
$plaintext = mcrypt_decrypt(MCRYPT_RIJNDAEL_128,
$secret_key, $crypttext, MCRYPT_MODE_CBC, $iv);
header('Content-Type: application/octet-stream');
header('Content-Length: ' . strlen($plaintext));
header('Content-Disposition: attachment; filename=' . ($outfile));
echo $plaintext;
?>
The PHP program takes the encrypted.pdf and force download the decrypted.pdf which should be the
same as the plain.pdf.
Encrypt a message in PHP and force it down the browser:
<?php
$secret_key = "01234567890abcde";
$iv = "fedcba9876543210";
$infile = "plain.pdf";
$outfile = "encrypted.pdf";
$plaintext = file_get_contents($infile);
$crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128,
$secret_key, $plaintext, MCRYPT_MODE_CBC, $iv);
header('Content-Type: application/octet-stream');
header('Content-Length: ' . strlen($crypttext));
header('Content-Disposition: attachment; filename=' . ($outfile));
echo $crypttext;
?>
The above PHP program will force an encrypted PDF file down from your browser. It takes the
unencrypted PDF file (plain.pdf) and force a download of encrypted.pdf (the encrypted file).
Now after saving the encrypted.pdf, we use a Java program to decrypt it:
import java.io.*;
import javax.crypto.*;
import javax.crypto.spec.*;
public class decrypt {
public static void main(String args[]) throws Exception {
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
SecretKeySpec keySpec = new SecretKeySpec("01234567890abcde".getBytes(), "AES");
IvParameterSpec ivSpec = new IvParameterSpec("fedcba9876543210".getBytes());
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
FileInputStream fis = new FileInputStream(new File("encrypted.pdf"));
CipherInputStream cis = new CipherInputStream(fis, cipher);
FileOutputStream fos = new FileOutputStream(new File("decrypted.pdf"));
byte[] b = new byte[8];
int i;
while ((i = cis.read(b)) != -1) {
fos.write(b, 0, i);
fos.flush(); fos.close();
cis.close(); fis.close();
?>
The Java program takes the encrypted.pdf and write out the decrypted.pdf which should be the same as
the plain.pdf.