Guest User

Untitled

a guest
Jan 17th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.17 KB | None | 0 0
  1. String encodedKey =
  2. Base64.getEncoder().encodeToString(secKey.getEncoded());
  3. System.out.println(secKey);
  4.  
  5. byte[] decodedKey = Base64.getDecoder().decode(line);
  6. SecretKey secKey = new SecretKeySpec(decodedKey, "AES");
  7. System.out.println(secKey);`
  8.  
  9. Exception in thread "main" java.security.InvalidKeyException:
  10. unsupported type
  11. at
  12. de.flexiprovider.api.BlockCipher.engineInit(BlockCipher.java:165)
  13. at de.flexiprovider.api.Cipher.engineInit(Cipher.java:68)
  14. at javax.crypto.Cipher.init(Cipher.java:1246)
  15. at javax.crypto.Cipher.init(Cipher.java:1186)
  16. at server.main(server.java:93)
  17.  
  18. import java.net.*;
  19. import java.io.*;
  20. import java.security.Security;
  21. import javax.crypto.*;
  22. import de.flexiprovider.api.keys.SecretKeySpec;
  23. import de.flexiprovider.core.FlexiCoreProvider;
  24. import de.flexiprovider.core.rijndael.RijndaelKeyFactory;
  25. import java.util.Base64;
  26. public class server {
  27. public final static int FILE_SIZE = 6022386;
  28. private Socket socket = null;
  29. private ServerSocket server = null;
  30. private DataInputStream in = null;
  31. int bytesRead;
  32. int current = 0;
  33. FileOutputStream fos = null;
  34. BufferedOutputStream bos = null;
  35. static SecretKey secKey = null;
  36.  
  37.  
  38. public server(int port)
  39. {
  40. // starts server and waits for a connection
  41. try
  42. {
  43. server = new ServerSocket(port);
  44. System.out.println("Server started");
  45. System.out.println("Waiting for a client ...");
  46. socket = server.accept();
  47. System.out.println("Client accepted");
  48.  
  49.  
  50.  
  51. // takes input from the client socket
  52. in = new DataInputStream(new
  53. BufferedInputStream(socket.getInputStream()));
  54. String line = in.readUTF();
  55.  
  56. byte[] decodedKey = Base64.getDecoder().decode(line);
  57. secKey = new SecretKeySpec(decodedKey, "AES");
  58.  
  59.  
  60.  
  61.  
  62.  
  63. byte [] mybytearray = new byte [FILE_SIZE];
  64. InputStream is = socket.getInputStream();
  65. fos = new FileOutputStream("cipher.txt");
  66. bos = new BufferedOutputStream(fos);
  67. bytesRead = is.read(mybytearray,0,mybytearray.length);
  68. current = bytesRead;
  69. do {
  70. bytesRead =
  71. is.read(mybytearray, current, (mybytearray.length-
  72. current));
  73. if(bytesRead >= 0) current += bytesRead;
  74. } while(bytesRead > -1);
  75. bos.write(mybytearray, 0 , current);
  76. bos.flush();
  77. System.out.println("File Received" );
  78.  
  79. // close connection
  80. socket.close();
  81. in.close();fos.close();bos.close();
  82. }
  83. catch(IOException i)
  84. {
  85. System.out.println(i);
  86. }
  87. }
  88. public static void main(String args[]) throws Exception
  89. {
  90. Security.addProvider(new FlexiCoreProvider());
  91. Cipher cipher = Cipher.getInstance("AES128_CBC", "FlexiCore");
  92. server ser = new server(5003);
  93.  
  94. byte[] block = new byte[8];
  95. int i;
  96.  
  97. String ciphertextFile = "cipher.txt";
  98. String cleartextAgainFile = "cleartextAgainSymm.txt";
  99.  
  100. System.out.println(secKey);
  101. cipher.init(Cipher.DECRYPT_MODE, secKey);
  102. FileInputStream fis = new FileInputStream(ciphertextFile);
  103. CipherInputStream cis = new CipherInputStream(fis, cipher);
  104.  
  105.  
  106. FileOutputStream fos = new FileOutputStream(cleartextAgainFile);
  107.  
  108. while ((i = cis.read(block)) != -1) {
  109. fos.write(block, 0, i);
  110. }
  111. fos.close();
  112. }
  113. }
  114.  
  115. Server started
  116. Waiting for a client ...
  117. Client accepted
  118. File Received
  119. de.flexiprovider.api.keys.SecretKeySpec@fffe8f6c
  120. Exception in thread "main" java.security.InvalidKeyException: unsupported type
  121. at de.flexiprovider.api.BlockCipher.engineInit(BlockCipher.java:165)
  122. at de.flexiprovider.api.Cipher.engineInit(Cipher.java:68)
  123. at javax.crypto.Cipher.init(Cipher.java:1246)
  124. at javax.crypto.Cipher.init(Cipher.java:1186)
  125.  
  126. import java.net.*;
  127. import java.io.*;
  128. import java.security.Security;
  129.  
  130. import javax.crypto.Cipher;
  131. import javax.crypto.CipherInputStream;
  132. import javax.crypto.CipherOutputStream;
  133. import javax.crypto.KeyGenerator;
  134. import javax.crypto.SecretKey;
  135.  
  136. import de.flexiprovider.api.keys.SecretKeySpec;
  137. import de.flexiprovider.core.FlexiCoreProvider;
  138.  
  139. import java.util.Arrays;
  140. import java.util.Base64;
  141.  
  142. public class Client {
  143.  
  144. static SecretKey secKey;
  145. // initialize socket and input output streams
  146. private Socket socket = null;
  147. private DataInputStream input = null;
  148. private DataOutputStream out = null;
  149. FileInputStream fis;
  150. File myFile;
  151. BufferedInputStream bis = null;
  152. OutputStream os = null;
  153.  
  154. public Client(String address, int port)
  155. {
  156.  
  157. // establish a connection
  158. try
  159. {
  160. socket = new Socket(address, port);
  161. System.out.println("Connected");
  162.  
  163. // takes input from terminal
  164. input = new DataInputStream(System.in);
  165.  
  166. // sends output to the socket
  167. out = new DataOutputStream(socket.getOutputStream());
  168.  
  169.  
  170. String encodedKey = Base64.getEncoder().encodeToString(secKey.getEncoded());
  171. out.writeUTF(encodedKey);
  172.  
  173. myFile = new File ("ciphertextSymm.txt");
  174. byte [] mybytearray = new byte [(int)myFile.length()];
  175. fis=new FileInputStream(myFile);
  176. bis = new BufferedInputStream(fis);
  177. bis.read(mybytearray,0,mybytearray.length);
  178. os = socket.getOutputStream();
  179. System.out.println("Sending ");
  180. os.write(mybytearray,0,mybytearray.length);
  181. os.flush();
  182. System.out.println("Done.");
  183.  
  184. }
  185. catch(UnknownHostException u)
  186. {
  187. System.out.println(u);
  188. }
  189. catch(IOException i)
  190. {
  191. System.out.println(i);
  192. }
  193.  
  194. // close the connection
  195. try
  196. {
  197. input.close();
  198. out.close();
  199. socket.close();
  200. bis.close();
  201. os.close();
  202. }
  203. catch(IOException x)
  204. {
  205. System.out.println(x);
  206. }
  207. }
  208. public static void main(String args[]) throws Exception
  209. {
  210. Security.addProvider(new FlexiCoreProvider());
  211. Cipher cipher = Cipher.getInstance("AES128_CBC", "FlexiCore");
  212. KeyGenerator keyGen = KeyGenerator.getInstance("AES", "FlexiCore");
  213. secKey = keyGen.generateKey();
  214.  
  215.  
  216. System.out.println(secKey);
  217. cipher.init(Cipher.ENCRYPT_MODE, secKey);
  218. String cleartextFile = "cleartext.txt";
  219. String ciphertextFile = "ciphertextSymm.txt";
  220.  
  221. FileInputStream fis = new FileInputStream(cleartextFile);
  222. FileOutputStream fos = new FileOutputStream(ciphertextFile);
  223. CipherOutputStream cos = new CipherOutputStream(fos, cipher);
  224.  
  225. byte[] block = new byte[8];
  226. int i;
  227. while ((i = fis.read(block)) != -1) {
  228. cos.write(block, 0, i);
  229. }
  230. cos.close();
  231.  
  232. Client client = new Client("127.0.0.1", 5003);
  233. }
  234. }
  235.  
  236. de.flexiprovider.core.rijndael.RijndaelKey@7c226727
  237. Connected
  238. Sending
  239. Done.
  240.  
  241. public static void main(String[] args) {
  242. try {
  243. //message to be encrypted
  244. final String message = "This is a test message";
  245. final String password = "password";
  246.  
  247. // Here the magic numbers
  248. final int pswdIterations = 65536;
  249. final int keySize = 128;
  250.  
  251. final byte[] saltBytes = {0, 1, 2, 3, 4, 5, 6};
  252.  
  253. SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
  254. PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), saltBytes, pswdIterations, keySize);
  255. SecretKey secretKey = factory.generateSecret(spec);
  256. SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES");
  257.  
  258. // Instantiate the cipher
  259. Cipher cipher = Cipher.getInstance("AES");
  260.  
  261. // Encrypt
  262. cipher.init(Cipher.ENCRYPT_MODE, secret);
  263. byte[] encrypted = cipher.doFinal(message.getBytes());
  264. System.out.println("Original string: " + message);
  265. System.out.println("Encrypted string: " + Arrays.toString(encrypted));
  266.  
  267. // Decrypt
  268. cipher.init(Cipher.DECRYPT_MODE, secret);
  269. byte[] decrypt_original = cipher.doFinal(encrypted);
  270. String decrypt_originalString = new String(decrypt_original);
  271. System.out.println("Decrypt string: " + decrypt_originalString);
  272. } catch (NoSuchAlgorithmException | InvalidKeySpecException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException ex) {
  273. Logger.getLogger(Sandbox.class.getName()).log(Level.SEVERE, null, ex);
  274. }
  275. }
  276.  
  277. stringKey = Base64.encodeToString(secretKey.getEncoded(), Base64.DEFAULT);
  278.  
  279. byte[] encodedKey = Base64.decode(stringKey, Base64.DEFAULT);
  280. SecretKey originalKey = new SecretKeySpec(encodedKey, 0, encodedKey.length, "AES");
  281.  
  282. String encodedKey = Base64.getEncoder().encodeToString(secretKey.getEncoded());
  283.  
  284. byte[] decodedKey = Base64.getDecoder().decode(encodedKey);
  285.  
  286. SecretKey originalKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, "AES");
Add Comment
Please, Sign In to add comment