Commit 29f9b069 authored by Eibad Ali's avatar Eibad Ali

[GAFP-4] : PGP Encryption/Decryption

parent d33ee051
......@@ -31,42 +31,41 @@ public class BlobTriggerFunction {
) throws IOException {
ResourceBundle resourceBundle = new PropertyResourceBundle(new FileInputStream("/Users/eali/Projects/azure/myazurefunctionsdemo/src/main/resources/application.properties"));
if (Pattern.matches("[a-z|A-Z]*.csv", name)) {
String directoryPath = resourceBundle.getString("files.directory.path")+name;
if (!Files.exists(Paths.get(directoryPath))) {
Files.createFile(Paths.get(directoryPath));
}
String directoryPath = resourceBundle.getString("files.directory.path") + name;
if (!Files.exists(Paths.get(directoryPath))) {
Files.createFile(Paths.get(directoryPath));
}
try (FileOutputStream fileOutputStream = new FileOutputStream(directoryPath, true)) {
try (FileOutputStream fileOutputStream = new FileOutputStream(directoryPath, true)) {
fileOutputStream.write(content);
}
// String encryptedFilePath = resourceBundle.getString("files.directory.path")+name;
OutputStream outbound = new FileOutputStream("/Users/eali/Projects/azure/myazurefunctionsdemo/src/main/resources/outbound/eibad.csv");
String inbound = "/Users/eali/Projects/azure/myazurefunctionsdemo/src/main/resources/inbound/eibad.csv";
InputStream inputStreamSecretKey = new FileInputStream(resourceBundle.getString("gpg.keychain.secret.key"));
InputStream inputStreamPublicKey = new FileInputStream(resourceBundle.getString("gpg.keychain.public.key"));
char[] pass = {'n', 'i', 's', 'u', 'm', '1', '2', '3', '4'};
// Writes data to the output stream
InputStream inputStreamSecretKey = new FileInputStream("/Users/eali/Projects/azure/myazurefunctionsdemo/src/main/resources/keys/gappoc.asc");
InputStream inputStreamPublicKey = new FileInputStream("/Users/eali/Projects/azure/myazurefunctionsdemo/src/main/resources/keys/gappocPublic.asc");
char[] pass = {'n','i','s','u','m','1','2','3','4'};
OutputStream outbound = new FileOutputStream(resourceBundle.getString("encrypted.files.directory.path") + name.replaceFirst(".csv",".asc"));
String inbound= resourceBundle.getString("files.directory.path") + name;
try {
// PGPPublicKey key = PGPUtils.readPublicKey(inputStream);
// use it when using only public key
// context.getLogger().info("key : "+ key.isEncryptionKey()+" "+key.getCreationTime() +"::"+key.toString());
// use it when using only public key
// PGPPublicKey key = PGPUtils.readPublicKey(inputStream);
// use it to get secret key when using public + private key
// PGPSecretKey pgpSecretKey = PGPUtils.readSecretKey(inputStream);
// for encryption
PGPUtils.encryptAndSignFile(outbound, inbound, inputStreamPublicKey, inputStreamSecretKey, true, true, pass);
context.getLogger().info("---File Encrypted---");
// PGPSecretKey pgpSecretKey = PGPUtils.readSecretKey(inputStream);
// use it when using public + private key
// context.getLogger().info("key : "+ pgpSecretKey.isSigningKey()+" "+pgpSecretKey.getKeyID() +"::"+pgpSecretKey.toString());
// context.getLogger().info("----");
// for decryption
// InputStream fileToBeDecrypt = new FileInputStream(resourceBundle.getString("encrypted.files.directory.path") + name.replaceFirst(".csv", ".asc"));
// OutputStream inboundN = new FileOutputStream(resourceBundle.getString("decrypted.files.directory.path"));
// PGPUtils.decryptFile(fileToBeDecrypt, inboundN, inputStreamSecretKey, pass, inputStreamPublicKey);
// PGPUtils.encryptAndSignFile(outbound,inbound,inputStreamPublicKey,inputStreamSecretKey,true,true,pass);
// outbound.close();
//
context.getLogger().info("---encryptAndSignFile---");
InputStream outboundIn = new FileInputStream("/Users/eali/Projects/azure/myazurefunctionsdemo/src/main/resources/outbound/eibad.csv");
OutputStream inboundN = new FileOutputStream("/Users/eali/Projects/azure/myazurefunctionsdemo/src/main/resources/out/a.csv");
PGPUtils.decryptFile(outboundIn,inboundN, inputStreamSecretKey,pass,inputStreamPublicKey);
outboundIn.close();
inboundN.close();
} catch (Exception e) {
e.printStackTrace();
}
......
files.directory.path = /Users/adanish/Documents/azure-function-blob-storage-examples/src/main/resources/files/
\ No newline at end of file
files.directory.path = /Users/eali/Projects/azure/myazurefunctionsdemo/src/main/resources/files/
encrypted.files.directory.path = /Users/eali/Projects/azure/myazurefunctionsdemo/src/main/resources/encrypted/
decrypted.files.directory.path = /Users/eali/Projects/azure/myazurefunctionsdemo/src/main/resources/decrypted/a.csv
gpg.keychain.public.key = /Users/eali/Projects/azure/myazurefunctionsdemo/src/main/resources/keys/gappocPublic.asc
gpg.keychain.secret.key = /Users/eali/Projects/azure/myazurefunctionsdemo/src/main/resources/keys/gappoc.asc
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment