Skip to content

Instantly share code, notes, and snippets.

@jpoetker
Created May 1, 2012 19:58
Show Gist options
  • Save jpoetker/2570933 to your computer and use it in GitHub Desktop.
Save jpoetker/2570933 to your computer and use it in GitHub Desktop.
Encrypts a file and uploads it to Atmos (POC - not robust)
package com.medplus.etech;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
import com.emc.esu.api.Metadata;
import com.emc.esu.api.MetadataList;
import com.emc.esu.api.rest.EsuRestApi;
public class EncryptedUpload {
private static final SecretKeySpec KEY = new SecretKeySpec(
DatatypeConverter.parseBase64Binary("THIS IS A SECRET"),
"AES");
private static long encryptedLength(long unencryptedLength) {
return (unencryptedLength/16 + 1) * 16;
}
private static InputStream createCipherStream(File file) throws FileNotFoundException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
FileInputStream fis = new FileInputStream(file);
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, KEY);
CipherInputStream cis = new CipherInputStream(fis, cipher);
return cis;
}
/**
* @param args
*/
public static void main(String[] args) {
try {
File file = new File(args[0]);
if (file.exists()) {
EsuRestApi esu = new EsuRestApi("api.atmosonline.com",
80,
"uid",
"sharedsecret");
MetadataList metadata = new MetadataList();
metadata.addMetadata(new Metadata("File-Name", args[0], true));
metadata.addMetadata(new Metadata("Encryption-Algorithm", "AES", true));
esu.createObjectFromStream(null,
metadata, createCipherStream(file), encryptedLength(file.length()), null);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment