Encrypting sensitive application data in Java
I'm currently assessing a project where highly sensitive personal information is handled thus it needs to be encrypted. We are talking like several 100 megabytes of multimedia files, like MP3 or something else. The application will most certainly be im开发者_高级运维plemented in Java with JavaFX as GUI/Frontend. Now I am searching for a feasible solution to protect that data from unintentional/intentional misuse. The data needs to be encrypted somehow. The user needs to provide login credentials before using the software, so using the password to unlock a key used for symmetric crypto would be possible. The users of the application will be non professional thus things like TrueCrypt or similar solutions won't do the trick. Although some kind of transparent solution would be best. So is there any (semi-) standard solution for this problem?
Thanks for the help
Greetings,
Andreas
The following method encrypts a given byte array, where keyC is the encryption key. initalVector is the initial vector used for the encryption. This vector is typically used for AES encryption in counter (CTR) mode, but is not necessary for other modes. It's an array of a certain 16 bytes, used for encrypting and decrypting.
private byte[] encryptAES128(byte[] input, byte[] initialVector) {
SecretKey aeskey = new SecretKeySpec(keyC, 0, 16, "AES");
AlgorithmParameterSpec paramSpec = new IvParameterSpec(initialVector);
cipher = Cipher.getInstance("AES/CTR/NOPADDING");
getAesCTRCipher().init(Cipher.ENCRYPT_MODE, aeskey, paramSpec);
return getAesCTRCipher().doFinal(input);
}
This method returns a new byte array, being the encrypted input array. It always works in blocks of 16 bytes. For larger files, you need run a for loop over the bytes and concatenate the result :)
Good luck!
Edit: After encrypting a block of 16 bytes, you need to increment the initial vector, that is if the encryption runs in Counter mode :)
Use a public-key encryption algorithm, such as RSA. strong, almost uncrackable and easy to use and understand. there's probably even a method or even a class for RSA or similar encryptions in Java.
精彩评论