Skip to main content

Encrypting and Decrypting with cloudCrypt

Encrypting and decrypting while copying to and from Amazon Web Services S3 or Dropbox.  cloudCrypt requires additional libraries located in the cloudCryptlib folder and the Pi SoftOnlineJCE-[version].jar as well as the Java agent jar, TEAdmin-[version].jar


Code Samples

See the Java environment settings for details on setting the project environment.


Java_cloudCryp

The following example demonstrates setting the properties for the AWS S3 Connector, encrypting on uploading and decrypting on downloading.

package cloudcrypt.secure;

import com.eruces.security.JniProvider;
import com.eruces.teagent.*;

import cloudcrypt.secure.io.IOConfig;

import java.io.*;
import java.net.URI;
import java.security.Provider;
import java.security.Security;
import java.security.Provider.Service;

public class Java_CloudCrypt {

private static java.security.KeyStore ks;

private final static String KEY_STORE_NAME = "EJKeyStore";
private final static String SERVER_NAME = "10.60.2.24";
private final static int PORT = 8888;
private final static String USER = "user";
private final static String PWD = "password1A";

private final static String S3_ACCESS_KEY = "BA5K23VMGHNVWOPQKDNS";
private final static String S3_SECRET_KEY = "fSM3DadEhDFs+FaQdsFWls/2Absdfe/2aSDbMPs+";
private final static String S3_SITE = "s3.amazonaws.com";

public static boolean CheckForErucesJCE() {

Provider[] provs = null;

try {
provs = java.security.Security.getProviders();
}
catch (UnsatisfiedLinkError e) {
if (e.getMessage().toLowerCase().indexOf("erucesjce") != -1) {
System.out.println("It seems that the dynamic library containing the Eruces JCE encryption classes was not found in the path.");
System.out.println("This library (Windows: \"erucesjce.dll\", Linux: \"liberucesjce.so\") must be present in \"java.library.path\".");
}
else {
System.out.println("Linkage has failed for a security library other than Eruces JCE. Please correct this issue to ensure proper behavior.");
e.printStackTrace();
}

return false;
}

boolean eruces_jce_listed = false;
Provider provider = null;

for (int i = 0; i < provs.length; i++) {
if (provs[i].getName() == "ERUCESJCE") {
eruces_jce_listed = true;
provider = provs[i];
}
}

if (eruces_jce_listed) {
System.out.println("The Eruces JCE seems to be available. This is a list of algorithms available:");

for (Service service : provider.getServices()) {
System.out.format("%1$-" + 20 + "s" + "%2$-" + 20 + "s" + "\n", service.getType(), service.getAlgorithm());
}
return true;
}

System.out.println("The ERUECES JCE provider does not seem to be available. The provider is available in ErucesOnlineJce-version.jar");
System.out.println("You must install the Java Cryptography Extension files, \"Unlimited Strength Jurisdiction Policy Files\" available from Oracle.");
System.out.println("You must edit the \"java.security\" file to include \"com.eruces.security.JniProvider\" as a security provider.");
return false;

}

public static void SetUpEncDec() throws Exception {

Security.addProvider(new JniProvider());

UPAuthenticationContext ctx = (UPAuthenticationContext)TEAgentConnectionFactory.getAuthenticationContext(TEAuthenticationType.USERANDPASSWORD);
System.setProperty("com.eruces.security.ServerName", SERVER_NAME);
System.setProperty("com.eruces.security.ServerPort", Integer.toString(PORT));
ctx.setUser(USER);
ctx.setPasswd(PWD);

TEAgentConnection conn = TEAgentConnectionFactory.getConnection(TEConnectionType.SECURE_SOCKET);
conn.open(SERVER_NAME, PORT, ctx);
String token = conn.getEncodedCookie();

ks = java.security.KeyStore.getInstance(KEY_STORE_NAME);
System.setProperty("com.eruces.security.KeyStoreAuthenticationType", "Token");
ks.load(new ByteArrayInputStream(token.getBytes()), new char[0]);

}

public static void TestEncrypt(String filename) throws Exception {

System.out.println("Encrypting a file...");

IOConfig cfg = new IOConfig();
cfg.put(IOConfig.CIPHER_KEY_STORE, ks);

IOProcessor p = new IOProcessor();
p.setInput((new File(filename)).toURI(), 1);
p.setOutput((new File(filename + ".enc")).toURI(), 1);
p.setOperation(IOProcessor.IOCryptOperation.Encrypt);

p.setConfig(cfg);

p.process();
}

public static void TestDecrypt(String filename) throws Exception {

System.out.println("Decrypting a file...");

IOConfig cfg = new IOConfig();
cfg.put(IOConfig.CIPHER_KEY_STORE, ks);

IOProcessor p = new IOProcessor();
p.setInput((new File(filename)).toURI(), 1);
p.setOutput((new File(filename + ".dec")).toURI(), 1);
p.setOperation(IOProcessor.IOCryptOperation.Decrypt);

p.setConfig(cfg);

p.process();
}


public static void TestCopy(String filename) throws Exception {

System.out.println("Copying a file...");

IOConfig cfg = new IOConfig();
cfg.put(IOConfig.CIPHER_KEY_STORE, ks);

IOProcessor p = new IOProcessor();
p.setInput((new File(filename)).toURI(), 1);
p.setOutput((new File(filename + ".cpy")).toURI(), 1);
p.setOperation(IOProcessor.IOCryptOperation.Nop);

p.setConfig(cfg);

p.process();
}

public static void TestUploadS3(String filename_a, String filename_b) throws Exception {

System.out.println("Uploading a file to Amazon S3...");

IOConfig cfg = new IOConfig();
cfg.setProperty("S3.Site", S3_SITE);
cfg.setProperty("S3.AccessKey", S3_ACCESS_KEY);
cfg.setProperty("S3.SecretKey", S3_SECRET_KEY);

IOProcessor p = new IOProcessor();
p.setInput((new File(filename_a)).toURI(), 1);
p.setOutput(URI.create(filename_b), 1);
p.setOperation(IOProcessor.IOCryptOperation.Nop);

p.setConfig(cfg);

p.process();
}

public static void TestDownloadS3(String filename_a, String filename_b) throws Exception {

System.out.println("Downloading a file from Amazon S3...");

IOConfig cfg = new IOConfig();
cfg.setProperty("S3.Site", S3_SITE);
cfg.setProperty("S3.AccessKey", S3_ACCESS_KEY);
cfg.setProperty("S3.SecretKey", S3_SECRET_KEY);

IOProcessor p = new IOProcessor();
p.setInput(URI.create(filename_a), 1);
p.setOutput((new File(filename_b)).toURI(), 1);
p.setOperation(IOProcessor.IOCryptOperation.Nop);

p.setConfig(cfg);

p.process();
}

public static void TestEncryptedUploadS3(String filename_a, String filename_b) throws Exception {

System.out.println("Encrypting and uploading a file to Amazon S3...");

IOConfig cfg = new IOConfig();
cfg.put(IOConfig.CIPHER_KEY_STORE, ks);
cfg.setProperty("S3.Site", S3_SITE);
cfg.setProperty("S3.AccessKey", S3_ACCESS_KEY);
cfg.setProperty("S3.SecretKey", S3_SECRET_KEY);

IOProcessor p = new IOProcessor();
p.setInput((new File(filename_a)).toURI(), 1);
p.setOutput(URI.create(filename_b), 1);
p.setOperation(IOProcessor.IOCryptOperation.Encrypt);

p.setConfig(cfg);

p.process();
}

public static void TestDecryptedDownloadS3(String filename_a, String filename_b) throws Exception {

System.out.println("Downloading and decrypting a file from Amazon S3...");

IOConfig cfg = new IOConfig();
cfg.put(IOConfig.CIPHER_KEY_STORE, ks);
cfg.setProperty("S3.Site", S3_SITE);
cfg.setProperty("S3.AccessKey", S3_ACCESS_KEY);
cfg.setProperty("S3.SecretKey", S3_SECRET_KEY);

IOProcessor p = new IOProcessor();
p.setInput(URI.create(filename_a), 1);
p.setOutput((new File(filename_b)).toURI(), 1);
p.setOperation(IOProcessor.IOCryptOperation.Decrypt);

p.setConfig(cfg);

p.process();
}

public static void main(String args[]) {

try {
SetUpEncDec();

TestEncrypt("c:/test/test1.txt");
TestDecrypt("c:/test/test1.txt.enc");
TestCopy("c:/test/test1.txt");

TestUploadS3("c:/test/test1.txt", "s3://forthreadtest/test_file.txt");
TestDownloadS3("s3://forthreadtest/test_file.txt", "c:/test/test1.txt");

TestEncryptedUploadS3("c:/test/test1.txt", "s3://forthreadtest/test_file.txt");
TestDecryptedDownloadS3("s3://forthreadtest/test_file.txt", "c:/test/test1.txt");

}
catch (Exception e) {
if (CheckForErucesJCE() == false) {
return;
}
e.printStackTrace();
}

}

}