示例#1
0
        private static void RunRsaWithRsaParameterKeyFromCSP()
        {
            // CSP = Configuration Service Provider or Windows Key Container
            // these can be stored as either:
            //  1) User-level key containers (C:\Users\<user_name>\AppData\Roaming\Microsoft\Crypto\RSA).
            //     These are only accessable by the user as they are stored in the users profile
            //  2) machine-level key container (C:\Users\All Users\Application Data\Microsoft\Crypto\RSA)
            //     These are stored on a global level that all users on the machine can access

            Console.WriteLine("Encryption Using RSA with Parameter key from CSP started");
            Console.WriteLine();

            CryptographyExample cryptographyExample = new CryptographyExample();

            const string originalMessage = "Some Text to Encrypt";

            Console.WriteLine(String.Format("Message before encryption: {0}", originalMessage));

            cryptographyExample.AssignNewRSAKeyAndStoreInCSP();

            byte[] encryptedMessage = cryptographyExample.EncryptDataUsingRSAStoredinCSP(Encoding.UTF8.GetBytes(originalMessage));
            Console.WriteLine(String.Format("Message after encryption: {0}", Encoding.UTF8.GetString(encryptedMessage)));

            byte[] decryptedMessage = cryptographyExample.DecryptDataUsingRSAStoredinCSP(encryptedMessage);
            Console.WriteLine(String.Format("Message after decryption: {0}", Encoding.UTF8.GetString(decryptedMessage), true));

            cryptographyExample.DeleteRSPkeyStoredInCSP();

            Console.WriteLine();
            Console.WriteLine("Encryption Using RSA with Parameter key from CSP ended");
        }