PRG.java 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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.security.InvalidAlgorithmParameterException;
  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.IvParameterSpec;
  16. import javax.crypto.spec.SecretKeySpec;
  17. import exceptions.IllegalInputException;
  18. public class PRG {
  19. private Cipher cipher;
  20. private SecretKeySpec skey;
  21. private int l; // output bit length
  22. public PRG(int l) {
  23. try {
  24. cipher = Cipher.getInstance("AES/CTR/NoPadding");
  25. } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
  26. e.printStackTrace();
  27. }
  28. readKey();
  29. this.l = l;
  30. }
  31. public byte[] compute(byte[] seed) {
  32. byte[] input;
  33. if (seed.length > 16) {
  34. throw new IllegalInputException(seed.length + " > 16");
  35. } else if (seed.length == 16) {
  36. input = seed;
  37. } else {
  38. input = new byte[16];
  39. System.arraycopy(seed, 0, input, input.length - seed.length, seed.length);
  40. }
  41. IvParameterSpec IV = new IvParameterSpec(input);
  42. byte[] msg = new byte[(l + 7) / 8];
  43. byte[] output = null;
  44. try {
  45. cipher.init(Cipher.ENCRYPT_MODE, skey, IV);
  46. output = cipher.doFinal(msg);
  47. } catch (InvalidKeyException | InvalidAlgorithmParameterException | IllegalBlockSizeException
  48. | BadPaddingException e) {
  49. e.printStackTrace();
  50. }
  51. return output;
  52. }
  53. public static void generateKey(Random rand) {
  54. byte[] key = new byte[16];
  55. rand.nextBytes(key);
  56. SecretKeySpec skey = new SecretKeySpec(key, "AES");
  57. FileOutputStream fos = null;
  58. ObjectOutputStream oos = null;
  59. try {
  60. fos = new FileOutputStream("key/prg.key");
  61. oos = new ObjectOutputStream(fos);
  62. oos.writeObject(skey);
  63. } catch (IOException e) {
  64. e.printStackTrace();
  65. } finally {
  66. if (oos != null) {
  67. try {
  68. oos.close();
  69. } catch (IOException e) {
  70. e.printStackTrace();
  71. }
  72. }
  73. }
  74. }
  75. public void readKey() {
  76. FileInputStream fis = null;
  77. ObjectInputStream ois = null;
  78. try {
  79. fis = new FileInputStream("key/prg.key");
  80. ois = new ObjectInputStream(fis);
  81. skey = (SecretKeySpec) ois.readObject();
  82. } catch (IOException | ClassNotFoundException e) {
  83. e.printStackTrace();
  84. } finally {
  85. if (ois != null) {
  86. try {
  87. ois.close();
  88. } catch (IOException e) {
  89. e.printStackTrace();
  90. }
  91. }
  92. }
  93. }
  94. }