Advertisement
Guest User

Untitled

a guest
May 22nd, 2015
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package encryptiontest;
  7.  
  8. import java.io.UnsupportedEncodingException;
  9. import java.math.BigInteger;
  10. import java.nio.charset.Charset;
  11. import java.security.GeneralSecurityException;
  12. import java.security.MessageDigest;
  13. import java.security.NoSuchAlgorithmException;
  14. import java.security.SecureRandom;
  15. import java.util.Arrays;
  16. import java.util.Base64;
  17. import javax.crypto.Cipher;
  18. import javax.crypto.spec.SecretKeySpec;
  19.  
  20. public class EncryptionTest {
  21.  
  22. private static final SecureRandom random = new SecureRandom();
  23.  
  24. public static String nextSessionId() {
  25. return new BigInteger(130, random).toString(63);
  26. }
  27.  
  28. public static void main(String[] args) throws UnsupportedEncodingException, GeneralSecurityException, Exception {
  29. while(true) {
  30. String key = nextSessionId();
  31. String source = nextSessionId();
  32. String result = encrypt(getKey(key), source);
  33.  
  34. System.out.println("source = " + source);
  35. System.out.println("key = " + key);
  36. System.out.println("result = " + result);
  37.  
  38. result = decrypt(getKey(key), result);
  39.  
  40. if (result.equals(source)) {
  41. } else {
  42. throw new Exception("Fail");
  43. }
  44. }
  45. }
  46.  
  47. private static String encrypt(byte[] key, String value) throws GeneralSecurityException {
  48. // Argument validation.
  49. if (key.length != 16) {
  50. throw new IllegalArgumentException("Invalid key size.");
  51. }
  52.  
  53. // Setup AES tool.
  54. SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
  55. Cipher cipher = Cipher.getInstance("AES");
  56. cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
  57.  
  58. // Do the job with AES tool.
  59. byte[] original = value.getBytes(Charset.forName("UTF-8"));
  60. byte[] binary = cipher.doFinal(original);
  61. return Base64.getEncoder().encodeToString(binary);
  62. }
  63.  
  64. private static String decrypt(byte[] key, String encrypted) throws GeneralSecurityException {
  65. // Argument validation.
  66. if (key.length != 16) {
  67. throw new IllegalArgumentException("Invalid key size.");
  68. }
  69.  
  70. // Setup AES tool.
  71. SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
  72. Cipher cipher = Cipher.getInstance("AES");
  73. cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
  74.  
  75. // Do the job with AES tool.
  76. byte[] binary = Base64.getDecoder().decode(encrypted);
  77. byte[] original = cipher.doFinal(binary);
  78. return new String(original, Charset.forName("UTF-8"));
  79. }
  80.  
  81. private static byte[] getKey(String string) throws UnsupportedEncodingException, NoSuchAlgorithmException {
  82. MessageDigest sha = MessageDigest.getInstance("SHA-1");
  83. byte[] key = sha.digest(string.getBytes("UTF-8"));
  84. key = Arrays.copyOf(key, 16);
  85. return key;
  86. }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement