Exemplo n.º 1
0
        private UserKeys GenerateNewKeyPair()
        {
            var rsa = new Chilkat.Rsa();

            rsa.GenerateKey(RsaKeySize);

            var publicKey  = rsa.ExportPublicKey();
            var privateKey = rsa.ExportPrivateKey();

            var symetricKey = EncryptionManager.GenerateSymmetricKey();

            var rsaEncryptor = new Chilkat.Rsa {
                EncodingMode = EncodingMode
            };

            rsaEncryptor.ImportPrivateKey(privateKey);

            // Encrypted with private so that public key can get access to symmetric key
            var encryptedSymetricKey = rsaEncryptor.EncryptStringENC(symetricKey, true);

            // Encrypted with application key so that the application can manage access to private keys
            var encryptedPrivateKey = EncryptionManager.Encrypt(privateKey, string.Empty);

            return(UserKeys.Create(encryptedPrivateKey, publicKey, encryptedSymetricKey));
        }
Exemplo n.º 2
0
        public SigningOutput SignContent(string serializedRequest)
        {
            var hashManager = new HashManager();
            var privateKey  = KeyStoreAdapter.GetPrivateKeyForUser(SignatoryReference);

            var envolope = SigningEnvelope <string> .Create(serializedRequest, SignatoryReference, SignatoryEmail, SignatoryIpAddress);

            var hashSigningContent = hashManager.HashContent(envolope.Body);

            var rsaEncryptor = new Chilkat.Rsa {
                EncodingMode = EncodingMode
            };

            rsaEncryptor.ImportPrivateKey(privateKey);

            var encryptedSignContentHash = rsaEncryptor.EncryptStringENC(hashSigningContent, true);

            envolope.AddEncryptedHashForBody(encryptedSignContentHash);

            var symmetricKey = KeyStoreAdapter.GetSymmetricKeyForUser(SignatoryReference);

            var serializedSignedContent = JsonConvert.SerializeObject(envolope);
            var encryptedSignedString   = Encrypt(serializedSignedContent, symmetricKey);

            return(SigningOutput.Create(encryptedSignedString, SignatoryReference));
        }
Exemplo n.º 3
0
 /// <summary>
 /// Encrypt using Given Public Key
 /// </summary>
 /// <param name="StringToEncrypt">w to Encrypt</param>
 /// <param name="PublicKey"></param>
 /// <returns></returns>
 public static string Encrypt(string StrToEncrypt, string PublicKey, string EncodingMode)
 {
     try
     {
         /// First we get the RSA Public Key
         string RSAPublicKey = PublicKey;
         /// rsa object
         Chilkat.Rsa rsa = new Chilkat.Rsa();
         /// bool for success
         bool success;
         /// unlock component
         success = rsa.UnlockComponent("VIENTORSA_TbpfVVr01Or6");
         /// rsa encoding mode as base64, hex
         rsa.EncodingMode = EncodingMode;
         /// import the public key to the API
         rsa.ImportPublicKey(RSAPublicKey);
         /// decrypted string
         string encryptedStr;
         ///we now decript the string
         encryptedStr = rsa.EncryptStringENC(StrToEncrypt, false);
         /// return the string
         return encryptedStr;
     }
     catch
     {
         return null;
     }
     finally
     {
         GC.Collect();
     }
 }
Exemplo n.º 4
0
        public void EncryptApplicationKeyFromCryptoServiceCert()
        {
            RegisterChilKat();
            X509Certificate2 theCert = null;

            var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);

            store.Open(OpenFlags.ReadOnly);

            foreach (var certificate in store.Certificates)
            {
                //TODO's
                Console.WriteLine(certificate.FriendlyName);

                if (certificate.FriendlyName == "CryptoServiceCert")
                {
                    theCert = certificate;
                }
            }

            var privateKey = theCert?.PrivateKey?.ToXmlString(true);
            var publicKey  = theCert?.PrivateKey?.ToXmlString(true);

            var sampleText = "+oPDU29Bv2hfUZ8fZozMGAVYUZjp1wdwDs42JY213tA=";

            var rsaEncryptor = new Chilkat.Rsa {
                EncodingMode = "hex"
            };

            rsaEncryptor.ImportPublicKey(publicKey);
            var encryptedText = rsaEncryptor.EncryptStringENC(sampleText, false);

            Console.WriteLine($"Encrypted Value: [{encryptedText}]");

            var rsaDecryptor = new Chilkat.Rsa {
                EncodingMode = "hex"
            };

            rsaDecryptor.ImportPrivateKey(privateKey);

            var decryptedText = rsaDecryptor.DecryptStringENC(encryptedText, true);
        }
Exemplo n.º 5
0
        public static void performRSA(string text)
        {
            Chilkat.Rsa rsa = new Chilkat.Rsa();

            bool success = rsa.UnlockComponent("Anything for 30-day trial");

            if (success != true)
            {
                Console.WriteLine("RSA component unlock failed");
                return;
            }

//  This example also generates the public and private
//  keys to be used in the RSA encryption.
//  Normally, you would generate a key pair once,
//  and distribute the public key to your partner.
//  Anything encrypted with the public key can be
//  decrypted with the private key.  The reverse is
//  also true: anything encrypted using the private
//  key can be decrypted using the public key.

//  Generate a 1024-bit key.  Chilkat RSA supports
//  key sizes ranging from 512 bits to 4096 bits.
            success = rsa.GenerateKey(1024);
            if (success != true)
            {
                Console.WriteLine(rsa.LastErrorText);
                return;
            }

//  Keys are exported in XML format:
            string publicKey  = rsa.ExportPublicKey();
            string privateKey = rsa.ExportPrivateKey();

            string plainText = "Encrypting and decrypting should be easy!";

            plainText = text;
//  Start with a new RSA object to demonstrate that all we
//  need are the keys previously exported:
            Chilkat.Rsa rsaEncryptor = new Chilkat.Rsa();

//  Encrypted output is always binary.  In this case, we want
//  to encode the encrypted bytes in a printable string.
//  Our choices are "hex", "base64", "url", "quoted-printable".
            rsaEncryptor.EncodingMode = "hex";

//  We'll encrypt with the public key and decrypt with the private
//  key.  It's also possible to do the reverse.
            success = rsaEncryptor.ImportPublicKey(publicKey);

            bool   usePrivateKey = false;
            string encryptedStr  = rsaEncryptor.EncryptStringENC(plainText, usePrivateKey);

//Console.WriteLine(encryptedStr);

//  Now decrypt:
            Chilkat.Rsa rsaDecryptor = new Chilkat.Rsa();

            rsaDecryptor.EncodingMode = "hex";
            success = rsaDecryptor.ImportPrivateKey(privateKey);

            usePrivateKey = true;
            string decryptedStr = rsaDecryptor.DecryptStringENC(encryptedStr, usePrivateKey);

//Console.WriteLine(decryptedStr);
        }