コード例 #1
0
        public static (string publicPem, string privatePem) RSAToPem(bool isPKCS8)
        {
            var rsaKey = RsaKey.CreateRsaKey();

            using (System.Security.Cryptography.RSA rsa = System.Security.Cryptography.RSA.Create())
            {
                rsa.FromJsonString(rsaKey.PrivateKey);

                var publicPem  = RsaProvider.ToPem(rsa, false, isPKCS8);
                var privatePem = RsaProvider.ToPem(rsa, true, isPKCS8);

                return(publicPem, privatePem);
            }
        }
        static void Main(string[] args)
        {
            Console.WriteLine("** RSA **");
            var rsaKey     = RsaKey.CreateRsaKey();
            var plaintext  = "Hello world 123456789/*-+!@#$%^&*()-=_+";
            var publicKey  = rsaKey.PublicKey;
            var privateKey = rsaKey.PrivateKey;
            //var exponent = rsaKey.Exponent;
            //var modulus = rsaKey.Modulus;



            var encrypted = RSAEncrypter.RSAEncrypt(publicKey, plaintext, RSAEncryptionPadding.OaepSHA512);
            var decrypted = RSADecrypter.RSADecrypt(privateKey, encrypted, RSAEncryptionPadding.OaepSHA512);


            Console.WriteLine("Encrypted: " + encrypted);
            Console.WriteLine("Decrypted: " + decrypted);
            //Console.WriteLine("publicKey: {0} privateKey: {1}", publicKey, privateKey);



            Console.ReadKey();
        }