Class RSAAESHybrid

java.lang.Object
org.kingdoms.utils.security.RSAAESHybrid

public final class RSAAESHybrid extends Object
Data -> AES -> RSA -> GZIP -> Base64 => Base64 -> GZIP -> RSA -> AES -> Data

Encrypts the main message using an AES symmetric key (unlimited length content encryption) then encrypts the AES key itself with an asymmetric RSA-2048 encryption. This causes to have two separate data, in order to send it, the two are merged together with the fixed encrypted AES key (256 bytes) at the beginning and the rest of the encrypted content. Then the resultant byte array is compressed with GZIP and the resultant compressed byte array is encoded in Base64 for easier ASCII data exchange. The same processed is reversed for decryption.

RSA is a fixed-length encryption. RSA-2048 can only encrypt 256 bytes at a time. Using multiple fixed RSA encryption blocks will decrease the security. So instead AES is used to encrypt variable-length data and the AES private fixed-length key itself is encrypted with RSA.

The private AES key for each server is generated per session (server start) and the public RSA key is provided below as a byte array.

  • Field Details

  • Constructor Details

    • RSAAESHybrid

      public RSAAESHybrid()
  • Method Details

    • getMainMethod

      public static String getMainMethod(String cipherAlgorithm)
    • encrypt

      public static org.kingdoms.data.Pair<String,org.kingdoms.data.Pair<byte[],byte[]>> encrypt(byte[] inputData)
      The key of the pair represents the compressed and Base64 encoded final value.
    • merge

      public static byte[] merge(byte[] aes, byte[] content)
    • splitString

      public static org.kingdoms.data.Pair<byte[],byte[]> splitString(String whole)
    • encrypt0

      public static org.kingdoms.data.Pair<byte[],byte[]> encrypt0(byte[] content) throws Exception
      Throws:
      Exception
    • decrypt0

      public static byte[] decrypt0(PrivateKey privateKey, byte[] encryptedAESKey, byte[] encryptedData) throws Exception
      Throws:
      Exception
    • generateKeyPair

      public static KeyPair generateKeyPair() throws NoSuchAlgorithmException, NoSuchProviderException
      Throws:
      NoSuchAlgorithmException
      NoSuchProviderException
    • generateAndPrintKeyPair

      public static void generateAndPrintKeyPair()