· 7 years ago · Oct 03, 2018, 03:54 AM
1public ByteString encrypt(ByteString input){
2 System.out.println("Encrypting...");
3 try {
4 Object lunaSlotManager = Class.forName("com.safenetinc.luna.LunaSlotManager").getDeclaredMethod("getInstance").invoke(null);
5 lunaSlotManager.getClass().getMethod("login", String.class, String.class).invoke(lunaSlotManager, PARITION_NAME, PARITION_PASSWORD);
6
7 Provider provider = (Provider) Class.forName("com.safenetinc.luna.provider.LunaProvider").newInstance();
8 Security.addProvider(provider);
9 Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding", provider);
10
11 KeyStore keyStore = KeyStore.getInstance("Luna", provider);
12 keyStore.load(null, null);
13
14 KeyGenerator aesKeyGenerator = KeyGenerator.getInstance("AES", provider);
15 aesKeyGenerator.init(128);
16 SecretKey aesKey = aesKeyGenerator.generateKey();
17 keyStore.setKeyEntry(KEY_NAME, aesKey, null, null);
18 Key key = keyStore.getKey(KEY_NAME, null);
19
20 byte[] nonce = new byte[NONCE_SIZE];
21 SecureRandom.getInstance("SHA1PRNG").nextBytes(nonce);
22
23 AlgorithmParameterSpec parameterSpec = new IvParameterSpec(nonce);
24
25 cipher.init(Cipher.ENCRYPT_MODE, key, parameterSpec);
26
27 byte[] output = cipher.doFinal(input.toByteArray());
28 return ByteString.copyFrom(output);
29 } catch (Exception e) {
30 e.printStackTrace();
31 }
32 return input;
33}