· 9 years ago · Jan 28, 2017, 05:42 AM
1import java.io.File;
2import java.io.IOException;
3import java.io.UnsupportedEncodingException;
4import java.nio.file.Files;
5import java.security.InvalidKeyException;
6import java.security.NoSuchAlgorithmException;
7import java.security.spec.InvalidKeySpecException;
8import javax.crypto.BadPaddingException;
9import javax.crypto.Cipher;
10import javax.crypto.IllegalBlockSizeException;
11import javax.crypto.NoSuchPaddingException;
12import javax.crypto.SecretKey;
13import javax.crypto.SecretKeyFactory;
14import javax.crypto.spec.DESKeySpec;
15
16public class DesImplementer {
17
18public static void main(String[] args) throws NoSuchAlgorithmException,UnsupportedEncodingException, InvalidKeyException,
19 InvalidKeySpecException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, IOException {
20
21 String str;
22 SecretKeyFactory myKeyFactory = SecretKeyFactory.getInstance("DES");
23
24 byte[] myPassword = "password".getBytes("UTF8");
25 DESKeySpec mySecureKey = new DESKeySpec(myPassword);
26 SecretKey myDesKey = myKeyFactory.generateSecret(mySecureKey);
27 Cipher myDesCipher = Cipher.getInstance("DES");
28 myDesCipher.init(Cipher.ENCRYPT_MODE, myDesKey);
29 byte[] myPlainText = Files.readAllBytes(new File("C:\FileTesting\PreEncrypt.txt").toPath());
30 byte[] myCipherText = myDesCipher.doFinal(myPlainText);
31
32 Files.write(new File("C:\FileTesting\Encrypted.txt").toPath(), myCipherText);
33 }
34
35}
36
37package filetobits;
38
39import java.io.FileNotFoundException;
40import java.io.FileReader;
41import java.io.FileWriter;
42import java.io.IOException;
43
44public class FileToBits {
45
46 public static void main(String[] args) throws IOException, FileNotFoundException {
47
48 FileReader inputStream = new FileReader("C:\FileTesting\Encrypted.txt");
49 FileWriter outputStream = new FileWriter("C:\FileTesting\EncryptedBin.txt");
50
51 int c;
52 while ((c = inputStream.read()) != -1) {
53 outputStream.write(Integer.toBinaryString(c));
54 outputStream.write(System.lineSeparator());
55 }
56 inputStream.close();
57 outputStream.close();``
58 }
59}