Skip to content

Instantly share code, notes, and snippets.

@axemclion
Created July 14, 2009 16:25
Show Gist options
  • Save axemclion/147042 to your computer and use it in GitHub Desktop.
Save axemclion/147042 to your computer and use it in GitHub Desktop.
ATMOS RSA Keymanager API
package com.emc.smashup.atmos.api;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.commons.codec.binary.Base64;
import com.emc.esu.api.DownloadHelper;
import com.emc.esu.api.EsuApi;
import com.emc.esu.api.Identifier;
import com.emc.esu.api.Metadata;
import com.emc.esu.api.MetadataList;
import com.emc.esu.api.ObjectId;
import com.emc.esu.api.ObjectMetadata;
import com.emc.smashup.rkm.ClientConfig;
import com.rsa.kmc.Decrypter;
import com.rsa.kmc.KMConfig;
import com.rsa.kmc.KMContext;
import com.rsa.kmc.KMKey;
import com.rsa.kmc.KeyAlias;
public class DownloadDecryptHelper extends DownloadHelper {
EsuApi esuApi;
public DownloadDecryptHelper(EsuApi esuApi, byte[] buffer) {
super(esuApi, buffer);
this.esuApi = esuApi;
}
@Override
public void readObject(Identifier id, File f) {
super.readObject(id, f);
byte b[] = new byte[1024];
try {
FileInputStream stream = new FileInputStream(f);
stream = new FileInputStream(f);
String content = "";
int length = 0;
while ((length = stream.read(b)) > 0) {
content += new String(b, 0, length);
}
String decryptedContent = decryptData(content.getBytes());
File file = f.getAbsoluteFile();
FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write(decryptedContent.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public String decryptData(byte b[]) {
String message = "";
try {
KMConfig config = new KMConfig(ClientConfig.SAMPLE_CLIENT_KEYSTORE_PASSWORD,
ClientConfig.SAMPLE_CACHE_PASSWORD, "config/sample_config.properties");
KMContext kmContext = config.newKMContext();
Decrypter decrypter = kmContext.getDecrypter();
b = Base64.decodeBase64(b);
byte[] result = decrypter.doFinal(b, 0, b.length);
message = new String(result);
} catch (Exception e) {
e.printStackTrace();
}
return message;
}
}
package com.emc.smashup.atmos.api;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.commons.codec.binary.Base64;
import com.emc.esu.api.Acl;
import com.emc.esu.api.EsuApi;
import com.emc.esu.api.Metadata;
import com.emc.esu.api.MetadataList;
import com.emc.esu.api.ObjectId;
import com.emc.esu.api.UploadHelper;
import com.emc.smashup.rkm.ClientConfig;
import com.emc.smashup.rkm.RKMKey;
import com.rsa.kmc.Decrypter;
import com.rsa.kmc.Encrypter;
import com.rsa.kmc.HeaderFormat;
import com.rsa.kmc.KMConfig;
import com.rsa.kmc.KMContext;
import com.rsa.kmc.KMKey;
public class UploadEncryptedHelper extends UploadHelper {
private KMKey key;
public UploadEncryptedHelper(EsuApi api) {
super(api);
}
public ObjectId createObject(File f, Acl acl, MetadataList meta) {
byte b[] = new byte[1024];
try {
FileInputStream stream = null;
stream = new FileInputStream(f);
String content = "";
int length = 0;
while ((length = stream.read(b)) > 0) {
content += new String(b, 0, length);
}
byte[] encryptData = encryptData(content.getBytes());
encryptData = Base64.encodeBase64(encryptData);
FileOutputStream outputStream = new FileOutputStream("out.txt");
outputStream.write(encryptData);
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
File f1 = new File("out.txt");
return super.createObject(f1, acl, null);
}
public byte[] encryptData(byte secretData[]) throws Exception {
RKMKey rkmKey = new RKMKey();
key = rkmKey.createKey();
Encrypter encrypter = key.getEncrypter();
encrypter.init();
encrypter.setHeaderFormat(HeaderFormat.VERSION_2_1);
byte[] encryptedData = encrypter.doFinal(secretData, 0, secretData.length);
return encryptedData;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment