Advertisement
Guest User

Untitled

a guest
May 5th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.64 KB | None | 0 0
  1. Insert Your Password
  2. AwDfTgY!"£QwErT&/()^^**
  3. Insert The Message to Encrypt
  4. Message
  5.  
  6. java.security.spec.InvalidKeySpecException: Password is not ASCII
  7. at com.sun.crypto.provider.PBEKey.<init>(PBEKey.java:62)
  8. at com.sun.crypto.provider.PBEKeyFactory.engineGenerateSecret(PBEKeyFactory.java:218)
  9. at javax.crypto.SecretKeyFactory.generateSecret(SecretKeyFactory.java:336)
  10. at SPCryptography.encryptTextWithPassword(SPCryptography.java:36)
  11. at SPCryptography.main(SPCryptography.java:203)
  12. Exception in thread "main" java.lang.NullPointerException
  13. at SPCryptography.encryptTextWithPassword(SPCryptography.java:45)
  14. at SPCryptography.main(SPCryptography.java:203)
  15.  
  16. Insert Your Password
  17. Mqweqw123/*Qwerty/()
  18. Insert The Message to Encrypt
  19. Message To Hide
  20. Original: Message To Hide
  21. Encrypted à+–.Ù‘ô°¹æyþ–xl
  22. Retype Password
  23. Mqweqw123/*Qwerty/()
  24. Decrypted Message To Hide
  25.  
  26. public class SPCryptography
  27. {
  28. private static final byte[] SALT = { (byte) 0x29, (byte) 0x5F, (byte) 0x05, (byte) 0x97, (byte) 0x12, (byte) 0x01,(byte) 0x02, (byte) 0x09 };
  29. private static final int ITER = 5;
  30. private static KeySpec keySpec = null;
  31. private static SecretKey secretKey = null;
  32.  
  33. private SPCryptography()
  34. {
  35. }
  36.  
  37. public static byte[] encryptTextWithPassword(char[] password,String message)
  38. {
  39. keySpec = new PBEKeySpec(password, SALT, ITER);
  40. try
  41. {
  42. secretKey = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
  43. }
  44. catch (InvalidKeySpecException | NoSuchAlgorithmException e)
  45. {
  46. e.printStackTrace();
  47. }
  48. Cipher cipher = null;
  49. try
  50. {
  51. cipher = Cipher.getInstance(secretKey.getAlgorithm());
  52. }
  53. catch (NoSuchAlgorithmException | NoSuchPaddingException e)
  54. {
  55. e.printStackTrace();
  56. }
  57. AlgorithmParameterSpec algoParamSpec = new PBEParameterSpec(SALT, ITER);
  58. try
  59. {
  60. cipher.init(Cipher.ENCRYPT_MODE, secretKey,algoParamSpec);
  61. }
  62. catch (InvalidKeyException | InvalidAlgorithmParameterException e)
  63. {
  64.  
  65. e.printStackTrace();
  66. }
  67. byte[] encryptedByte = null;
  68. try
  69. {
  70. encryptedByte = cipher.doFinal(message.getBytes(StandardCharsets.UTF_8));
  71. }
  72. catch (IllegalBlockSizeException | BadPaddingException e)
  73. {
  74. e.printStackTrace();
  75. }
  76. return encryptedByte;
  77. }
  78.  
  79.  
  80. public static byte[] decryptTextWithPassword(char[] password,byte[] enMex)
  81. {
  82. keySpec = new PBEKeySpec(password, SALT, ITER);
  83. try
  84. {
  85. secretKey = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
  86. }
  87. catch(Throwable t){t.printStackTrace();}
  88. Cipher cipher = null;
  89. try
  90. {
  91. cipher = Cipher.getInstance(secretKey.getAlgorithm());
  92. } catch (NoSuchAlgorithmException | NoSuchPaddingException e)
  93. {
  94. e.printStackTrace();
  95. }
  96. AlgorithmParameterSpec algoParamSpec = new PBEParameterSpec(SALT, ITER);
  97. try
  98. {
  99. cipher.init(Cipher.DECRYPT_MODE, secretKey, algoParamSpec);
  100. }
  101. catch (InvalidKeyException | InvalidAlgorithmParameterException e)
  102. {
  103. e.printStackTrace();
  104. }
  105. byte[] decryptedBytes = null;
  106. try
  107. {
  108. decryptedBytes = cipher.doFinal(enMex);
  109. } catch (IllegalBlockSizeException | BadPaddingException e)
  110. {
  111. e.printStackTrace();
  112. }
  113. return decryptedBytes;
  114. }
  115.  
  116. public static void main(String args[]) throws Throwable
  117. {
  118. InputStreamReader input = new InputStreamReader(System.in);
  119. BufferedReader tastiera = new BufferedReader(input);
  120. while(true)
  121. {
  122. String userPassword = ""; char[] userPassArray;
  123. String message = "";
  124. System.out.println("Insert Your Password");
  125. userPassword = tastiera.readLine();
  126. System.out.println("Insert The Message to Encrypt");
  127. message = tastiera.readLine();
  128. byte[] encryptedText = SPCryptography.encryptTextWithPassword(userPassword.toCharArray(),message);
  129. System.out.println("Original: "+message);
  130. System.out.println("Encrypted "+new String(encryptedText));
  131. System.out.println("Retype Password");
  132. userPassword = tastiera.readLine();
  133. byte[] decrypted = SPCryptography.decryptTextWithPassword(userPassword.toCharArray(),encryptedText);
  134. System.out.println("Decrypted "+new String(decrypted));
  135. }
  136. }
  137.  
  138. }
  139.  
  140. PwdTest!"£^?==)
  141. !"£=)(&PWd
  142. MYCP"++£=)(&PWd
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement