Class MBouncy


  • public class MBouncy
    extends Object
    This utility uses explicit bouncy castle methods for cryptography. It depends on BC but not JCE. For some compatibility reasons it makes sense to use BC instead of JCE (e.g. open jdk on not common environments / hardware JCE is not available).
    Author:
    mikehummel
    • Constructor Detail

      • MBouncy

        public MBouncy()
    • Method Detail

      • init

        public static void init()
      • getPublicKey

        public static PublicKey getPublicKey​(String key)
        Transform a string encoded public key to a public key object.
        Parameters:
        key - Public Key as string
        Returns:
        Public Key as object
      • getPrivateKey

        public static PrivateKey getPrivateKey​(String key)
        Transforms a string encoded private key into a private key object
        Parameters:
        key - Private Key as string
        Returns:
        Private Key as object
      • getPublicKey

        public static String getPublicKey​(KeyPair key)
        Returns the public key of a key pair as string
        Parameters:
        key -
        Returns:
        Public Key as string
      • getPrivateKey

        public static String getPrivateKey​(KeyPair key)
        Returns the private key of a key pair as string
        Parameters:
        key -
        Returns:
        Private Key as string
      • encryptRsa117

        public static byte[] encryptRsa117​(byte[] text,
                                           PublicKey key)
                                    throws Exception
        Encrypt one block with maximal 117 bytes (max block size). Optimized for one block step.
        Parameters:
        text -
        key -
        Returns:
        The encrypted block (128 bytes)
        Throws:
        Exception
      • encryptRsa

        public static byte[] encryptRsa​(byte[] text,
                                        PublicKey key)
                                 throws Exception
        Encrypt a unlimited amount of bytes with rsa.
        Parameters:
        text -
        key -
        Returns:
        encrypted bytes
        Throws:
        Exception
      • decryptRsa

        public static byte[] decryptRsa​(byte[] text,
                                        PrivateKey key)
                                 throws Exception
        Decrypt an encrypted block.
        Parameters:
        text -
        key -
        Returns:
        decrypted data
        Throws:
        Exception
      • encryptRsa117

        public static String encryptRsa117​(String text,
                                           PublicKey key)
                                    throws Exception
        Encrypt a single block with max 117 bytes as base64 string. Optimized for one block encryption. The text will be encoded with UTF8.
        Parameters:
        text - UTF8 Text
        key -
        Returns:
        encrypted string
        Throws:
        Exception
      • decryptRsa117

        public static byte[] decryptRsa117​(byte[] text,
                                           PrivateKey key)
                                    throws Exception
        Decrypt a single rsa block (128 bytes) with a result of maximal 117 bytes. Optimized for a single block step.
        Parameters:
        text -
        key -
        Returns:
        decrypted string
        Throws:
        Exception
      • decryptRsa117

        public static String decryptRsa117​(String text,
                                           PrivateKey key)
                                    throws Exception
        Decrypt a single base64 encrypted byte block. The text will decoded with UTF8.
        Parameters:
        text -
        key -
        Returns:
        decrypted string
        Throws:
        Exception
      • encodeBase64

        public static String encodeBase64​(byte[] bytes)
        encode bytes with base64 algorithm.
        Parameters:
        bytes -
        Returns:
        Base64 encoded string
      • decodeBase64

        public static byte[] decodeBase64​(String text)
        Decode Base64 encoded string to bytes
        Parameters:
        text -
        Returns:
        original bytes
      • createRandom

        public static byte[] createRandom​(int size)
        Create a array of 'size' with random content.
        Parameters:
        size -
        Returns:
        random content
      • encryptAes

        public static byte[] encryptAes​(byte[] key,
                                        byte[] data)
        Encrypt the data using symmetric AES.
        Parameters:
        key - The key with 16, 24 or 32 bytes.
        data -
        Returns:
        Encoded data
      • encryptAes

        public static String encryptAes​(byte[] key,
                                        String data)
        Encrypt the String using UTF8 encoding and return a base64 encoded string.
        Parameters:
        key -
        data -
        Returns:
        Base64 encoded encrypted string
      • generateAesKeySpec

        public static Key generateAesKeySpec​(byte[] key)
        Creates a key object from bytes.
        Parameters:
        key -
        Returns:
        The bytes key as object
      • decryptAes

        public static byte[] decryptAes​(byte[] key,
                                        byte[] encrypted)
        Decrypt a encrypted byte array.
        Parameters:
        key -
        encrypted -
        Returns:
        original bytes
      • decryptAes

        public static String decryptAes​(byte[] key,
                                        String encrypted)
        Decrypt a base64 encoded string and return the ariginal string. String encoding is UTF8.
        Parameters:
        key -
        encrypted -
        Returns:
        The original string
      • getRsaKeyFromPool

        public static KeyPair getRsaKeyFromPool()
        Generating RSA keys needs a lot of resources (ca 100ms per key). Therefore you can use a keypool. The keypool will regularly renew the keys.
        Returns:
        A key from the pool