Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. DataOutputStream dOut = newDataOutputStream(socket.getOutputStream());
  2.  
  3. byte[] s2 = Encrypt3.encrypt2(myString);
  4.  
  5. dOut.writeInt(s2.length); // write length of the message
  6. dOut.write(s2);
  7.  
  8. import java.security.Key;
  9.  
  10. import javax.crypto.Cipher;
  11.  
  12. import javax.crypto.spec.SecretKeySpec;
  13.  
  14.  
  15. public class Encrypt3 {
  16. public static String key = "mykey";
  17.  
  18. public static byte[] encrypt2(String text ){
  19. String encrypt ="";
  20.  
  21. try{
  22. // Create key and cipher
  23. Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
  24. Cipher cipher = Cipher.getInstance("AES");
  25. // encrypt the text
  26. cipher.init(Cipher.ENCRYPT_MODE, aesKey);
  27. byte[] encrypted = cipher.doFinal(text.getBytes());
  28. return encrypted ;
  29. }catch(Exception e){
  30. System.out.println(e);
  31. }
  32. return null ;
  33. }
  34.  
  35.  
  36. public static String decrypt2(byte[] encrypted2){
  37. String decrypt ="";
  38. try{
  39. Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
  40. Cipher cipher = Cipher.getInstance("AES");
  41. // decrypt the text
  42. cipher.init(Cipher.DECRYPT_MODE, aesKey);
  43. decrypt = new String(cipher.doFinal(encrypted2));
  44. }catch(Exception e){
  45. System.out.println(e);
  46. }
  47.  
  48. return decrypt ;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement