diff --git a/.idea/crypto-java.iml b/.idea/crypto-java.iml deleted file mode 100644 index fbb3494..0000000 --- a/.idea/crypto-java.iml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 93e4b49..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/build.gradle b/build.gradle index c9c44b9..d04b276 100644 --- a/build.gradle +++ b/build.gradle @@ -3,7 +3,7 @@ plugins { } group 'com.cubetiqs' -version '1.0' +version '1.1' repositories { mavenCentral() diff --git a/src/main/java/com/cubetiqs/crypto/Crypto.java b/src/main/java/com/cubetiqs/crypto/Crypto.java index e3112ba..c19dce3 100644 --- a/src/main/java/com/cubetiqs/crypto/Crypto.java +++ b/src/main/java/com/cubetiqs/crypto/Crypto.java @@ -1,6 +1,8 @@ package com.cubetiqs.crypto; +import com.cubetiqs.crypto.core.CryptoUtil; import com.cubetiqs.crypto.provider.DefaultCryptoProvider; +import com.cubetiqs.crypto.util.FunctionUtil; /** * Crypto, provides the methods to encrypt and decrypt the data with implemented for any provider @@ -33,21 +35,41 @@ public byte[] decrypt(byte[] encrypted) { private static Crypto instance; - public static Crypto createInstance(CryptoProvider provider) { + public static Crypto newInstance(CryptoProvider provider) { return new Crypto(provider); } - public static Crypto newInstance(CryptoProvider provider) { + public static Crypto createInstance(CryptoProvider provider) { if (instance == null) { - instance = createInstance(provider); + instance = newInstance(provider); } return instance; } + public static Crypto newDefaultInstance(String keyText, String ivText) { + return newInstance(DefaultCryptoProvider.newInstance(encodeToBase64(keyText), encodeToBase64(ivText))); + } + public static Crypto defaultInstance(String key, String iv) { if (instance == null) { - instance = createInstance(new DefaultCryptoProvider(key, iv)); + return createInstance(DefaultCryptoProvider.newInstance(key, iv)); } return instance; } + + public static void resetInstance() { + instance = null; + } + + public static String encodeToBase64(String text) { + return FunctionUtil.encodeToBase64(text); + } + + public static String createKey() { + return CryptoUtil.createKey(32, null, null); + } + + public static String createIV() { + return CryptoUtil.createRandomString(16); + } } diff --git a/src/main/java/com/cubetiqs/crypto/core/CryptoUtil.java b/src/main/java/com/cubetiqs/crypto/core/CryptoUtil.java index 3d80967..1f66bc6 100644 --- a/src/main/java/com/cubetiqs/crypto/core/CryptoUtil.java +++ b/src/main/java/com/cubetiqs/crypto/core/CryptoUtil.java @@ -12,12 +12,19 @@ import java.util.HashMap; import java.util.Map; -import static com.cubetiqs.crypto.util.FunctionUtil.DEFAULT_ENCODING; -import static com.cubetiqs.crypto.util.FunctionUtil.decodeBase64; +import static com.cubetiqs.crypto.util.FunctionUtil.*; public final class CryptoUtil { private static final int BUFFER_SIZE = 4 * 1024; + public static String createKey(int length, String algorithm, Integer keySize) { + return generateKey(length, algorithm, keySize); + } + + public static String createRandomString(int length) { + return generateRandomString(length); + } + public static byte[] encrypt(byte[] data, CryptoProvider.CryptoOptions options) { try ( InputStream fis = new ByteArrayInputStream(data); diff --git a/src/main/java/com/cubetiqs/crypto/provider/DefaultCryptoProvider.java b/src/main/java/com/cubetiqs/crypto/provider/DefaultCryptoProvider.java index 4356d34..0286416 100644 --- a/src/main/java/com/cubetiqs/crypto/provider/DefaultCryptoProvider.java +++ b/src/main/java/com/cubetiqs/crypto/provider/DefaultCryptoProvider.java @@ -14,11 +14,52 @@ public class DefaultCryptoProvider implements CryptoProvider { private final CryptoOptions options; + /** + * Create new instance of Default Crypto Provider. + * + * @param key String | Key (32) (Base64 encode) for encryption. + * @param iv String | IV (16) (Base64 encode) for encryption. + */ public DefaultCryptoProvider(String key, String iv) { assert key != null; this.options = new Options(key, iv); } + /** + * Create new instance of Default Crypto Provider. + * + * @param key String | Key (32) (Base64 encode) for encryption. + */ + public DefaultCryptoProvider(String key) { + this(key, null); + } + + /** + * Create new instance for CryptoProvider with Default Crypto Provider. + * + * @param key String | Key (32) (Base64 encode) for encryption. + * @param iv String | IV (16) (Base64 encode) for encryption. + * @return CryptoProvider + */ + public static CryptoProvider newInstance(String key, String iv) { + return new DefaultCryptoProvider(key, iv); + } + + /** + * Create new instance for CryptoProvider with Default Crypto Provider. + * + * @param key String | Key (32) (Base64 encode) for encryption. + * @return CryptoProvider + */ + public static CryptoProvider newInstance(String key) { + return newInstance(key, null); + } + + /** + * Create default instance of Default Crypto Provider. + * + * @param options CryptoOptions + */ public DefaultCryptoProvider(CryptoOptions options) { assert options != null; this.options = options; @@ -29,10 +70,20 @@ public CryptoOptions getOptions() { return options; } + /** + * Get Key for encryption. + * + * @return String | Base64 encoded key. + */ public String getKey() { return getOptions().getKey(); } + /** + * Get Iv for encryption. + * + * @return String | Base64 encoded iv. + */ public String getIv() { return getOptions().getIv(); } diff --git a/src/main/java/com/cubetiqs/crypto/util/FunctionUtil.java b/src/main/java/com/cubetiqs/crypto/util/FunctionUtil.java index 8e641e3..7881e7b 100644 --- a/src/main/java/com/cubetiqs/crypto/util/FunctionUtil.java +++ b/src/main/java/com/cubetiqs/crypto/util/FunctionUtil.java @@ -3,13 +3,25 @@ import com.cubetiqs.crypto.CryptoProvider; import com.cubetiqs.crypto.core.CryptoUtil; +import javax.crypto.KeyGenerator; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; +import java.security.Key; +import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; import java.util.Base64; public final class FunctionUtil { public static final Charset DEFAULT_ENCODING = StandardCharsets.UTF_8; + public static String encodeToBase64(String text) { + if (text == null) { + return null; + } + byte[] bytes = encodeBase64(text.getBytes(DEFAULT_ENCODING)); + return new String(bytes, DEFAULT_ENCODING); + } + public static byte[] encodeBase64(byte[] bytes) { return Base64.getEncoder().encode(bytes); } @@ -19,6 +31,9 @@ public static byte[] decodeBase64(byte[] bytes) { } public static byte[] decodeBase64FromString(String str) { + if (str == null) { + return null; + } return decodeBase64(str.getBytes(DEFAULT_ENCODING)); } @@ -43,4 +58,34 @@ public static String decrypt(String encrypted, CryptoProvider.CryptoOptions opti return new String(data, DEFAULT_ENCODING); } + + public static String generateRandomString(int length) { + byte[] iv = new byte[length]; + SecureRandom random = new SecureRandom(); + random.nextBytes(iv); + return Base64.getEncoder().encodeToString(iv).substring(0, length); + } + + public static String generateKey(int length, String algorithm, Integer keySize) { + try { + if (algorithm == null) { + algorithm = "AES"; + } + if (keySize == null) { + keySize = 256; + } + if (length == 0) { + length = 32; + } + Key key; + SecureRandom random = new SecureRandom(); + KeyGenerator keyGenerator = KeyGenerator.getInstance(algorithm); + keyGenerator.init(keySize, random); + key = keyGenerator.generateKey(); + return Base64.getEncoder().encodeToString(key.getEncoded()).substring(0, length); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + return null; + } + } } diff --git a/src/test/java/BasicTest.java b/src/test/java/BasicTest.java index e4f180a..2251197 100644 --- a/src/test/java/BasicTest.java +++ b/src/test/java/BasicTest.java @@ -29,4 +29,12 @@ public void decryptTest() { System.out.println("Decrypted: " + decrypted); Assertions.assertEquals(TEXT, decrypted); } + + @Test + public void encodeToBase64Test() { + String text = "Hello World"; + String textBase64 = "SGVsbG8gV29ybGQ="; + String encoded = FunctionUtil.encodeToBase64(text); + Assertions.assertEquals(textBase64, encoded); + } } diff --git a/src/test/java/DefaultCryptoV2Test.java b/src/test/java/DefaultCryptoV2Test.java new file mode 100644 index 0000000..d046c27 --- /dev/null +++ b/src/test/java/DefaultCryptoV2Test.java @@ -0,0 +1,29 @@ +import com.cubetiqs.crypto.Crypto; +import com.cubetiqs.crypto.util.FunctionUtil; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class DefaultCryptoV2Test { + @Test + public void encryptAndDecryptV2Test() { + String key = Crypto.createKey(); + String iv = Crypto.createIV(); + String rawText = "I'm Sambo Chea"; + + Crypto crypto = Crypto.newDefaultInstance(key, iv); + String encrypted = crypto.encrypt(rawText); + String decrypted = crypto.decrypt(encrypted); + + System.out.println("Encrypted: " + encrypted); + System.out.println("Decrypted: " + decrypted); + Assertions.assertEquals(rawText, decrypted); + } + + @Test + public void generateKeyTest() { + String ivBase64 = FunctionUtil.generateRandomString(16); + String keyBase642 = FunctionUtil.generateKey(32, null, null); + System.out.println(ivBase64); + System.out.println(keyBase642); + } +}