Class RSAEncrypter


  • public class RSAEncrypter
    extends java.lang.Object
    This 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.String decrypt​(byte[] cipher, java.security.PrivateKey privateKey)
      Decrypts RSA encrypted data
      static byte[] encrypt​(java.lang.String message, java.security.PublicKey publicKey)
      Encrypts a message using a RSA PublicKey
      static java.security.KeyPair generateKeyPair​(int size)
      Generates a KeyPair with given size.
      static java.security.PrivateKey generatePrivateKeyFromString​(byte[] key)
      Generates a private key from byte array
      static java.security.PublicKey generatePublicKeyFromString​(byte[] key)
      Generates a public key from byte array
      static java.lang.String getFingerprint​(java.security.PublicKey key)
      Generates a fingerprint string of a public key.
      static java.lang.String keyToString​(java.security.Key key)
      Encodes a Key to a String representation.
      static java.lang.String sign​(java.security.PrivateKey privateKey, java.lang.String input)
      Generates a RSA signature for a given input.
      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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • RSAEncrypter

        public RSAEncrypter()
    • Method Detail

      • generateKeyPair

        public static java.security.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 java.lang.String decrypt​(byte[] cipher,
                                               java.security.PrivateKey privateKey)
                                        throws java.security.InvalidKeyException,
                                               javax.crypto.IllegalBlockSizeException,
                                               javax.crypto.BadPaddingException
        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
        Throws:
        java.security.InvalidKeyException - in case the key format is not supported
        javax.crypto.BadPaddingException - in case something failed while padding
        javax.crypto.IllegalBlockSizeException - in case the blocksize is invalid
      • encrypt

        public static byte[] encrypt​(java.lang.String message,
                                     java.security.PublicKey publicKey)
                              throws java.security.InvalidKeyException,
                                     javax.crypto.IllegalBlockSizeException,
                                     javax.crypto.BadPaddingException
        Encrypts a message using a RSA PublicKey
        Parameters:
        message - to be encrypted
        publicKey - ised to encrypt the data
        Returns:
        byte array of encrypted data
        Throws:
        java.security.InvalidKeyException - in case the key format is not supported
        javax.crypto.BadPaddingException - in case something failed while padding
        javax.crypto.IllegalBlockSizeException - in case the blocksize is invalid
      • sign

        public static java.lang.String sign​(java.security.PrivateKey privateKey,
                                            java.lang.String input)
                                     throws java.security.InvalidKeyException,
                                            java.security.SignatureException
        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
        Throws:
        java.security.InvalidKeyException - in case the key format is not supported
        java.security.SignatureException - in case something went wrong while creating the signature
      • 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 sender
        data - original data
        signature - to be tested
        Returns:
        true if the signature matches the data and public key
      • generatePublicKeyFromString

        public static java.security.PublicKey generatePublicKeyFromString​(byte[] key)
                                                                   throws java.security.spec.InvalidKeySpecException
        Generates a public key from byte array
        Parameters:
        key - byte array
        Returns:
        public key
        Throws:
        java.security.spec.InvalidKeySpecException - in case the key format is not supported
      • generatePrivateKeyFromString

        public static java.security.PrivateKey generatePrivateKeyFromString​(byte[] key)
                                                                     throws java.security.spec.InvalidKeySpecException
        Generates a private key from byte array
        Parameters:
        key - byte array
        Returns:
        private key
        Throws:
        java.security.spec.InvalidKeySpecException - in case the key format is not supported
      • 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