Exemplo n.º 1
0
        static void Main(string[] args)
        {
            Console.WriteLine("**** RSA公钥密钥对生成工具 ****");
            Console.WriteLine();

            KeyPair keyPair = RSACryptoTextProvider.CreateKeyPair();

            Console.WriteLine("公钥:{0}", keyPair.PublicKey);
            Console.WriteLine();
            Console.WriteLine("私钥:{0}", keyPair.PrivateKey);
            Console.WriteLine();

            Console.Write("请输入测试用字符串:");
            string sourceText = Console.ReadLine();
            string cipherText = RSACryptoTextProvider.Encrypt(keyPair.PublicKey, sourceText);

            Console.WriteLine("密文:{0}", cipherText);
            string decryptText = RSACryptoTextProvider.Decrypt(keyPair.PrivateKey, cipherText);

            Console.WriteLine("解密:{0} {1}", decryptText, decryptText == sourceText ? "ok" : "error");
            Console.WriteLine();

            Console.Write("请按回车键结束程序");
            Console.ReadLine();
            Console.WriteLine();
        }
Exemplo n.º 2
0
        Task <string> IOneOffKeyPairGrain.GetPublicKey()
        {
            KeyPair keyPair = RSACryptoTextProvider.CreateKeyPair();

            _keyPair = new CachedObject <KeyPair>(keyPair, DateTime.Now.AddSeconds(DiscardIntervalSeconds));
            return(Task.FromResult(keyPair.PublicKey));
        }
        Task <string> IOneOffKeyPairGrain.Decrypt(string name, string cipherText, bool fOAEP)
        {
            string result = null;

            if (_cache.TryGetValue(name, out CachedObject <KeyPair> cachedObject))
            {
                result = RSACryptoTextProvider.Decrypt(cachedObject.Value.PrivateKey, cipherText, fOAEP);
                _cache.Remove(name);
            }

            return(Task.FromResult(result));
        }
Exemplo n.º 4
0
        Task <string> IOneOffKeyPairGrain.Decrypt(string cipherText, bool fOAEP)
        {
            if (_keyPair == null)
            {
                throw new InvalidOperationException("需先获取公钥才能解密!");
            }
            if (_keyPair.IsInvalid)
            {
                throw new InvalidOperationException("需重新获取公钥才能解密!");
            }

            return(Task.FromResult(RSACryptoTextProvider.Decrypt(_keyPair.Value.PrivateKey, cipherText, fOAEP)));
        }