diff --git a/src/main/java/org/gcube/common/encryption/IEncrypter.java b/src/main/java/org/gcube/common/encryption/IEncrypter.java index 14b91b6..f72ad90 100644 --- a/src/main/java/org/gcube/common/encryption/IEncrypter.java +++ b/src/main/java/org/gcube/common/encryption/IEncrypter.java @@ -5,31 +5,25 @@ import java.security.Key; /** * * @author Roberto Cirillo (CNR) - * @author Luca Frosini (ISTI - CNR) - * + * * @param the type of the object to encrypt/decrypt + * + * Use {@link org.gcube.common.encryption.encrypter.Encrypter} instead */ +@Deprecated public interface IEncrypter { - public T encrypt(T t) throws Exception; - - public T encrypt(T t, Key key) throws Exception; - /** * Encrypts with the given key or the default key * @param t the object to encrypt * @param key the key * @return the encrypted object * @throws Exception if the key is not available, invalid or the object cannot be encrypted + */ @Deprecated public T encrypt(T t, Key ... key) throws Exception; - - public T decrypt(T t) throws Exception; - - public T decrypt(T t, Key key) throws Exception; - /** * Decrypts with the given key or the default key * @param t the object to decrypt diff --git a/src/main/java/org/gcube/common/encryption/StringEncrypter.java b/src/main/java/org/gcube/common/encryption/StringEncrypter.java index 2b37ae0..65b2148 100644 --- a/src/main/java/org/gcube/common/encryption/StringEncrypter.java +++ b/src/main/java/org/gcube/common/encryption/StringEncrypter.java @@ -8,53 +8,30 @@ import javax.xml.bind.DatatypeConverter; * Encrypter for {@link String} objects * @author Manuele Simi (CNR) * @author Roberto Cirillo (ISTI - CNR) + * @author Lucio Lelii (ISTI - CNR) * @author Luca Frosini (ISTI - CNR) + * + * Use {@link org.gcube.common.encryption.encrypter.StringEncrypter} instead + * */ -public class StringEncrypter implements IEncrypter { - +@Deprecated +public class StringEncrypter implements IEncrypter{ + private static StringEncrypter singleton; - - StringEncrypter() { - } + + StringEncrypter() {} /** * Gets the Encrypter for {@link String} * @return the encrypter */ - public static StringEncrypter getEncrypter() { - if(singleton == null) { + public static StringEncrypter getEncrypter(){ + if (singleton == null) { singleton = new StringEncrypter(); } return singleton; } - /** - * Encrypts the string with the context Key - * @param string the string to encrypt - * @return the encrypted string in a Base64 encoding - * @throws Exception - */ - @Override - public String encrypt(String string) throws Exception { - Key ekey = SymmetricKey.getKey(); - return encrypt(string, ekey); - } - - /** - * Encrypts the string with the given key - * @param string the string to encrypt - * @param key the key for encrypting - * @return the encrypted string in a Base64 encoding - * @throws Exception - */ - @Override - public String encrypt(String string, Key ekey) throws Exception { - Cipher cipher = Cipher.getInstance(ekey.getAlgorithm()); - cipher.init(Cipher.ENCRYPT_MODE, ekey); - // return new String(Base64.encode(cipher.doFinal(string.getBytes()))); - return new String(DatatypeConverter.printBase64Binary((cipher.doFinal(string.getBytes())))); - } - /** * Encrypts the string with the given key * @param key the key for encrypting @@ -62,49 +39,27 @@ public class StringEncrypter implements IEncrypter { * @throws Exception */ @Deprecated - @Override - public String encrypt(String string, Key... key) throws Exception { - Key ekey = (key != null && key.length > 0) ? key[0] : SymmetricKey.getKey(); - return encrypt(string, ekey); + public String encrypt(String string, Key ... key) throws Exception { + Key ekey = (key!=null && key.length>0)? key[0] : SymmetricKey.getKey(); + Cipher cipher = Cipher.getInstance(ekey.getAlgorithm()); + cipher.init(Cipher.ENCRYPT_MODE, ekey); +// return new String(Base64.encode(cipher.doFinal(string.getBytes()))); + return new String(DatatypeConverter.printBase64Binary((cipher.doFinal(string.getBytes())))); } /** * Decrypts the string with the given key * @param key the key to use for decrypting - * @return the decripted string - * @throws Exception - */ - @Override - public String decrypt(String string) throws Exception { - Key dkey = SymmetricKey.getKey(); - return decrypt(string, dkey); - } - - /** - * Decrypts the string with the given key - * @param key the key to use for decrypting - * @return the decripted string - * @throws Exception - */ - @Override - public String decrypt(String string, Key dkey) throws Exception { - Cipher cipher = Cipher.getInstance(dkey.getAlgorithm()); - cipher.init(Cipher.DECRYPT_MODE, dkey); - // return new String(cipher.doFinal(Base64.decode(string.getBytes()))); - return new String(cipher.doFinal(DatatypeConverter.parseBase64Binary(string))); - } - - /** - * Decrypts the string with the given key - * @param key the key to use for decrypting - * @return the decripted string + * @return the decrypted string * @throws Exception */ @Deprecated - @Override - public String decrypt(String string, Key... key) throws Exception { - Key dkey = (key != null && key.length > 0) ? key[0] : SymmetricKey.getKey(); - return decrypt(string, dkey); + public String decrypt(String string, Key ... key) throws Exception { + Key dkey = (key!=null && key.length>0)? key[0] : SymmetricKey.getKey(); + Cipher cipher = Cipher.getInstance(dkey.getAlgorithm()); + cipher.init(Cipher.DECRYPT_MODE, dkey); +// return new String(cipher.doFinal(Base64.decode(string.getBytes()))); + return new String(cipher.doFinal(DatatypeConverter.parseBase64Binary(string))); } } diff --git a/src/main/java/org/gcube/common/encryption/SymmetricKey.java b/src/main/java/org/gcube/common/encryption/SymmetricKey.java index d9607bc..d7cde0d 100644 --- a/src/main/java/org/gcube/common/encryption/SymmetricKey.java +++ b/src/main/java/org/gcube/common/encryption/SymmetricKey.java @@ -41,7 +41,7 @@ public final class SymmetricKey { * @return the key * @throws InvalidKeyException if the key is not available or is invalid */ - protected synchronized static Key getKey() throws InvalidKeyException { + public synchronized static Key getKey() throws InvalidKeyException { if(!keyContextMap.containsKey(ScopeProvider.instance.get())) load(ScopeProvider.instance.get()); return keyContextMap.get(ScopeProvider.instance.get()); diff --git a/src/main/java/org/gcube/common/encryption/encrypter/Encrypter.java b/src/main/java/org/gcube/common/encryption/encrypter/Encrypter.java new file mode 100644 index 0000000..7326296 --- /dev/null +++ b/src/main/java/org/gcube/common/encryption/encrypter/Encrypter.java @@ -0,0 +1,20 @@ +package org.gcube.common.encryption.encrypter; + +import java.security.Key; + +/** + * @author Luca Frosini (ISTI - CNR) + * + * @param the type of the object to encrypt/decrypt + */ +public interface Encrypter { + + public T encrypt(T t) throws Exception; + + public T encrypt(T t, Key key) throws Exception; + + public T decrypt(T t) throws Exception; + + public T decrypt(T t, Key key) throws Exception; + +} diff --git a/src/main/java/org/gcube/common/encryption/encrypter/StringEncrypter.java b/src/main/java/org/gcube/common/encryption/encrypter/StringEncrypter.java new file mode 100644 index 0000000..186787e --- /dev/null +++ b/src/main/java/org/gcube/common/encryption/encrypter/StringEncrypter.java @@ -0,0 +1,83 @@ +package org.gcube.common.encryption.encrypter; + +import javax.crypto.Cipher; +import java.security.Key; +import javax.xml.bind.DatatypeConverter; + +import org.gcube.common.encryption.SymmetricKey; + +/** + * @author Luca Frosini (ISTI - CNR) + */ +public class StringEncrypter implements Encrypter { + + private static StringEncrypter singleton; + + private StringEncrypter() { + } + + /** + * Gets the Encrypter for {@link String} + * @return the encrypter + */ + public static StringEncrypter getEncrypter() { + if(singleton == null) { + singleton = new StringEncrypter(); + } + return singleton; + } + + /** + * Encrypts the string with the context Key + * @param string the string to encrypt + * @return the encrypted string in a Base64 encoding + * @throws Exception + */ + @Override + public String encrypt(String string) throws Exception { + Key ekey = SymmetricKey.getKey(); + return encrypt(string, ekey); + } + + /** + * Encrypts the string with the given key + * @param string the string to encrypt + * @param key the key for encrypting + * @return the encrypted string in a Base64 encoding + * @throws Exception + */ + @Override + public String encrypt(String string, Key ekey) throws Exception { + Cipher cipher = Cipher.getInstance(ekey.getAlgorithm()); + cipher.init(Cipher.ENCRYPT_MODE, ekey); + // return new String(Base64.encode(cipher.doFinal(string.getBytes()))); + return new String(DatatypeConverter.printBase64Binary((cipher.doFinal(string.getBytes())))); + } + + /** + * Decrypts the string with the given key + * @param key the key to use for decrypting + * @return the decripted string + * @throws Exception + */ + @Override + public String decrypt(String string) throws Exception { + Key dkey = SymmetricKey.getKey(); + return decrypt(string, dkey); + } + + /** + * Decrypts the string with the given key + * @param key the key to use for decrypting + * @return the decripted string + * @throws Exception + */ + @Override + public String decrypt(String string, Key dkey) throws Exception { + Cipher cipher = Cipher.getInstance(dkey.getAlgorithm()); + cipher.init(Cipher.DECRYPT_MODE, dkey); + // return new String(cipher.doFinal(Base64.decode(string.getBytes()))); + return new String(cipher.doFinal(DatatypeConverter.parseBase64Binary(string))); + } + +} diff --git a/src/test/java/org/gcube/common/encryption/StringEncrypterTest.java b/src/test/java/org/gcube/common/encryption/StringEncrypterTest.java index 31fc4a1..9deb27b 100644 --- a/src/test/java/org/gcube/common/encryption/StringEncrypterTest.java +++ b/src/test/java/org/gcube/common/encryption/StringEncrypterTest.java @@ -9,6 +9,9 @@ import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; +import junit.framework.Assert; + +@SuppressWarnings("deprecation") public class StringEncrypterTest { static java.security.Key key; @@ -31,7 +34,7 @@ public class StringEncrypterTest { System.out.println("---- STRING ENCRYPTION ----"); System.out.println("String to encrypt " + toEnc); encString = StringEncrypter.getEncrypter().encrypt(toEnc,key); - System.out.println("Encrypted string " + encString); + System.out.println("Encrypted string : " + encString); } catch (Exception e) { e.printStackTrace(); fail("failed to encrypt"); @@ -39,7 +42,9 @@ public class StringEncrypterTest { try { System.out.println("---- STRING DECRYPTION ----"); System.out.println("String to decrypt " + encString); - System.out.println("Decrypted string " + StringEncrypter.getEncrypter().decrypt(encString,key)); + String decryptedString = StringEncrypter.getEncrypter().decrypt(encString,key); + System.out.println("Decrypted string : " + decryptedString); + Assert.assertTrue(decryptedString.compareTo(toEnc)==0); } catch (Exception e) { e.printStackTrace(); fail("failed to decrypt"); diff --git a/src/test/java/org/gcube/common/encryption/encrypter/StringEncrypterTest.java b/src/test/java/org/gcube/common/encryption/encrypter/StringEncrypterTest.java new file mode 100644 index 0000000..e3e9ed2 --- /dev/null +++ b/src/test/java/org/gcube/common/encryption/encrypter/StringEncrypterTest.java @@ -0,0 +1,54 @@ +package org.gcube.common.encryption.encrypter; + +import static org.junit.Assert.*; + +import org.gcube.common.encryption.encrypter.StringEncrypter; +import org.gcube.common.encryption.SymmetricKey; +import org.gcube.common.scope.api.ScopeProvider; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +import junit.framework.Assert; + +public class StringEncrypterTest { + + static java.security.Key key; + static String toEnc = "String to encrypt"; + static String encString; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + ScopeProvider.instance.set("/gcube/devsec"); + key = SymmetricKey.getKey(); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + @Test + public final void testEncryptDecrypt() { + try { + System.out.println("---- STRING ENCRYPTION ----"); + System.out.println("String to encrypt " + toEnc); + encString = StringEncrypter.getEncrypter().encrypt(toEnc,key); + System.out.println("Encrypted string " + encString); + } catch (Exception e) { + e.printStackTrace(); + fail("failed to encrypt"); + } + try { + System.out.println("---- STRING DECRYPTION ----"); + System.out.println("String to decrypt " + encString); + String decryptedString = StringEncrypter.getEncrypter().decrypt(encString,key); + System.out.println("Decrypted string " + decryptedString); + Assert.assertTrue(decryptedString.compareTo(toEnc)==0); + } catch (Exception e) { + e.printStackTrace(); + fail("failed to decrypt"); + } + } + + +} diff --git a/src/test/resources/devsec.gcubekey b/src/test/resources/devsec.gcubekey new file mode 100644 index 0000000..260f269 --- /dev/null +++ b/src/test/resources/devsec.gcubekey @@ -0,0 +1 @@ +6 4Zð/Uä‰ Cå±ß˜ \ No newline at end of file