java.lang.Object
dev.netcode.security.encryption.RSAEncrypter
This class simplifies the process of RSA Encrypting data.
Using it you can generate RSA Keys and encrypt/decrypt data.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Stringdecrypt(byte[] cipher, PrivateKey privateKey)Decrypts RSA encrypted datastatic byte[]Encrypts a message using a RSAPublicKeystatic KeyPairgenerateKeyPair(int size)Generates aKeyPairwith given size.static PrivateKeygeneratePrivateKeyFromString(byte[] key)Generates a private key from byte arraystatic PublicKeygeneratePublicKeyFromString(byte[] key)Generates a public key from byte arraystatic StringgetFingerprint(PublicKey key)Generates a fingerprint string of a public key.static StringkeyToString(Key key)Encodes a Key to a String representation.static Stringsign(PrivateKey privateKey, String input)Generates a RSA signature for a given input.static booleanverifySignature(PublicKey publicKey, String data, String signature)Verifies that a given signature matches given data and a given public key.
-
Constructor Details
-
RSAEncrypter
public RSAEncrypter()
-
-
Method Details
-
generateKeyPair
Generates aKeyPairwith given size. The higher the size, the more secure the key can be considered. Size must be divisible by 2.- Parameters:
size- of the keys to be generated- Returns:
- the generated
KeyPair
-
decrypt
Decrypts RSA encrypted data- Parameters:
cipher- data to be decryptedprivateKey- to be used to decrypt the data- Returns:
- decrypted data as UTF-8 encoded String
-
encrypt
Encrypts a message using a RSAPublicKey- Parameters:
message- to be encryptedpublicKey- ised to encrypt the data- Returns:
- byte array of encrypted data
-
sign
Generates a RSA signature for a given input. The signature can be used to make sure a message comes from a specific sender- Parameters:
privateKey- used to sign the datainput- data to be signed- Returns:
- Signature as String
-
verifySignature
Verifies that a given signature matches given data and a given public key. This process ensures that the data really comes from the sender- Parameters:
publicKey- of the senderdata- original datasignature- to be tested- Returns:
- true if the signature matches the data and public key
-
generatePublicKeyFromString
Generates a public key from byte array- Parameters:
key- byte array- Returns:
- public key
-
generatePrivateKeyFromString
Generates a private key from byte array- Parameters:
key- byte array- Returns:
- private key
-
keyToString
Encodes a Key to a String representation. The key will be encoded to Base64- Parameters:
key- to transform- Returns:
- String representation
-
getFingerprint
Generates a fingerprint string of a public key. The key has to be X509 compatible. The fingerprint will be SHA-1 representation of the key- Parameters:
key- to get the fingerprint of- Returns:
- the fingerprint
-