예제 #1
0
        public void RsaTest()
        {
            var text = "Hello NStandard.";
            var data = text.Bytes();

            var encryptRsa = new RsaProvider();

            encryptRsa.FromXmlString(@"<RSAKeyValue>
    <Modulus>wyL0Tl0oPhililRHagPFIlKjWotnDKeK84p9sMjCPUfQqTb9zTne/K7KdpNnHSE5zmjUNWPGFSFxkdec/XVrLz0ZD0euTJo8ZN7Wx1eVYGczh5seLIOazf6zMgEW5jqwVx69Zs0K39alJDPqOYF7Q/2bBD9/BWvhRymkfKlSI5Z9LNi0i5ZRzuXqz6RcvPR35O/FdzWypKQsuRU73szDEwStkwu1ZgsSdyV50RoMAWodnvPDhJnVNdLtpeE6d2EDXKSnmmh+64h4HAK753UlCgjPuJ/jBxSiveSO6yOp18Hs1/tLHfZblgz8BM3tzMPgrPU3xNE3CHXujYSVKi4zsQ==</Modulus>
    <Exponent>AQAB</Exponent>
    <P>6gZc6Zm7gt1nvEqwU5/HdDIZGx0hYwa5OIOL1J72km35JVwYrheC/40MtwLOBt6cnuVeVWy5Q/LaJOeRs3k3dZjo15CeUZ59jvg3JzkHdk2hgDN/Cw9JamM1fEanAdgVKzU70fvbZ5K+gqcNkbacV/hh7cyOwBZUpxIqld9UVe8=</P>
    <Q>1XXFALNiLtSE24/k7PQK6yriaf4T/obVZh8oJbB9z8Af+FdfT3NeNdwHrl6GcQHYVPHLNudD3XCLIzhWkysKaITKKx2JZgRFffb8wh5eyRvPWa0ymiZ1R6Ye2A36BeNXno1pT9f2OkbuUgkavZ3KcSlTvFXzHpYGFjEBmPwSsF8=</Q>
    <DP>3fg1Dnj7Ss/YAddR0a9+Ti7qczY5IaUR75GOApjYROE8bHwjCJVScjOF+NwXMJrbMTdbN2lNfC6PTGu8Xd++g7MKPtRz5fSPIRk7rt8/va06Xs/5UzMnrln1NDALXgtnYLk4SR46581fF6t9ilULi8ESmavpRjaoCmOHsunGI08=</DP>
    <DQ>k9850fpyka53OwK7u8pzpeXXY0W35CLTwiLjVPimrzyQ6SDdzdRF91mtmIWy7KYyjuXRuP8MbGKCgKuOjfTLCQy0YJndjOZ4nYJ0JqWTVA4H3j+1RkROCoxx4YoNIfcTw6qCweUBle19OydDdwfQLgRLFbUU7qNPJCBe0vQMnlE=</DQ>
    <InverseQ>DE+NWEF21QkpVCJRPbjq01Vy7gHfy0WnpMDW+NEVBEJCbBfza+h6/FsO+c5RtVT3QN0stP+EfhDzeZSqNG/0Pf5GKhDKF7YsxFB8yHs3kUf0fN8PuoIFFqPwRXB4PytmvKg9h5e/aSbA7P3W2iDt5gH+IVUODF5cKfPvilk7izc=</InverseQ>
    <D>hawGK0BJdvAvRikhmo/mlPKDEF16RALpfpeaLmX4GT5+w8v15IYGKJYb/0pOUngWPz00UTZ91K/KOpu24TF0MTHrXro9vh/Ry0+TVY67twQ7GmO6McgdXYtieZihdSky7xsRp8BB+L/y9G/TvXzjUdoCPNC6VJ5n/fWaxgK/T7xujQVKWa0MEY0BtPma0tqWb1t0UxGseuG7Kb2JyYZDKc3sCr4ILkvpL9+nXkG8jS8kZcYyNEzEKhwOTU4SyMcHHq0tyuE9Iq6KEnHDcAekaXRvZnG/Sw1SDH7Yg44NOtdfZxhUxIOP8Gj1yFiDy/QJUsxAW7V60+2UwFb6DnEGuQ==</D>
</RSAKeyValue>");
            var cipher        = encryptRsa.Encrypt(data);
            var signature     = encryptRsa.SignData(data, HashAlgorithmName.SHA1);
            var hash          = Sha1Hasher.Hash(data);
            var hashSignature = encryptRsa.SignHash(hash, HashAlgorithmName.SHA1);

            var decryptRsa = new RsaProvider();

            decryptRsa.FromPemString(@"-----BEGIN PRIVATE KEY-----
MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDDIvROXSg+GKWK
VEdqA8UiUqNai2cMp4rzin2wyMI9R9CpNv3NOd78rsp2k2cdITnOaNQ1Y8YVIXGR
15z9dWsvPRkPR65Mmjxk3tbHV5VgZzOHmx4sg5rN/rMyARbmOrBXHr1mzQrf1qUk
M+o5gXtD/ZsEP38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GI08CgYEA
k9850fpyka53OwK7u8pzpeXXY0W35CLTwiLjVPimrzyQ6SDdzdRF91mtmIWy7KYy
juXRuP8MbGKCgKuOjfTLCQy0YJndjOZ4nYJ0JqWTVA4H3j+1RkROCoxx4YoNIfcT
w6qCweUBle19OydDdwfQLgRLFbUU7qNPJCBe0vQMnlECgYAMT41YQXbVCSlUIlE9
uOrTVXLuAd/LRaekwNb40RUEQkJsF/Nr6Hr8Ww75zlG1VPdA3Sy0/4R+EPN5lKo0
b/Q9/kYqEMoXtizEUHzIezeRR/R83w+6ggUWo/BFcHg/K2a8qD2Hl79pJsDs/dba
IO3mAf4hVQ4MXlwp8++KWTuLNw==
-----END PRIVATE KEY-----");
            var decipher = decryptRsa.Decrypt(cipher).String();

            Assert.Equal(text, decipher);
            Assert.True(encryptRsa.VerifyData(data, signature, HashAlgorithmName.SHA1));
            Assert.True(encryptRsa.VerifyHash(hash, hashSignature, HashAlgorithmName.SHA1));
            Assert.True(encryptRsa.ToXmlString(true) == decryptRsa.ToXmlString(true));
            Assert.True(encryptRsa.ToPemString(true) == decryptRsa.ToPemString(true));
        }
예제 #2
0
        private static void Rsa()
        {
            var text = "Hello World!";

            // 加密解密
            var provider = new RsaProvider(RsaType.RSA2, Encoding.UTF8);
            var enc      = provider.Encrypt(text);
            var dec      = provider.Decrypt(enc);

            Console.WriteLine($"Enc: {enc}");
            Console.WriteLine($"Dec: {dec}");

            // 签名验证
            provider = new RsaProvider(RsaType.RSA2, Encoding.UTF8, Const.RSAPrivateKey, Const.RSAPublicKey);
            var sig = provider.Sign(text);
            var ver = provider.Verify(text, sig);

            Console.WriteLine(sig);
            Console.WriteLine(ver);

            //var prik = Const.RSAPrivateKey;
            //var pubk = Const.RSAPublicKey;
            //var sign = RsaVerify.Sign(text, prik);
            //var verify = RsaVerify.SignCheck(text, sign, pubk);
            //Console.WriteLine(sign);
            //Console.WriteLine(verify);
        }
예제 #3
0
        /// <summary>
        /// RSA2048加密
        /// </summary>
        private static void RSA2048Encrypt()
        {
            var rsaHelper = new RsaProvider(RsaType.RSA2, Encoding.UTF8);

            ConsoleHelper.LoopProcessInputUntil(input =>
            {
                var enc = rsaHelper.Encrypt(input);
                Console.WriteLine($"  Input: {input}");
                Console.WriteLine($"Encrypt: {enc}");
            }, "$End");
        }
예제 #4
0
 public string Encrypt(string input)
 {
     return(_rsaHelper.Encrypt(input));
 }