public class Crypto
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
private javax.crypto.SecretKey |
aesKey |
private java.security.KeyPair |
keyPair |
private KeyStorer |
keyStorer |
Constructor and Description |
---|
Crypto(java.security.KeyPair keyPair)
Initializes cryptography with the given RSA keypair and generates a new AES key.
|
Crypto(User user,
DatabaseConnection databaseConnection)
Initializes cryptography for the given user, fetching their keys from the database and the keystore file.
|
Modifier and Type | Method and Description |
---|---|
byte[] |
decrypt(byte[] text)
Decrypt text using AES.
|
byte[] |
encrypt(double value)
Encrypt double value using AES.
|
byte[] |
encrypt(int value)
Encrypt int value using AES.
|
byte[] |
encrypt(java.lang.String text)
Encrypt the plain text using AES.
|
javax.crypto.SecretKey |
getAesKey() |
private javax.crypto.SecretKey |
getAesKey(User user)
Returns the (unencrypted) AES key
|
private byte[] |
getEncryptedAESKey()
Return the RSA-encrypted AES key
|
private byte[] |
getEncryptedAESKey(byte[] aesKey,
java.security.PublicKey publicKey)
Return AES key after RSA encryption with the given RSA public key
|
java.security.KeyPair |
getKeyPair() |
void |
regenerateKeys(User oldUser,
User updatedUser)
Regenerate new RSA keys, re-encrypt the AES key using the new RSA Public Key then store all the keys.
|
private final KeyStorer keyStorer
private java.security.KeyPair keyPair
private javax.crypto.SecretKey aesKey
public Crypto(User user, DatabaseConnection databaseConnection) throws DatabaseException
user
- databaseConnection
- DatabaseException
public Crypto(java.security.KeyPair keyPair)
keyPair
- private javax.crypto.SecretKey getAesKey(User user) throws DatabaseException, NoSuchKeysFoundForUserException
user
- DatabaseException
NoSuchKeysFoundForUserException
public byte[] decrypt(byte[] text)
text
- encrypted textpublic byte[] encrypt(java.lang.String text)
text
- original plain textpublic byte[] encrypt(int value)
value
- public byte[] encrypt(double value)
value
- public void regenerateKeys(User oldUser, User updatedUser)
oldUser
- updatedUser
- private byte[] getEncryptedAESKey()
private byte[] getEncryptedAESKey(byte[] aesKey, java.security.PublicKey publicKey)
aesKey
- publicKey
- public java.security.KeyPair getKeyPair()
public javax.crypto.SecretKey getAesKey()