Esempio n. 1
0
        public void EncryptTest()
        {
            var str          = "HelloWorld!";
            var rsaEncrypter = new RsaEncryptor(PublicKey, PrivateKey);
            var encrypted    = rsaEncrypter.Encrypt(str);

            Assert.Equal(str, rsaEncrypter.Decrypt(encrypted));

            var rsa2Encrypter = new RsaEncryptor(PublicKey, PrivateKey);

            rsa2Encrypter.SetHashAlg("RSA2");
            var encrypted2 = rsa2Encrypter.Encrypt(str);

            Assert.Equal(str, rsa2Encrypter.Decrypt(encrypted2));


            var rsaEncrypter2 = new RsaEncryptor();

            rsaEncrypter2.SetHashAlg("RSA");
            rsaEncrypter2.LoadPublicKey(PublicKey);
            rsaEncrypter2.LoadPrivateKey(PrivateKey);
            var encrypted2_2 = rsaEncrypter2.Encrypt(str);

            Assert.Equal(str, rsaEncrypter2.Decrypt(encrypted2_2));
        }
Esempio n. 2
0
        public void GenerateKeyPair_Test()
        {
            var keyPair = RsaUtil.GenerateKeyPair(RSAKeyFormat.PKCS1, 512);

            Assert.NotEmpty(keyPair.PublicKey);
            var rsaEncryptor = new RsaEncryptor(keyPair.PublicKey, keyPair.PrivateKey);
            var d1           = rsaEncryptor.Encrypt("hello");
            var d2           = rsaEncryptor.Decrypt(d1);

            Assert.Equal("hello", d2);

            var sign = rsaEncryptor.SignData("string1");

            Assert.True(rsaEncryptor.VerifyData("string1", sign));


            var rsaParams1 = RsaUtil.ReadPrivateKeyInfo(keyPair.PrivateKey);

            var rsaParams2     = RsaUtil.ReadPrivateKeyInfo(keyPair.PrivateKey);
            var rsaPrivateKey2 = RsaUtil.ExportPrivateKeyPkcs8(rsaParams2);
            var rsaEncryptor2  = new RsaEncryptor();

            rsaEncryptor2.LoadPrivateKey(rsaPrivateKey2);

            var d3 = rsaEncryptor2.Decrypt(d1);

            Assert.Equal("hello", d3);
        }