Class MCrypt


  • public class MCrypt
    extends Object
    This tool is implementing functions to work with encryption and obfuscation to protect data. The algorithm is implemented separate to the usual java.security package. If you want to use the save and proven java implementation do not use this tool. With this tool you do not need any high security patch for your java JRE. You need to provide the bouncycastle bcprov-jdk15on library to read keys from ASN1 encoded files.
    Author:
    mikehummel
    • Constructor Detail

      • MCrypt

        public MCrypt()
    • Method Detail

      • loadPrivateRsaKey

        public static AsyncKey loadPrivateRsaKey​(File file)
                                          throws IOException
        Load a private key from file.
        Parameters:
        file -
        Returns:
        the key object
        Throws:
        IOException
      • loadPublicRsaKey

        public static AsyncKey loadPublicRsaKey​(File file)
                                         throws IOException
        Load a public key from file.
        Parameters:
        file -
        Returns:
        the key object
        Throws:
        IOException
      • loadPrivateRsaKey

        public static AsyncKey loadPrivateRsaKey​(String key)
                                          throws IOException
        Load a RSA private key into a AsyncKey object.
        Parameters:
        key - key as ASN1 encoded string containing "-----BEGIN RSA PRIVATE KEY-----"
        Returns:
        Corresponding key object
        Throws:
        IOException - If the key start or stop token was not found
      • loadPublicRsaKey

        public static AsyncKey loadPublicRsaKey​(String key)
                                         throws IOException
        Load a RSA public key into a AsyncKey object.
        Parameters:
        key - key as ASN1 encoded string containing "-----BEGIN RSA PUBLIC KEY-----"
        Returns:
        Corresponding key object
        Throws:
        IOException - If the key start or stop token was not found
      • encode

        public static BigInteger encode​(AsyncKey key,
                                        BigInteger in)
                                 throws IOException
        Encode data using a RSA like algorithm. It's not using the java implementation.
        Parameters:
        key - public key
        in - clear data
        Returns:
        encoded data
        Throws:
        IOException
      • encodeWithSalt

        public static String encodeWithSalt​(AsyncKey key,
                                            String in)
                                     throws IOException
        Encode data using a RSA like algorithm. It's not using the java implementation.
        Parameters:
        key - public key
        in - clear data
        Returns:
        encoded and Base91 encoded string
        Throws:
        IOException
      • encode

        public static String encode​(AsyncKey key,
                                    String in)
                             throws IOException
        Encode data using a RSA like algorithm. It's not using the java implementation.
        Parameters:
        key - public key
        in - clear data
        Returns:
        encoded and Base91 encoded string
        Throws:
        IOException
      • encodeBytes

        public static BigInteger[] encodeBytes​(AsyncKey key,
                                               byte[] in)
                                        throws IOException
        Encode data using a RSA like algorithm. It's not using the java implementation.
        Parameters:
        key - public key
        in - clear data
        Returns:
        encoded data
        Throws:
        IOException
      • encodeBytes

        public static BigInteger[] encodeBytes​(AsyncKey key,
                                               BigInteger[] in)
                                        throws IOException
        Encode data using a RSA like algorithm. It's not using the java implementation.
        Parameters:
        key - public key
        in - clear data
        Returns:
        encoded data
        Throws:
        IOException
      • decodeWithSalt

        public static String decodeWithSalt​(AsyncKey key,
                                            String in)
                                     throws IOException
        Decode the data using Base91 byte encoding and the private key from 'key' using a RSA like algorithm. It's not the java implementation used.
        Parameters:
        key - private key
        in - the encoded data presentation
        Returns:
        the decoded string
        Throws:
        IOException
      • decode

        public static String decode​(AsyncKey key,
                                    String in)
                             throws IOException
        Decode the data using Base91 byte encoding and the private key from 'key' using a RSA like algorithm. It's not the java implementation used.
        Parameters:
        key - private key
        in - the encoded data presentation
        Returns:
        the decoded string
        Throws:
        IOException
      • decode

        public static BigInteger[] decode​(AsyncKey key,
                                          BigInteger[] in)
                                   throws IOException
        Decode the data using the private key from 'key' using a RSA like algorithm. It's not the java implementation used.
        Parameters:
        key - private key
        in - encoded data
        Returns:
        decoded array of data
        Throws:
        IOException
      • decodeBytes

        public static byte[] decodeBytes​(AsyncKey key,
                                         BigInteger[] in)
                                  throws IOException
        Decode data using the private key from 'key' using a RSA like algorithm. It's not the java implementation used.
        Parameters:
        key - private key
        in - encoded data
        Returns:
        decoded array of data.
        Throws:
        IOException
      • createRandomCipherBlockRotate

        public static CipherBlockRotate createRandomCipherBlockRotate​(int size)
        Create a random block using the MRandom service.
        Parameters:
        size -
        Returns:
        CipherBlockRotate
      • createCipherOutputStream

        public static OutputStream createCipherOutputStream​(OutputStream parent,
                                                            String passphrase)
                                                     throws IOException
        Create a output stream automatically encode the stream with the pass phrase.
        Parameters:
        parent -
        passphrase -
        Returns:
        OutputStream
        Throws:
        IOException
      • createCipherInputStream

        public static InputStream createCipherInputStream​(InputStream parent,
                                                          String passphrase)
                                                   throws IOException
        Create a stream to decode a data stream with a simple pass phrase using createCipherOutputStream.
        Parameters:
        parent -
        passphrase -
        Returns:
        new input stream
        Throws:
        IOException
      • obfuscate

        public static byte[] obfuscate​(byte[] in)
        Only obfuscate the byte array. The obfuscation is used to confuse the reader but not to secure the data. TODO: Create more complex algorithm
        Parameters:
        in -
        Returns:
        an obfuscated string
      • unobfuscate

        public static byte[] unobfuscate​(byte[] in)
        Decode obfuscated byte array.
        Parameters:
        in -
        Returns:
        original byte array
      • getMaxLoad

        public static int getMaxLoad​(BigInteger modulus)
        Returns the maximum amount of bytes that can be encrypted at once.
        Parameters:
        modulus -
        Returns:
        maximum byte length
      • md5WithSalt

        public static String md5WithSalt​(String real)
        Create a salt and create a md5 using the salt. The first 4 characters represent the salt.
        Parameters:
        real -
        Returns:
        salt and md5
      • validateMd5WithSalt

        public static boolean validateMd5WithSalt​(String md5,
                                                  String real)
        Check if the md5 and the real are the same. The md5 must be created with md5Salt before.
        Parameters:
        md5 -
        real -
        Returns:
        true if the both values are the same and no exception was thrown
      • encode

        public static byte[] encode​(String passphrase,
                                    byte[] in)
        Encode the byte array synchronous using the pass phrase. The encryption is not stable, two encryptions of the same sample will result in different encryptions.
        Parameters:
        passphrase -
        in -
        Returns:
        encoded byte array
      • decode

        public static byte[] decode​(String passphrase,
                                    byte[] in)
        Decode the byte array synchronous using the pass phrase.
        Parameters:
        passphrase -
        in -
        Returns:
        decoded byte array
      • addPepper

        public static String addPepper​(String content)
        Add a pepper string in front of the content
        Parameters:
        content -
        Returns:
        pepper and content
      • removePepper

        public static String removePepper​(String withPepper)
        Remove a pepper string from front of content
        Parameters:
        withPepper - pepper + content
        Returns:
        content only
      • addPepper

        public static byte[] addPepper​(byte[] content)
        Add a pepper array in front of a byte array
        Parameters:
        content -
        Returns:
        pepper and content as new array
      • removePepper

        public static byte[] removePepper​(byte[] withPepper)
        Remove a pepper from content
        Parameters:
        withPepper -
        Returns:
        new array with content only
      • toUuidHash

        public static UUID toUuidHash​(String in)