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

[GAFP-4] : PGP Encryption/Decryption

parent d33ee051
...@@ -31,42 +31,41 @@ public class BlobTriggerFunction { ...@@ -31,42 +31,41 @@ public class BlobTriggerFunction {
) throws IOException { ) throws IOException {
ResourceBundle resourceBundle = new PropertyResourceBundle(new FileInputStream("/Users/eali/Projects/azure/myazurefunctionsdemo/src/main/resources/application.properties")); 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)) { if (Pattern.matches("[a-z|A-Z]*.csv", name)) {
String directoryPath = resourceBundle.getString("files.directory.path")+name; String directoryPath = resourceBundle.getString("files.directory.path") + name;
if (!Files.exists(Paths.get(directoryPath))) { if (!Files.exists(Paths.get(directoryPath))) {
Files.createFile(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); 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"); InputStream inputStreamSecretKey = new FileInputStream(resourceBundle.getString("gpg.keychain.secret.key"));
String inbound = "/Users/eali/Projects/azure/myazurefunctionsdemo/src/main/resources/inbound/eibad.csv"; 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 // Writes data to the output stream
InputStream inputStreamSecretKey = new FileInputStream("/Users/eali/Projects/azure/myazurefunctionsdemo/src/main/resources/keys/gappoc.asc"); OutputStream outbound = new FileOutputStream(resourceBundle.getString("encrypted.files.directory.path") + name.replaceFirst(".csv",".asc"));
InputStream inputStreamPublicKey = new FileInputStream("/Users/eali/Projects/azure/myazurefunctionsdemo/src/main/resources/keys/gappocPublic.asc"); String inbound= resourceBundle.getString("files.directory.path") + name;
char[] pass = {'n','i','s','u','m','1','2','3','4'};
try { try {
// PGPPublicKey key = PGPUtils.readPublicKey(inputStream); // use it when using only public key
// use it when using only public key // PGPPublicKey key = PGPUtils.readPublicKey(inputStream);
// context.getLogger().info("key : "+ key.isEncryptionKey()+" "+key.getCreationTime() +"::"+key.toString());
// 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); // for decryption
// use it when using public + private key // InputStream fileToBeDecrypt = new FileInputStream(resourceBundle.getString("encrypted.files.directory.path") + name.replaceFirst(".csv", ".asc"));
// context.getLogger().info("key : "+ pgpSecretKey.isSigningKey()+" "+pgpSecretKey.getKeyID() +"::"+pgpSecretKey.toString()); // OutputStream inboundN = new FileOutputStream(resourceBundle.getString("decrypted.files.directory.path"));
// context.getLogger().info("----"); // 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) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
......
files.directory.path = /Users/adanish/Documents/azure-function-blob-storage-examples/src/main/resources/files/ files.directory.path = /Users/eali/Projects/azure/myazurefunctionsdemo/src/main/resources/files/
\ No newline at end of file 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