Advertisement
Guest User

Untitled

a guest
Apr 17th, 2014
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. public Boolean decryptSessionKey() {
  2.  
  3. // first, base64 decode the session key
  4. String sslString = "openssl base64 -d -in enc_sesskey -out temp";
  5.  
  6. try {
  7. Process p = Runtime.getRuntime().exec(sslString);
  8. } catch (IOException e2) {
  9. e2.printStackTrace();
  10. }
  11.  
  12. // now we can decrypt it
  13. try {
  14. sslString = "openssl rsautl -in temp -inkey privkey.pem -decrypt";
  15. Process p = Runtime.getRuntime().exec(sslString);
  16. BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
  17.  
  18. try {
  19. String s;
  20. while ((s = stdInput.readLine()) != null) {
  21. decrypted_session_password = s;
  22. writeToFile(decrypted_sesskey, s);
  23. }
  24. return true;
  25.  
  26. } catch (Exception e) {
  27. return false;
  28. }
  29. } catch (IOException e1) {
  30. return false;
  31. } catch (Exception e) {
  32. return false;
  33. }
  34. }
  35.  
  36. public Boolean encryptSessionKey(Cert receiver_cert) {
  37.  
  38. String sslString =
  39. "openssl rsautl base64 -in sesskey -out temp -inkey cert.pem -encrypt -certin";
  40.  
  41. // run this openssl encryption. Note that it will not yet be base64 encoded
  42. try {
  43. Process p = Runtime.getRuntime().exec(sslString);
  44. } catch (IOException e2) {
  45. e2.printStackTrace();
  46. }
  47.  
  48. // now we base64-encode the encrypted file
  49. sslString = "openssl base64 -in temp -out enc_sesskey";
  50.  
  51. try {
  52. Process p = Runtime.getRuntime().exec(sslString);
  53. } catch (IOException e1) {return false;
  54. } catch (Exception e) {return false;
  55. }
  56.  
  57. return true;
  58. }
  59.  
  60. openssl rsautl -encrypt -in sesskey -inkey cert.pem -certin -out temp
  61. openssl base64 -e -in temp -out enc_sesskey
  62.  
  63. openssl base64 -d -in enc_sesskey -out temp
  64. openssl rsautl -decrypt -in temp -inkey privkey.pem -out sesskey2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement