Package de.mhus.lib.core.crypt
Class MCrypt
- java.lang.Object
-
- de.mhus.lib.core.crypt.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 Summary
Constructors Constructor Description MCrypt()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static byte[]addPepper(byte[] content)Add a pepper array in front of a byte arraystatic StringaddPepper(String content)Add a pepper string in front of the contentstatic InputStreamcreateCipherInputStream(InputStream parent, String passphrase)Create a stream to decode a data stream with a simple pass phrase using createCipherOutputStream.static OutputStreamcreateCipherOutputStream(OutputStream parent, String passphrase)Create a output stream automatically encode the stream with the pass phrase.static OutputStreamcreateCipherOutputStream(OutputStream parent, String passphrase, int version)static CipherBlockRotatecreateRandomCipherBlockRotate(int size)Create a random block using the MRandom service.static Stringdecode(AsyncKey key, String in)Decode the data using Base91 byte encoding and the private key from 'key' using a RSA like algorithm.static BigIntegerdecode(AsyncKey key, BigInteger in)Decode one byte using the private key.static BigInteger[]decode(AsyncKey key, BigInteger[] in)Decode the data using the private key from 'key' using a RSA like algorithm.static byte[]decode(String passphrase, byte[] in)Decode the byte array synchronous using the pass phrase.static byte[]decodeBytes(AsyncKey key, BigInteger[] in)Decode data using the private key from 'key' using a RSA like algorithm.static StringdecodeWithSalt(AsyncKey key, String in)Decode the data using Base91 byte encoding and the private key from 'key' using a RSA like algorithm.static Stringencode(AsyncKey key, String in)Encode data using a RSA like algorithm.static BigIntegerencode(AsyncKey key, BigInteger in)Encode data using a RSA like algorithm.static byte[]encode(String passphrase, byte[] in)Encode the byte array synchronous using the pass phrase.static BigInteger[]encodeBytes(AsyncKey key, byte[] in)Encode data using a RSA like algorithm.static BigInteger[]encodeBytes(AsyncKey key, BigInteger[] in)Encode data using a RSA like algorithm.static StringencodeWithSalt(AsyncKey key, String in)Encode data using a RSA like algorithm.static intgetMaxLoad(BigInteger modulus)Returns the maximum amount of bytes that can be encrypted at once.static AsyncKeyloadPrivateRsaKey(File file)Load a private key from file.static AsyncKeyloadPrivateRsaKey(String key)Load a RSA private key into a AsyncKey object.static AsyncKeyloadPublicRsaKey(File file)Load a public key from file.static AsyncKeyloadPublicRsaKey(String key)Load a RSA public key into a AsyncKey object.static Stringmd5(String real)static Stringmd5WithSalt(String real)Create a salt and create a md5 using the salt.static byte[]obfuscate(byte[] in)Only obfuscate the byte array.static byte[]removePepper(byte[] withPepper)Remove a pepper from contentstatic StringremovePepper(String withPepper)Remove a pepper string from front of contentstatic Stringsha256(String text)static UUIDtoUuidHash(String in)static byte[]unobfuscate(byte[] in)Decode obfuscated byte array.static booleanvalidateMd5WithSalt(String md5, String real)Check if the md5 and the real are the same.
-
-
-
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 keyin- 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 keyin- 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 keyin- 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 keyin- 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 keyin- clear data- Returns:
- encoded data
- Throws:
IOException
-
decode
public static BigInteger decode(AsyncKey key, BigInteger in) throws IOException
Decode one byte using the private key.- Parameters:
key- private keyin- encoded byte- Returns:
- decoded byte
- 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 keyin- 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 keyin- 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 keyin- 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 keyin- 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
-
createCipherOutputStream
public static OutputStream createCipherOutputStream(OutputStream parent, String passphrase, int version) throws IOException
- 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
-
-