Last active
October 2, 2018 21:49
-
-
Save kardolus/59626c039408d3e2c6977a7eb0736d11 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public ByteString encrypt(ByteString input){ | |
System.out.println("Encrypting..."); | |
try { | |
Object lunaSlotManager = Class.forName("com.safenetinc.luna.LunaSlotManager").getDeclaredMethod("getInstance").invoke(null); | |
lunaSlotManager.getClass().getMethod("login", String.class, String.class).invoke(lunaSlotManager, PARITION_NAME, PARITION_PASSWORD); | |
Provider provider = (Provider) Class.forName("com.safenetinc.luna.provider.LunaProvider").newInstance(); | |
Security.addProvider(provider); | |
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding", provider); | |
KeyStore keyStore = KeyStore.getInstance("Luna", provider); | |
keyStore.load(null, null); | |
KeyGenerator aesKeyGenerator = KeyGenerator.getInstance("AES", provider); | |
aesKeyGenerator.init(128); | |
SecretKey aesKey = aesKeyGenerator.generateKey(); | |
keyStore.setKeyEntry(KEY_NAME, aesKey, null, null); | |
Key key = keyStore.getKey(KEY_NAME, null); | |
byte[] nonce = new byte[NONCE_SIZE]; | |
SecureRandom.getInstance("SHA1PRNG").nextBytes(nonce); | |
AlgorithmParameterSpec parameterSpec = new IvParameterSpec(nonce); | |
cipher.init(Cipher.ENCRYPT_MODE, key, parameterSpec); | |
byte[] output = cipher.doFinal(input.toByteArray()); | |
return ByteString.copyFrom(output); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return input; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment