SimpleAES.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package crypto;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.ObjectInputStream;
  6. import java.io.ObjectOutputStream;
  7. import java.math.BigInteger;
  8. import java.security.InvalidKeyException;
  9. import java.security.NoSuchAlgorithmException;
  10. import java.util.Random;
  11. import javax.crypto.BadPaddingException;
  12. import javax.crypto.Cipher;
  13. import javax.crypto.IllegalBlockSizeException;
  14. import javax.crypto.NoSuchPaddingException;
  15. import javax.crypto.spec.SecretKeySpec;
  16. public class SimpleAES {
  17. private Cipher cipherEnc;
  18. private Cipher cipherDec;
  19. public SimpleAES() {
  20. SecretKeySpec skey = readKey();
  21. try {
  22. cipherEnc = Cipher.getInstance("AES/ECB/PKCS5Padding");
  23. cipherDec = Cipher.getInstance("AES/ECB/PKCS5Padding");
  24. cipherEnc.init(Cipher.ENCRYPT_MODE, skey);
  25. cipherDec.init(Cipher.DECRYPT_MODE, skey);
  26. } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException e) {
  27. e.printStackTrace();
  28. }
  29. }
  30. public synchronized byte[] encrypt(byte[] in) {
  31. byte[] out = null;
  32. try {
  33. out = cipherEnc.doFinal(in);
  34. } catch (IllegalBlockSizeException | BadPaddingException e) {
  35. e.printStackTrace();
  36. }
  37. return out;
  38. }
  39. public synchronized byte[] decrypt(byte[] in) {
  40. byte[] out = null;
  41. try {
  42. out = cipherDec.doFinal(in);
  43. } catch (IllegalBlockSizeException | BadPaddingException e) {
  44. e.printStackTrace();
  45. }
  46. return out;
  47. }
  48. public static void generateKey(Random rand) {
  49. byte[] key = new byte[16];
  50. rand.nextBytes(key);
  51. SecretKeySpec skey = new SecretKeySpec(key, "AES");
  52. FileOutputStream fos = null;
  53. ObjectOutputStream oos = null;
  54. try {
  55. fos = new FileOutputStream("key/aes.key");
  56. oos = new ObjectOutputStream(fos);
  57. oos.writeObject(skey);
  58. } catch (IOException e) {
  59. e.printStackTrace();
  60. } finally {
  61. if (oos != null) {
  62. try {
  63. oos.close();
  64. } catch (IOException e) {
  65. e.printStackTrace();
  66. }
  67. }
  68. }
  69. }
  70. public SecretKeySpec readKey() {
  71. FileInputStream fis = null;
  72. ObjectInputStream ois = null;
  73. SecretKeySpec skey = null;
  74. try {
  75. fis = new FileInputStream("key/aes.key");
  76. ois = new ObjectInputStream(fis);
  77. skey = (SecretKeySpec) ois.readObject();
  78. } catch (IOException | ClassNotFoundException e) {
  79. e.printStackTrace();
  80. } finally {
  81. if (ois != null) {
  82. try {
  83. ois.close();
  84. } catch (IOException e) {
  85. e.printStackTrace();
  86. }
  87. }
  88. }
  89. return skey;
  90. }
  91. // test
  92. public static void main(String[] args) {
  93. SimpleAES aes = new SimpleAES();
  94. byte[] plain = new byte[10240 * 8];
  95. Crypto.sr.nextBytes(plain);
  96. byte[] enc = aes.encrypt(plain);
  97. byte[] tmp = new byte[plain.length];
  98. Crypto.sr.nextBytes(tmp);
  99. aes.decrypt(aes.encrypt(tmp));
  100. byte[] dec = aes.decrypt(enc);
  101. long in = new BigInteger(plain).longValue();
  102. long cipher = new BigInteger(enc).longValue();
  103. long out = new BigInteger(dec).longValue();
  104. System.out.println(in != cipher);
  105. System.out.println(in == out);
  106. System.out.println(plain.length == dec.length);
  107. }
  108. }