Class RSACipher


  • public class RSACipher
    extends Object
    Utility class intended to simplify RSA encryption / decryption.
    • Field Detail

      • MAX_ENCRYPTED_DATA_SIZE

        public static final int MAX_ENCRYPTED_DATA_SIZE
        See Also:
        Constant Field Values
      • MAX_NON_ASCII_ENCRYPTED_DATA_SIZE

        public static final int MAX_NON_ASCII_ENCRYPTED_DATA_SIZE
        See Also:
        Constant Field Values
    • Method Detail

      • generateKeys

        public static void generateKeys()
        Generates new public and private keys and stores them in static fields.
      • decryptWebRequestData

        @Nullable
        public static String decryptWebRequestData​(String hexEncoded)
                                            throws DecryptionFailedException
        Decrypts hex encoded JavaScript encrypted data.
        Parameters:
        hexEncoded - hex encoded encrypted byte array
        Returns:
        decrypted string
        Throws:
        DecryptionFailedException - if decryption failed
      • encryptData

        @Nullable
        public static String encryptData​(String plain)
                                  throws EncryptionFailedException
        Encrypts specified string, and returns hex representation of the encrypted data
        Parameters:
        plain - text to encrypt
        Returns:
        hex representation of the encrypted data
        Throws:
        EncryptionFailedException - if encryption failed
      • encryptDataForWeb

        public static String encryptDataForWeb​(String plain)
                                        throws EncryptionFailedException
        Encrypts specified string, and returns hex representation of the encrypted data. It differs from encryptData(String) in that it stores length of the encrypted string as a last byte into byte array before encryption
        Parameters:
        plain - text to encrypt
        Returns:
        hex representation of the encrypted data
        Throws:
        EncryptionFailedException - if encryption failed
      • encryptData

        @Nullable
        public static String encryptData​(String plain,
                                         PublicKey pubKey)
                                  throws EncryptionFailedException
        Encrypts data using the specified public key using default system charset
        Parameters:
        plain - text to encrypt
        pubKey - public key
        Returns:
        data encrypted with specified public key in hex form
        Throws:
        EncryptionFailedException - if encryption failed
      • getHexEncodedPublicKey

        public static String getHexEncodedPublicKey()
        Returns hex representation of the public key
        Returns:
        hex representation of the public key
      • getHexEncodedPublicKeyExponent

        public static String getHexEncodedPublicKeyExponent()
        Returns hex representation of the public key exponent
        Returns:
        hex representation of the public key exponent
      • recreateKey

        public static PublicKey recreateKey​(String hexModulus,
                                            String hexExponent)
                                     throws InvalidKeyException
        Recreates public key from hex encoded modulus & exponent.
        Parameters:
        hexModulus - hex encoded modulus
        hexExponent - hex encoded exponent
        Returns:
        recreated public key
        Throws:
        InvalidKeyException - if key recreation failed