Class RSAEncrypter

java.lang.Object
dev.netcode.security.encryption.RSAEncrypter

public class RSAEncrypter extends Object
This class simplifies the process of RSA Encrypting data. Using it you can generate RSA Keys and encrypt/decrypt data.
  • Constructor Details

    • RSAEncrypter

      public RSAEncrypter()
  • Method Details

    • generateKeyPair

      public static KeyPair generateKeyPair(int size)
      Generates a KeyPair with 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 String decrypt(byte[] cipher, PrivateKey privateKey)
      Decrypts RSA encrypted data
      Parameters:
      cipher - data to be decrypted
      privateKey - to be used to decrypt the data
      Returns:
      decrypted data as UTF-8 encoded String
    • encrypt

      public static byte[] encrypt(String message, PublicKey publicKey)
      Encrypts a message using a RSA PublicKey
      Parameters:
      message - to be encrypted
      publicKey - ised to encrypt the data
      Returns:
      byte array of encrypted data
    • sign

      public static String sign(PrivateKey privateKey, 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 data
      input - data to be signed
      Returns:
      Signature as String
    • verifySignature

      public static boolean verifySignature(PublicKey publicKey, String data, 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 sender
      data - original data
      signature - to be tested
      Returns:
      true if the signature matches the data and public key
    • generatePublicKeyFromString

      public static PublicKey generatePublicKeyFromString(byte[] key)
      Generates a public key from byte array
      Parameters:
      key - byte array
      Returns:
      public key
    • generatePrivateKeyFromString

      public static PrivateKey generatePrivateKeyFromString(byte[] key)
      Generates a private key from byte array
      Parameters:
      key - byte array
      Returns:
      private key
    • keyToString

      public static String keyToString(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 String getFingerprint(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