Example #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();
        }
Example #2
0
 public static string SHA512(string data)
 {
     byte[] hash;
     using (SHA512 shaM = new SHA512Managed())
     {
         hash = shaM.ComputeHash(data.GetBytes());
     }
     return(AESProvider.HexString(hash));
 }
Example #3
0
 /// <summary>
 /// Create an SHA512 hash of a file.
 /// </summary>
 /// <param name="fileName">The full path to a file to get the hash.</param>
 /// <returns>The SHA512 Hash.</returns>
 public static string SHA512HashFile(string fileName)
 {
     if (!System.IO.File.Exists(fileName))
     {
         //Utilities.OnFileError(Utilities.GetCurrentMethod(), fileName);
         throw new CryptographicException("File does not exist.");
     }
     return(AESProvider.CalculateSHA512HashFile(fileName));
 }
Example #4
0
 public static string GenerateRandomString(int length)
 {
     return(AESProvider.HexString(AESProvider.GenerateRandomBytes(length)));
 }
Example #5
0
 /// <summary>
 /// Create a SHA2-512 hash of a text input.
 /// This 128 character hash is recommended for the most secure password encryption.
 /// </summary>
 /// <param name="password">A text to create a hash (often a password).</param>
 /// <returns>The 128 character hex SHA512 Hash.</returns>
 public static string SHA512Hash(string password)
 {
     return(AESProvider.CalculateSHA512Hash(password));
 }
Example #6
0
 /// <summary>
 /// Create an MD5 hash of a text input (http://wikipedia.org/wiki/MD5).
 /// This 32 character hash is recommended where a general or shorter hash is required (password or data integrity).
 /// </summary>
 /// <param name="text">A text or password to create a hash.</param>
 /// <returns>The 32 character hex MD5 Hash.</returns>
 public static string MD5Hash(string text)
 {
     return(AESProvider.CalculateMD5Hash(text));
 }
Example #7
0
 /// <summary>
 /// Decrypt an AES encrypted cipher (previously encrypted) using a password key.
 /// </summary>
 /// <param name="cipher">The encrypted text (cipher).</param>
 /// <param name="password">The password key for the encryption.</param>
 /// <returns>The original unencrypted text or "" if password and cipher don't match.</returns>
 public static string Decrypt(string cipher, string password)
 {
     return(AESProvider.DecryptString(cipher, password));
 }
Example #8
0
 /// <summary>
 /// Encrypt some text using AES encryption and a password key.
 /// </summary>
 /// <param name="plaintext">The text to encrypt.</param>
 /// <param name="key">The password key for the encryption.</param>
 /// <returns>The encrypted text (cipher).</returns>
 public static string Encrypt(string plaintext, string key)
 {
     return(AESProvider.EncryptString(plaintext, key));
 }
Example #9
0
 static PowerAES()
 {
     AESCryptoProvider = new AESProvider();
 }
Example #10
0
 static PowerAES()
 {
     AESCryptoProvider = new AESProvider();
 }