Package de.mhus.lib.core.crypt
Class MBouncy
- java.lang.Object
-
- de.mhus.lib.core.crypt.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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classMBouncy.RSA_KEY_SIZE
-
Field Summary
Fields Modifier and Type Field Description protected static StringALGORITHM_AESprotected static StringALGORITHM_RSAprotected static StringPROVIDERstatic MBouncy.RSA_KEY_SIZERSA_KEY_SIZE_DEFAULTprotected static StringTRANSFORMATION
-
Constructor Summary
Constructors Constructor Description MBouncy()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static byte[]createRandom(int size)Create a array of 'size' with random content.static byte[]decodeBase64(String text)Decode Base64 encoded string to bytesstatic byte[]decryptAes(byte[] key, byte[] encrypted)Decrypt a encrypted byte array.static StringdecryptAes(byte[] key, String encrypted)Decrypt a base64 encoded string and return the ariginal string.static byte[]decryptRsa(byte[] text, PrivateKey key)Decrypt an encrypted block.static byte[]decryptRsa117(byte[] text, PrivateKey key)Decrypt a single rsa block (128 bytes) with a result of maximal 117 bytes.static StringdecryptRsa117(String text, PrivateKey key)Decrypt a single base64 encrypted byte block.static StringencodeBase64(byte[] bytes)encode bytes with base64 algorithm.static byte[]encryptAes(byte[] key, byte[] data)Encrypt the data using symmetric AES.static StringencryptAes(byte[] key, String data)Encrypt the String using UTF8 encoding and return a base64 encoded string.static byte[]encryptRsa(byte[] text, PublicKey key)Encrypt a unlimited amount of bytes with rsa.static byte[]encryptRsa117(byte[] text, PublicKey key)Encrypt one block with maximal 117 bytes (max block size).static StringencryptRsa117(String text, PublicKey key)Encrypt a single block with max 117 bytes as base64 string.static KeygenerateAesKeySpec(byte[] key)Creates a key object from bytes.static KeyPairgenerateRsaKey(MBouncy.RSA_KEY_SIZE size)Generate a RSA key pair with 1024 bits.static PrivateKeygetPrivateKey(String key)Transforms a string encoded private key into a private key objectstatic StringgetPrivateKey(KeyPair key)Returns the private key of a key pair as stringstatic PublicKeygetPublicKey(String key)Transform a string encoded public key to a public key object.static StringgetPublicKey(KeyPair key)Returns the public key of a key pair as stringstatic KeyPairgetRsaKeyFromPool()Generating RSA keys needs a lot of resources (ca 100ms per key).static voidinit()
-
-
-
Field Detail
-
ALGORITHM_RSA
protected static final String ALGORITHM_RSA
- See Also:
- Constant Field Values
-
PROVIDER
protected static final String PROVIDER
- See Also:
- Constant Field Values
-
TRANSFORMATION
protected static final String TRANSFORMATION
- See Also:
- Constant Field Values
-
ALGORITHM_AES
protected static final String ALGORITHM_AES
- See Also:
- Constant Field Values
-
RSA_KEY_SIZE_DEFAULT
public static final MBouncy.RSA_KEY_SIZE RSA_KEY_SIZE_DEFAULT
-
-
Method Detail
-
init
public static void init()
-
generateRsaKey
public static KeyPair generateRsaKey(MBouncy.RSA_KEY_SIZE size) throws NoSuchAlgorithmException, NoSuchProviderException
Generate a RSA key pair with 1024 bits.- Parameters:
size-- Returns:
- The key pair
- Throws:
NoSuchAlgorithmExceptionNoSuchProviderException
-
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 ExceptionEncrypt 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 ExceptionEncrypt 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 ExceptionDecrypt 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 Textkey-- Returns:
- encrypted string
- Throws:
Exception
-
decryptRsa117
public static byte[] decryptRsa117(byte[] text, PrivateKey key) throws ExceptionDecrypt 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
-
-