Ejemplo n.º 1
0
        public static void WriteEncryptedFile(string toEncrypt, string outputFile,
                                              StoreLocation storeLocation, StoreName storeName,
                                              string thumbprint, string issuer, string subject, string friendlyName)
        {
            if (string.IsNullOrEmpty(thumbprint) && string.IsNullOrEmpty(issuer) && string.IsNullOrEmpty(subject) && string.IsNullOrEmpty(friendlyName))
            {
                Console.WriteLine("no certificate data provided (must use at least one of Thumbprint, Issuer, Subject or FriendlyName)");
                return;
            }

            var certificate = AsymHelper.SelectCertificate(false, storeLocation, storeName, thumbprint, subject, friendlyName, issuer);

            if (certificate == null)
            {
                Console.WriteLine("could not find a certificate that has a usable private key and matches the given data");
                return;
            }

            if (File.Exists(outputFile))
            {
                Console.WriteLine("file already exists: {0}. Maybe you intent to use Overwrite=\"true\"?", new FileInfo(outputFile).FullName);
                return;
            }

            var encrypted = certificate.GetRSAPublicKey().Encrypt(Encoding.UTF8.GetBytes(toEncrypt), RSAEncryptionPadding.Pkcs1);

            File.WriteAllBytes(outputFile, encrypted);

            Console.WriteLine("\nSuccess\n");
        }
Ejemplo n.º 2
0
        private bool validate()
        {
            if (string.IsNullOrEmpty(Thumbprint) && string.IsNullOrEmpty(Issuer) && string.IsNullOrEmpty(Subject) && string.IsNullOrEmpty(FriendlyName))
            {
                Log.LogError("no certificate data provided (must use at least one of Thumbprint, Issuer, Subject or FriendlyName)");
                return(false);
            }

            certificate = AsymHelper.SelectCertificate(false, storeLocation, storeName, Thumbprint, Subject, FriendlyName, Issuer);
            if (certificate == null)
            {
                Log.LogError("could not find a certificate that has a usable private key and matches the given data");
                return(false);
            }

            if (File.Exists(OutputFile))
            {
                if (Overwrite)
                {
                    Log.LogWarning("overwriting file: {0}", new FileInfo(OutputFile).FullName);
                }
                else
                {
                    Log.LogError("file already exists: {0}. Maybe you intent to use Overwrite=\"true\"?", new FileInfo(OutputFile).FullName);
                    return(false);
                }
            }

            return(true);
        }
Ejemplo n.º 3
0
        private bool validate()
        {
            if (!File.Exists(InputFile))
            {
                Log.LogError("file does not exist: {0}.", new FileInfo(InputFile).FullName);
                return(false);
            }

            if (string.IsNullOrEmpty(Thumbprint) && string.IsNullOrEmpty(Issuer) && string.IsNullOrEmpty(Subject) && string.IsNullOrEmpty(FriendlyName))
            {
                Log.LogError("no certificate data provided (must use at least one of Thumbprint, Issuer, Subject or FriendlyName)");
                return(false);
            }

            certificate = AsymHelper.SelectCertificate(true, storeLocation, storeName, Thumbprint, Subject, FriendlyName, Issuer);
            if (certificate == null)
            {
                Log.LogError("could not find a certificate that has a usable private key and matches the given data");
                return(false);
            }

            return(true);
        }