Guest User

Untitled

a guest
Jul 15th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.10 KB | None | 0 0
  1. KeyGenerator kg = KeyGenerator.getInstance("AES");
  2. kg.init(128);
  3. SecretKey key = kg.generateKey();
  4. Cipher c = Cipher.getInstance("AES");
  5. c.init(Cipher.ENCRYPT_MODE, key);
  6. FileInputStream fis; FileOutputStream fos; CipherOutputStream cos;
  7. fis = new FileInputStream("FileTo.encrypt");
  8. fos = new FileOutputStream("Encrypted.file");
  9.  
  10. //write encrypted to file
  11. cos = new CipherOutputStream(fos, c);
  12. byte[] b = new byte[16];
  13. int i = fis.read(b);
  14. while (i != -1) {
  15. cos.write(b, 0, i);
  16. i = fis.read(b);
  17. }
  18. cos.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. }
  50. in.close();
  51. fos0.flush();
  52. fos0.close();
  53.  
  54. c1.update(b3);
  55.  
  56. package forums;
  57.  
  58. import java.io.*;
  59. import java.security.*;
  60. import javax.crypto.*;
  61. import 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. */
  69. public 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.  
  164. import javax.crypto.Cipher;
  165.  
  166. public 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)
  184. byte[] key = keyString.getBytes();
  185. System.out.println(asHex(key).toUpperCase());
  186.  
  187. String clearText = "sdhhgfffhamayaqqqaaaa";
  188. // ZXNzYXNlbmhhZWhmcmFjYQ== (Base64)
  189. // 6573736173656E686165686672616361 (Hex)
  190. byte[] clear = clearText.getBytes();
  191. System.out.println(asHex(clear).toUpperCase());
  192.  
  193. SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
  194. // PKCS5Padding or NoPadding
  195. Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
  196. cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
  197.  
  198. byte[] encrypted = cipher.doFinal(clear);
  199. System.out.println(asHex(encrypted).toUpperCase());
  200. cipher.init(Cipher.DECRYPT_MODE, skeySpec);
  201. byte[] original =
  202. cipher.doFinal(encrypted);
  203.  
  204. System.out.println(original);
  205. String originalString = new String(original);
  206. System.out.println("Original string: " +
  207. originalString + " " + asHex(original));
  208. }
  209. }
Add Comment
Please, Sign In to add comment