· 7 years ago · Jul 16, 2018, 01:18 AM
1KeyGenerator kg = KeyGenerator.getInstance("AES");
2kg.init(128);
3SecretKey key = kg.generateKey();
4 Cipher c = Cipher.getInstance("AES");
5c.init(Cipher.ENCRYPT_MODE, key);
6FileInputStream fis; FileOutputStream fos; CipherOutputStream cos;
7fis = new FileInputStream("FileTo.encrypt");
8fos = new FileOutputStream("Encrypted.file");
9
10//write encrypted to file
11cos = new CipherOutputStream(fos, c);
12byte[] b = new byte[16];
13int i = fis.read(b);
14while (i != -1) {
15 cos.write(b, 0, i);
16 i = fis.read(b);
17}
18cos.close();
19
20 //write key to file
21 byte[] keyEncoded = key.getEncoded();
22 FileOutputStream kos = new FileOutputStream("crypt.key");
23 kos.write(keyEncoded);
24 kos.close();
25
26//Load Key
27 FileInputStream fis2= new FileInputStream("a.key");
28 File f=new File("a.key");
29 long l=f.length();
30 byte[] b1=new byte[(int)l];
31 fis2.read(b1, 0, (int)l);
32
33
34
35 SecretKeySpec ks2=new SecretKeySpec(b1,"AES");
36
37 Cipher c1 = Cipher.getInstance("AES");
38 c1.init(Cipher.DECRYPT_MODE, ks2);
39 FileInputStream fis1=new FileInputStream("Encrypted.file");
40 CipherInputStream in= new CipherInputStream(fis1,c1);
41 FileOutputStream fos0 =new FileOutputStream("decrypted.file");
42 byte[] b3=new byte[1];
43 int ia=in.read(b3);
44 while (ia >=0)
45 {
46 c1.update(b3); //<-------remove this
47 fos0.write(b3, 0, ia);
48 ia=in.read(b3);
49 }
50in.close();
51fos0.flush();
52fos0.close();
53
54c1.update(b3);
55
56package forums;
57
58import java.io.*;
59import java.security.*;
60import javax.crypto.*;
61import javax.crypto.spec.*;
62
63/**
64 This program tests the DES cipher. Usage:
65 java DESTest -genkey keyfile
66 java DESTest -encrypt plaintext encrypted keyfile
67 java DESTest -decrypt encrypted decrypted keyfile
68*/
69public class DESTest
70{
71 private static void usage() {
72 System.err.print(
73 "This program tests the javax.crypto DES cipher package.n"
74 + "usage: java DESTest -genkey keyfilen"
75 + "java DESTest -encrypt plaintext encrypted keyfilen"
76 + "java DESTest -decrypt encrypted decrypted keyfilen"
77 );
78 }
79
80 public static void main(String[] args) {
81 if ( args.length < 2 || args.length > 4
82 || !args[0].matches("-genkey|-encrypt|-decrypt")
83 ) {
84 usage();
85 return;
86 }
87 try {
88 if ("-genkey".equals(args[0])) {
89 KeyGenerator keygen = KeyGenerator.getInstance("DES");
90 SecureRandom random = new SecureRandom();
91 keygen.init(random);
92 SecretKey key = keygen.generateKey();
93 ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(args[1]));
94 out.writeObject(key);
95 out.close();
96 } else {
97 int mode;
98 if ("-encrypt".equals(args[0])) {
99 mode = Cipher.ENCRYPT_MODE;
100 } else { //-decrypt
101 mode = Cipher.DECRYPT_MODE;
102 }
103
104 ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(args[3]));
105 Key key = (Key) keyIn.readObject();
106 keyIn.close();
107
108 InputStream in = new FileInputStream(args[1]);
109 OutputStream out = new FileOutputStream(args[2]);
110 Cipher cipher = Cipher.getInstance("DES");
111 cipher.init(mode, key);
112
113 crypt(in, out, cipher);
114 in.close();
115 out.close();
116 }
117 } catch (IOException exception) {
118 exception.printStackTrace();
119 } catch (GeneralSecurityException exception) {
120 exception.printStackTrace();
121 } catch (ClassNotFoundException exception) {
122 exception.printStackTrace();
123 }
124 }
125
126 /**
127 Uses a cipher to transform the bytes in an input stream
128 and sends the transformed bytes to an output stream.
129 @param in the input stream
130 @param out the output stream
131 @param cipher the cipher that transforms the bytes
132 */
133 public static void crypt(InputStream in, OutputStream out, Cipher cipher)
134 throws IOException, GeneralSecurityException
135 {
136 int blockSize = cipher.getBlockSize();
137 int outputSize = cipher.getOutputSize(blockSize);
138 byte[] inBytes = new byte[blockSize];
139 byte[] outBytes = new byte[outputSize];
140
141 int inLength = 0;;
142 boolean more = true;
143 while (more) {
144 inLength = in.read(inBytes);
145 if (inLength == blockSize) {
146 int outLength = cipher.update(inBytes, 0, blockSize, outBytes);
147 out.write(outBytes, 0, outLength);
148 System.out.println(outLength);
149 } else {
150 more = false;
151 }
152 }
153 if (inLength > 0) {
154 outBytes = cipher.doFinal(inBytes, 0, inLength);
155 } else {
156 outBytes = cipher.doFinal();
157 }
158 System.out.println(outBytes.length);
159 out.write(outBytes);
160 }
161
162}
163
164import javax.crypto.Cipher;
165
166public class AESTest {
167 public static String asHex (byte buf[]) {
168 StringBuffer strbuf = new StringBuffer(buf.length * 2);
169 int i;
170
171 for (i = 0; i < buf.length; i++) {
172 if (((int) buf[i] & 0xff) < 0x10)
173 strbuf.append("0");
174
175 strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
176 }
177
178 return strbuf.toString();
179 }
180
181 public static void main(String[] args) throws Exception {
182 String keyString = "ssssssssssssssss";
183// 546578746F2070617261207465737465 (Hex)
184byte[] key = keyString.getBytes();
185System.out.println(asHex(key).toUpperCase());
186
187String clearText = "sdhhgfffhamayaqqqaaaa";
188// ZXNzYXNlbmhhZWhmcmFjYQ== (Base64)
189// 6573736173656E686165686672616361 (Hex)
190byte[] clear = clearText.getBytes();
191System.out.println(asHex(clear).toUpperCase());
192
193SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
194// PKCS5Padding or NoPadding
195Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
196cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
197
198byte[] encrypted = cipher.doFinal(clear);
199System.out.println(asHex(encrypted).toUpperCase());
200cipher.init(Cipher.DECRYPT_MODE, skeySpec);
201byte[] original =
202 cipher.doFinal(encrypted);
203
204System.out.println(original);
205String originalString = new String(original);
206System.out.println("Original string: " +
207 originalString + " " + asHex(original));
208 }
209}