Package com.jgcomptech.tools
Class SecurityTools.RSAHashes
- java.lang.Object
-
- com.jgcomptech.tools.SecurityTools.RSAHashes
-
- Enclosing class:
- SecurityTools
public static final class SecurityTools.RSAHashes extends java.lang.ObjectContains methods dealing with RSA encryption and decryption.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static byte[]decrypt(java.security.PrivateKey key, byte[] cipherText)Decrypts specified text with private key.static byte[]decryptFromString(java.security.PrivateKey key, java.lang.String cipherText)Decrypts specified text with private key.static byte[]encrypt(java.security.PublicKey key, java.lang.String plainText)Encrypts specified text with public key.static java.lang.StringencryptToString(java.security.PublicKey key, java.lang.String plainText)Encrypts specified text with public key.static java.security.KeyPairgenerateKeyPair()Generates a key pair.static java.security.KeyPairgenerateKeyPair(boolean saveToFiles, java.lang.String fileName)Generates a key pair and saves them to files matching the specified filename.static java.security.PrivateKeyreadPrivateKeyFromBytes(byte[] bytes)Converts a byte array to a PrivateKey object.static java.security.PublicKeyreadPublicKeyFromBytes(byte[] bytes)Converts a byte array to a PublicKey object.
-
-
-
Method Detail
-
generateKeyPair
public static java.security.KeyPair generateKeyPair() throws java.io.IOException, java.security.GeneralSecurityExceptionGenerates a key pair.- Returns:
- Key pair as a KeyPair object
- Throws:
java.io.FileNotFoundException- if the file is a directory rather than a regular file, or for some other reason cannot be opened for writingjava.io.IOException- if an I/O error occursjava.security.GeneralSecurityException- if error occurs
-
generateKeyPair
public static java.security.KeyPair generateKeyPair(boolean saveToFiles, java.lang.String fileName) throws java.io.IOException, java.security.GeneralSecurityExceptionGenerates a key pair and saves them to files matching the specified filename.- Parameters:
saveToFiles- If true KeyPair will be saved to two separate filesfileName- File name to use to save files- Returns:
- Key pair as a KeyPair object
- Throws:
java.io.FileNotFoundException- if the file is a directory rather than a regular file, or for some other reason cannot be opened for writingjava.io.IOException- if an I/O error occursjava.security.GeneralSecurityException- if error occurs
-
readPublicKeyFromBytes
public static java.security.PublicKey readPublicKeyFromBytes(byte[] bytes) throws java.security.GeneralSecurityExceptionConverts a byte array to a PublicKey object.- Parameters:
bytes- To read from- Returns:
- Converted public key as PublicKey object
- Throws:
java.security.GeneralSecurityException- if error occurs
-
readPrivateKeyFromBytes
public static java.security.PrivateKey readPrivateKeyFromBytes(byte[] bytes) throws java.security.GeneralSecurityExceptionConverts a byte array to a PrivateKey object.- Parameters:
bytes- To read from- Returns:
- Converted public key as PrivateKey object
- Throws:
java.security.GeneralSecurityException- if error occurs
-
encrypt
public static byte[] encrypt(java.security.PublicKey key, java.lang.String plainText) throws java.security.GeneralSecurityExceptionEncrypts specified text with public key.- Parameters:
key- Public key to encrypt withplainText- String to encrypt- Returns:
- Encrypted text as byte array
- Throws:
java.security.GeneralSecurityException- if error occurs
-
encryptToString
public static java.lang.String encryptToString(java.security.PublicKey key, java.lang.String plainText) throws java.security.GeneralSecurityExceptionEncrypts specified text with public key.- Parameters:
key- Public key to encrypt withplainText- String to encrypt- Returns:
- Encrypted text as string
- Throws:
java.security.GeneralSecurityException- if error occurs
-
decrypt
public static byte[] decrypt(java.security.PrivateKey key, byte[] cipherText) throws java.security.GeneralSecurityExceptionDecrypts specified text with private key.- Parameters:
key- Private key to decrypt withcipherText- String to decrypt- Returns:
- Decrypted text as byte array
- Throws:
java.security.GeneralSecurityException- if error occurs
-
decryptFromString
public static byte[] decryptFromString(java.security.PrivateKey key, java.lang.String cipherText) throws java.security.GeneralSecurityExceptionDecrypts specified text with private key.- Parameters:
key- Private key to decrypt withcipherText- String to decrypt- Returns:
- Decrypted text as string
- Throws:
java.security.GeneralSecurityException- if error occurs
-
-