- java.lang.Object
-
- dev.netcode.security.encryption.RSAEncrypter
-
public class RSAEncrypter extends java.lang.ObjectThis class simplifies the process of RSA Encrypting data. Using it you can generate RSA Keys and encrypt/decrypt data.
-
-
Constructor Summary
Constructors Constructor Description RSAEncrypter()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.Stringdecrypt(byte[] cipher, java.security.PrivateKey privateKey)Decrypts RSA encrypted datastatic byte[]encrypt(java.lang.String message, java.security.PublicKey publicKey)Encrypts a message using a RSAPublicKeystatic java.security.KeyPairgenerateKeyPair(int size)Generates aKeyPairwith given size.static java.security.PrivateKeygeneratePrivateKeyFromString(byte[] key)Generates a private key from byte arraystatic java.security.PublicKeygeneratePublicKeyFromString(byte[] key)Generates a public key from byte arraystatic java.lang.StringgetFingerprint(java.security.PublicKey key)Generates a fingerprint string of a public key.static java.lang.StringkeyToString(java.security.Key key)Encodes a Key to a String representation.static java.lang.Stringsign(java.security.PrivateKey privateKey, java.lang.String input)Generates a RSA signature for a given input.static booleanverifySignature(java.security.PublicKey publicKey, java.lang.String data, java.lang.String signature)Verifies that a given signature matches given data and a given public key.
-
-
-
Method Detail
-
generateKeyPair
public static java.security.KeyPair generateKeyPair(int size)
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
public static java.lang.String decrypt(byte[] cipher, java.security.PrivateKey privateKey)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
public static byte[] encrypt(java.lang.String message, java.security.PublicKey publicKey)Encrypts a message using a RSAPublicKey- Parameters:
message- to be encryptedpublicKey- ised to encrypt the data- Returns:
- byte array of encrypted data
-
sign
public static java.lang.String sign(java.security.PrivateKey privateKey, java.lang.String input)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
public static boolean verifySignature(java.security.PublicKey publicKey, java.lang.String data, java.lang.String signature)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
public static java.security.PublicKey generatePublicKeyFromString(byte[] key)
Generates a public key from byte array- Parameters:
key- byte array- Returns:
- public key
-
generatePrivateKeyFromString
public static java.security.PrivateKey generatePrivateKeyFromString(byte[] key)
Generates a private key from byte array- Parameters:
key- byte array- Returns:
- private key
-
keyToString
public static java.lang.String keyToString(java.security.Key key)
Encodes a Key to a String representation. The key will be encoded to Base64- Parameters:
key- to transform- Returns:
- String representation
-
getFingerprint
public static java.lang.String getFingerprint(java.security.PublicKey key)
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
-
-