Пример #1
0
 public static API_OpenPgp moveFilesToFolder(this API_OpenPgp openPgp, string targetFolder)
 {
     targetFolder.createDir();     // make sure it exists
     openPgp.PrivateKey = Files.copy(openPgp.PrivateKey, targetFolder);
     openPgp.PublicKey  = Files.copy(openPgp.PublicKey, targetFolder);
     return(openPgp);
 }
Пример #2
0
 public static string encryptFile(this API_OpenPgp openPgp, string fileToEncrypt)
 {
     try
     {
         var publicKey = openPgp.PublicKey;
         if (publicKey.fileExists().isFalse())
         {
             publicKey = PublicDI.CurrentScript.directoryName().pathCombine(publicKey);
         }
         if (fileToEncrypt.fileExists().isFalse())
         {
             "[API_OpenPgp] in API_OpenPgp signFile, the provided file to encrypt doesn't exist: {0}".error(fileToEncrypt);
             return("");
         }
         var keyIn = File.OpenRead(publicKey);
         var pathToEncryptedFile = fileToEncrypt + ".asc";
         var fos = File.Create(pathToEncryptedFile);
         EncryptFile(fos, fileToEncrypt, OpenPgp_HelperMethods.ReadPublicKey(keyIn), true, true);
         fos.Close();
         return(pathToEncryptedFile);
     }
     catch (Exception ex)
     {
         ex.log("[API_OpenPgp]  in encryptFile");
         return(null);
     }
 }
Пример #3
0
        public static string createKey(this API_OpenPgp openPgp)
        {
            string random_Identity   = 12.randomLetters();
            string random_passPhrase = 64.randomLetters();

            return(openPgp.createKey(random_Identity, random_passPhrase));
        }
Пример #4
0
 public static string save(this API_OpenPgp openPgp, string pathToSave)
 {
     if (openPgp.serialize(pathToSave))
     {
         return(pathToSave);
     }
     return("");
 }
Пример #5
0
        public static string createKey(this API_OpenPgp openPgp, string identity, string passPhrase, string targetFolder)
        {
            targetFolder.createDir();           // make sure it is created
            string pathTo_PrivateKey = targetFolder.pathCombine("{0}_{1}".format(identity, defaultSufix_PrivateKey));
            string pathTo_PublicKey  = targetFolder.pathCombine("{0}_{1}".format(identity, defaultSufix_PublicKey));

            return(openPgp.createKey(identity, passPhrase, pathTo_PrivateKey, pathTo_PublicKey));
        }
        public void createKey()
        {
        	var openPgp = new API_OpenPgp();
			openPgp.createKey(); 
			//openPgp.moveFilesToFolder(@"C:\O2\_USERDATA\Pgp");
			var file = @"C:\O2\_USERDATA\Pgp\PgpDetails for {0}.xml".format(openPgp.Identity);
			openPgp.save(file); 
			//show.file(file); 
			Assert.That(openPgp.PrivateKey.fileExists(), "PrivateKey file did not exist");
			Assert.That(openPgp.PublicKey.fileExists(), "PublicKey  file did not exist");
			Assert.That(file.fileExists(), "Saved API_OpenPgp file did not exist");			
        }
        public void createKey()
        {
            var openPgp = new API_OpenPgp();

            openPgp.createKey();
            //openPgp.moveFilesToFolder(@"C:\O2\_USERDATA\Pgp");
            var file = @"C:\O2\_USERDATA\Pgp\PgpDetails for {0}.xml".format(openPgp.Identity);

            openPgp.save(file);
            //show.file(file);
            Assert.That(openPgp.PrivateKey.fileExists(), "PrivateKey file did not exist");
            Assert.That(openPgp.PublicKey.fileExists(), "PublicKey  file did not exist");
            Assert.That(file.fileExists(), "Saved API_OpenPgp file did not exist");
        }
        //TODo: make this generic
        public void encryptAndDecrypt()
        {
            var testPgpAccount = @"C:\O2\_USERDATA\Pgp\PgpDetails for fgRcFCSvXtGq.xml";
            var testFile       = @"C:\O2\_USERDATA\Pgp\TestContentFile";
            var testFile_2     = @"C:\O2\_USERDATA\Pgp\TestContentFile_2.txt";

            var openPgp       = API_OpenPgp.load(testPgpAccount);
            var encryptedFile = openPgp.encryptFile(testFile);

            Assert.That(encryptedFile.fileExists(), "Encrypted File didn't exist");

            var decryptedFile = openPgp.decryptFile(encryptedFile);

            Assert.That(decryptedFile.fileExists(), "Decrypted File didn't exist");
        }
Пример #9
0
 public static string encryptText(this API_OpenPgp openPgp, string textToEncrypt)
 {
     try
     {
         if (openPgp.notNull())
         {
             var tempFile       = textToEncrypt.save();
             var resultTempFile = openPgp.encryptFile(tempFile);
             var result         = resultTempFile.fileContents();
             Files.deleteFile(tempFile);
             Files.deleteFile(resultTempFile);
             return(result);
         }
     }
     catch (Exception ex)
     {
         ex.log("[API_OpenPgp] in EncryptText");
     }
     return(null);
 }
Пример #10
0
 public static string decryptText(this API_OpenPgp openPgp, string textToEncrypt)
 {
     try
     {
         if (openPgp.notNull())
         {
             var tempFile = textToEncrypt.saveWithExtension(".asc");
             "tempFile: {0}".info(tempFile);
             var resultTempFile = openPgp.decryptFile(tempFile);
             var result         = resultTempFile.fileContents();
             Files.deleteFile(tempFile);
             Files.deleteFile(resultTempFile);
             return(result);
         }
     }
     catch (Exception ex)
     {
         ex.log("[API_OpenPgp] in DecryptText");
     }
     return(null);
 }
Пример #11
0
        public static string decryptFile(this API_OpenPgp openPgp, string fileToDecrypt)
        {
            "In Decrypt step".info();
            var privateKey = openPgp.PrivateKey;

            if (privateKey.fileExists().isFalse())
            {
                privateKey = PublicDI.CurrentScript.directoryName().pathCombine(privateKey);
            }
            var passPhrase = openPgp.PassPhrase;

            if (passPhrase.valid().isFalse())
            {
                passPhrase = "What is the passphase to unlock the Private Key?".askUser();
            }

            if (fileToDecrypt.fileExists().isFalse())
            {
                "in API_OpenPgp signFile, the provided file to decrypt doesn't exist: {0}".error(fileToDecrypt);
                return("");
            }
            var keyIn             = File.OpenRead(privateKey);
            var fin               = File.OpenRead(fileToDecrypt);
            var originalExtension = Path.GetFileNameWithoutExtension(fileToDecrypt).extension();

            if (originalExtension.valid().isFalse())
            {
                originalExtension = ".out";
            }
            var pathToDecryptedFile = fileToDecrypt + originalExtension;             // new FileInfo(args[1]).Name + ".out"

            DecryptFile(fin, keyIn, passPhrase.ToCharArray(), pathToDecryptedFile);
            fin.Close();
            keyIn.Close();
            //var pathToEncryptedFile = fileToEncrypt + ".asc";
            //var fos = File.Create(pathToEncryptedFile);
            //EncryptFile(fos, fileToEncrypt, OpenPgp_HelperMethods.ReadPublicKey(keyIn), true, true);
            //fos.Close();
            return(pathToDecryptedFile);
        }
Пример #12
0
        public static API_OpenPgp fixOpenPgpKeysPaths(this API_OpenPgp openPgp, string file)
        {
            // see if we need to resolve the paths
            if (openPgp.PrivateKey.fileExists().isFalse())
            {
                var localPrivateKey = file.directoryName().pathCombine(openPgp.PrivateKey);
                if (localPrivateKey.fileExists())
                {
                    openPgp.PrivateKey = localPrivateKey;
                }
            }

            if (openPgp.PublicKey.fileExists().isFalse())
            {
                var localPublicKey = file.directoryName().pathCombine(openPgp.PublicKey);
                if (localPublicKey.fileExists())
                {
                    openPgp.PublicKey = localPublicKey;
                }
            }
            return(openPgp);
        }
Пример #13
0
        public static string createKey(this API_OpenPgp openPgp, string identity, string passPhrase, string pathTo_PrivateKey, string pathTo_PublicKey)
        {
            if (identity.valid().isFalse())
            {
                "[API_OpenPgp] there was no value provided for the mandatory field: PGP Identity".error();
                return(null);
            }
            IAsymmetricCipherKeyPairGenerator kpg = GeneratorUtilities.GetKeyPairGenerator("RSA");

            kpg.Init(new RsaKeyGenerationParameters(
                         //BigInteger.ValueOf(0x10001), new SecureRandom(), 1024, 25));
                         BigInteger.ValueOf(0x10001), new SecureRandom(), 2048, 25));

            AsymmetricCipherKeyPair kp = kpg.GenerateKeyPair();

            Stream privateKey, publicKey;

            privateKey = File.Create(pathTo_PrivateKey);
            publicKey  = File.Create(pathTo_PublicKey);

            new RsaKeyRingGenerator().ExportKeyPair(privateKey, publicKey, kp.Public, kp.Private, identity, passPhrase.ToCharArray(), true);

            privateKey.Close();
            publicKey.Close();

            openPgp.PrivateKey = pathTo_PrivateKey;
            openPgp.PublicKey  = pathTo_PublicKey;
            openPgp.Identity   = identity;
            openPgp.PassPhrase = passPhrase;

            var openPgpFile = pathTo_PublicKey.directoryName().pathCombine("{0}_OpenPgp.xml".format(identity));

            openPgp.save(openPgpFile);
            "[API_OpenPgp] Pgp mappings for identity '{0}' saved to: {0}".debug(openPgpFile);
            return(openPgpFile);
        }
Пример #14
0
 public static string save(this API_OpenPgp openPgp)
 {
     return(openPgp.save("_API_OpenPgp.xml".tempFile()));
 }
Пример #15
0
 public static string createKey(this API_OpenPgp openPgp, string identity, string passPhrase)
 {
     return(openPgp.createKey(identity, passPhrase, "".tempDir()));
 }