Ejemplo n.º 1
0
        public static void Main(string[] args)
        {
            const int rsaKeySize = 728; //Smaller key sizes are easier to generate while testing
            var prsa = new PowerRSA(rsaKeySize, PowerRSA.PHashAlgorithm.SHA256);
            const string p = "this is n";
            var c = prsa.EncryptStringWithPublicKey(p);
            Console.WriteLine(c);
            var d = prsa.DecryptStringWithPrivateKey(c);
            Console.WriteLine(d);
            var x = prsa.PublicKey;
            Console.WriteLine("RSAProvider Data: " + prsa.PrivateKey);
            Console.WriteLine("Exporting Private key to PKCS format:");
            var priPemKey = RSAExtensions.ConvertPrivateKeyToPKCS(prsa);
            Console.WriteLine(priPemKey);
            Console.Write("PKCS Signing...");
            const string signData = "Hello, World!";
            var signature = RSAExtensions.SignWithPKCSPrivateKey(signData, prsa);
            Console.WriteLine(signature);
            Console.Write("Verifying...");
            var verification = RSAExtensions.VerifyWithPKCSPublicKey(signData, signature, prsa);
            Console.WriteLine(verification);

            prsa.Dispose();

            var pub = new PowerRSA(x, rsaKeySize, PowerRSA.PHashAlgorithm.SHA256);
            var e = pub.EncryptStringWithPublicKey(p);
            var d2 = prsa.DecryptStringWithPrivateKey(e);
            Console.WriteLine(d2);
            pub.Dispose();
            Console.WriteLine(e);
            const string k = "1234";
            var a1 = PowerAES.Encrypt(p, k);
            Console.WriteLine(a1);
            var d1 = PowerAES.Decrypt(a1, k);
            Console.WriteLine(d1);
            Console.WriteLine(PowerAES.SHA512Hash(p));

            Console.WriteLine("Testing AES encryption on strings...");
            var plaintextString = "Hi i like pie";
            var password = "******";
            var encryptedString = PowerAES.Encrypt(plaintextString, password);
            var decryptedString = PowerAES.Decrypt(encryptedString, password);
            Debug.Assert(decryptedString == plaintextString);

            Console.WriteLine("Testing AES encryption directly on bytes...");
            var aesProvider = new AESProvider();
            var salt = aesProvider.GenerateRandomBytes(24);
            var key = aesProvider.DeriveKeyFromPassphrase("monkey", salt);
            var iv = aesProvider.GenerateRandomBytes(16); //128-bit IV
            var plaintextBytes = Encoding.UTF8.GetBytes("Hi I am a monkey");
            var encryptedBytes = aesProvider.EncryptBytes(plaintextBytes, key, iv);
            var decryptedBytes = aesProvider.DecryptBytes(iv, salt, encryptedBytes, key);
            Debug.Assert(decryptedBytes.SequenceEqual(plaintextBytes));
            Console.WriteLine("Hash Test");
            var hash = HashUtils.SHA512(k);
            Console.WriteLine(hash);
            Console.WriteLine("Demo completed");
            Console.ReadKey();
        }
Ejemplo n.º 2
0
 public static string SignWithPKCSPrivateKey(string plaintext, PowerRSA prsa)
 {
     var signer = SignerUtilities.GetSigner("SHA256withRSA");
     var rsa = RSA.Create();
     rsa.FromXmlString(prsa.PrivateKey);
     var bcKeyPair = DotNetUtilities.GetRsaKeyPair(rsa);
     signer.Init(true, bcKeyPair.Private);
     var byteplaintext = Encoding.UTF8.GetBytes(plaintext);
     signer.BlockUpdate(byteplaintext, 0, byteplaintext.Length);
     var signature = signer.GenerateSignature();
     /* Base 64 encode the sig so its 8-bit clean */
     var signedString = Convert.ToBase64String(signature);
     return signedString;
 }
Ejemplo n.º 3
0
        public static string SignWithPKCSPrivateKey(string plaintext, PowerRSA prsa)
        {
            var signer = SignerUtilities.GetSigner("SHA256withRSA");
            var rsa    = RSA.Create();

            rsa.FromXmlString(prsa.PrivateKey);
            var bcKeyPair = DotNetUtilities.GetRsaKeyPair(rsa);

            signer.Init(true, bcKeyPair.Private);
            var byteplaintext = Encoding.UTF8.GetBytes(plaintext);

            signer.BlockUpdate(byteplaintext, 0, byteplaintext.Length);
            var signature = signer.GenerateSignature();
            /* Base 64 encode the sig so its 8-bit clean */
            var signedString = Convert.ToBase64String(signature);

            return(signedString);
        }
Ejemplo n.º 4
0
 public static bool VerifyWithPKCSPublicKey(string message, string expectedSignature, PowerRSA prsa)
 {
     var rsa = RSA.Create();
     rsa.FromXmlString(prsa.PublicKey);
     /* Make the key */
     var key = DotNetUtilities.GetRsaPublicKey(rsa);
     /* Init alg */
     var signer = SignerUtilities.GetSigner("SHA256withRSA");
     /* Populate key */
     signer.Init(false, key);
     /* Get the signature into bytes */
     var expectedSig = Convert.FromBase64String(expectedSignature);
     /* Get the bytes to be signed from the string */
     var msgBytes = Encoding.UTF8.GetBytes(message);
     /* Calculate the signature and see if it matches */
     signer.BlockUpdate(msgBytes, 0, msgBytes.Length);
     return signer.VerifySignature(expectedSig);
 }
Ejemplo n.º 5
0
 /// <summary>
 ///     Generates a PKCS private key from the PowerRSA object
 /// </summary>
 /// <param name="prsa"></param>
 /// <returns></returns>
 public static string ConvertPrivateKeyToPKCS(PowerRSA prsa)
 {
     var rsa = RSA.Create();
     rsa.FromXmlString(prsa.PrivateKey);
     var bcKeyPair = DotNetUtilities.GetRsaKeyPair(rsa);
     var pkcs8Gen = new Pkcs8Generator(bcKeyPair.Private);
     var pemObj = pkcs8Gen.Generate();
     string outputPem;
     using (var sw = new StringWriter())
     {
         var pkcs8Out = sw;
         var pemWriter = new PemWriter(pkcs8Out);
         pemWriter.WriteObject(pemObj);
         pkcs8Out.Close();
         outputPem = sw.ToString();
     }
     return outputPem;
 }
Ejemplo n.º 6
0
        /// <summary>
        ///     Generates a PKCS private key from the PowerRSA object
        /// </summary>
        /// <param name="prsa"></param>
        /// <returns></returns>
        public static string ConvertPrivateKeyToPKCS(PowerRSA prsa)
        {
            var rsa = RSA.Create();

            rsa.FromXmlString(prsa.PrivateKey);
            var    bcKeyPair = DotNetUtilities.GetRsaKeyPair(rsa);
            var    pkcs8Gen  = new Pkcs8Generator(bcKeyPair.Private);
            var    pemObj    = pkcs8Gen.Generate();
            string outputPem;

            using (var sw = new StringWriter())
            {
                var pkcs8Out  = sw;
                var pemWriter = new PemWriter(pkcs8Out);
                pemWriter.WriteObject(pemObj);
                pkcs8Out.Close();
                outputPem = sw.ToString();
            }
            return(outputPem);
        }
Ejemplo n.º 7
0
        public static bool VerifyWithPKCSPublicKey(string message, string expectedSignature, PowerRSA prsa)
        {
            var rsa = RSA.Create();

            rsa.FromXmlString(prsa.PublicKey);
            /* Make the key */
            var key = DotNetUtilities.GetRsaPublicKey(rsa);
            /* Init alg */
            var signer = SignerUtilities.GetSigner("SHA256withRSA");

            /* Populate key */
            signer.Init(false, key);
            /* Get the signature into bytes */
            var expectedSig = Convert.FromBase64String(expectedSignature);
            /* Get the bytes to be signed from the string */
            var msgBytes = Encoding.UTF8.GetBytes(message);

            /* Calculate the signature and see if it matches */
            signer.BlockUpdate(msgBytes, 0, msgBytes.Length);
            return(signer.VerifySignature(expectedSig));
        }
 public CryptUDPClient(string publicKey, int port)
 {
     _udpClient = new UdpClient(port);
     _rsaProvider = new PowerRSA(publicKey, 1024);
     DoNormalInit();
 }
 public CryptUDPClient(int port)
 {
     _udpClient = new UdpClient(port);
     _rsaProvider = new PowerRSA(1024);
     DoNormalInit();
 }
 public void ReinitializeRSA(string publicKey)
 {
     _rsaProvider = new PowerRSA(publicKey, 1024);
 }