To maintain runtime backward compatibility the new encryption methods has been moved in a separated class

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/Common/common-encryption@177195 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2019-02-20 13:53:37 +00:00
parent d25d02c801
commit 836401080c
8 changed files with 195 additions and 83 deletions

View File

@ -5,31 +5,25 @@ import java.security.Key;
/**
*
* @author Roberto Cirillo (CNR)
* @author Luca Frosini (ISTI - CNR)
*
*
* @param <T> the type of the object to encrypt/decrypt
*
* Use {@link org.gcube.common.encryption.encrypter.Encrypter} instead
*/
@Deprecated
public interface IEncrypter<T> {
public T encrypt(T t) throws Exception;
public T encrypt(T t, Key key) throws Exception;
/**
* Encrypts <T> 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 <T> with the given key or the default key
* @param t the object to decrypt

View File

@ -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<String> {
@Deprecated
public class StringEncrypter implements IEncrypter<String>{
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<String> {
* @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)));
}
}

View File

@ -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());

View File

@ -0,0 +1,20 @@
package org.gcube.common.encryption.encrypter;
import java.security.Key;
/**
* @author Luca Frosini (ISTI - CNR)
*
* @param <T> the type of the object to encrypt/decrypt
*/
public interface Encrypter<T> {
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;
}

View File

@ -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<String> {
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)));
}
}

View File

@ -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");

View File

@ -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");
}
}
}

View File

@ -0,0 +1 @@
6 4Z<34>/U<><55> C<><43>ߘ