public void TestEncodeAndDecode_byteArr_Key() { Console.Out.WriteLine("encode and decode"); RSAService instance = new RSAService(keysize); RSAPrivateKey privateKey = instance.ReadPrivateKey(privateKeyfile); RSAPublicKey publicKey = instance.ReadPublicKey(publicKeyfile); byte[] encData = instance.Encode(msgBytes, privateKey); byte[] decData = instance.Decode(encData, publicKey); Boolean bCompare = Compare.SafeEquals(msgBytes, decData); Assert.IsTrue(bCompare); encData = instance.Encode(msgBytes, publicKey); decData = instance.Decode(encData, privateKey); bCompare = Compare.SafeEquals(msgBytes, decData); Assert.IsTrue(bCompare); }
public static void Test() { Console.Out.WriteLine("Begin Example_010."); // Create some data to test with. Support.TestData(TESTDATA_FILE); /** * Create a public / private RSA key pair. */ RSAService rsa = new RSAService(); if (!rsa.AreKeysPresent(privateKeyfile, publicKeyfile)) { Console.Out.WriteLine("Begin Create RSA Keys."); using (FileStream os_private = new FileStream(privateKeyfile, FileMode.Create), os_public = new FileStream(publicKeyfile, FileMode.Create)) { rsa.GenerateKey(os_private, os_public); } Console.Out.WriteLine("End Create RSA Keys."); } /** * RSA keys are asynchronous; there is a public and private key. Each * key can only decrypt data encrypted with the other key. A client * process would not have both keys, this is only for demonstration * purposes. */ Console.Out.WriteLine("Begin Read RSA Keys."); RSAPrivateKey privateKey = null; RSAPublicKey publicKey = null; using (FileStream is_private = new FileStream(privateKeyfile, FileMode.Open), is_public = new FileStream(publicKeyfile, FileMode.Open)) { privateKey = rsa.ReadPrivateKey(is_private); publicKey = rsa.ReadPublicKey(is_public); } Console.Out.WriteLine("End Read RSA Keys."); /** * Use public key to encrypt a file stream directly to another file * stream. */ Console.Out.WriteLine("Begin Encrypt Data."); using (FileStream outstream = new FileStream(TESTDATA_ENC_FILE, FileMode.Create), instream = new FileStream(TESTDATA_FILE, FileMode.Open)) { rsa.Encode(instream, outstream, publicKey); } Console.Out.WriteLine("End Encrypt Data."); /** * Now decrypt the encrypted file using the private key. */ Console.Out.WriteLine("Begin Decrypt Data."); using (FileStream outstream = new FileStream(TESTDATA_DEC_FILE, FileMode.Create), instream = new FileStream(TESTDATA_ENC_FILE, FileMode.Open)) { rsa.Decode(instream, outstream, privateKey); } Console.Out.WriteLine("End Decrypt Data."); /** * Compare the original and decrypted files. */ string shaOriginal = DigestSHA.Sha256(new FileStream(TESTDATA_FILE, FileMode.Open)); string shaDecripted = DigestSHA.Sha256(new FileStream(TESTDATA_DEC_FILE, FileMode.Open)); if (Compare.SafeEquals(UTF8Encoding.UTF8.GetBytes(shaOriginal), UTF8Encoding.UTF8.GetBytes(shaDecripted))) { Console.Out.WriteLine("Encrypted and decrypted files are the same."); } else { Console.Out.WriteLine("Encrypted and decrypted files are NOT the same."); } Console.Out.WriteLine("End Example_010."); }
public void TestEncodeAndDecode_3args() { Console.Out.WriteLine("encode and decode stream"); RSAService instance = new RSAService(keysize); RSAPrivateKey privateKey = instance.ReadPrivateKey(privateKeyfile); RSAPublicKey publicKey = instance.ReadPublicKey(publicKeyfile); byte[] decData; using (MemoryStream outstream = new MemoryStream()) { using (MemoryStream instream = new MemoryStream(msgBytes)) { instance.Encode(instream, outstream, privateKey); byte[] encData = outstream.ToArray(); decData = instance.Decode(encData, publicKey); } } Boolean bCompare = Compare.SafeEquals(msgBytes, decData); Assert.IsTrue(bCompare); using (MemoryStream outstream = new MemoryStream()) { using (MemoryStream instream = new MemoryStream(msgBytes)) { instance.Encode(instream, outstream, publicKey); byte[] encData = outstream.ToArray(); decData = instance.Decode(encData, privateKey); } } bCompare = Compare.SafeEquals(msgBytes, decData); Assert.IsTrue(bCompare); }
public void TestReadPublicKey_InputStream() { Console.Out.WriteLine("readPublicKey"); using (FileStream instream = new FileStream(publicKeyfile, FileMode.Open)) { RSAService instance = new RSAService(keysize); RSAPublicKey result = instance.ReadPublicKey(instream); Assert.IsNotNull(result); } }
public void TestReadPublicKey_String() { Console.Out.WriteLine("readPublicKey"); RSAService instance = new RSAService(keysize); RSAPublicKey publicKey = instance.ReadPublicKey(publicKeyfile); Assert.IsNotNull(publicKey); }
public static void Test() { Console.Out.WriteLine("Begin Example_020."); // Create some data to test with. Support.TestData(TESTDATA_FILE); /** * Create a public / private RS key pair. */ RSAService rsa = new RSAService(); if (!rsa.AreKeysPresent(privateKeyfile, publicKeyfile)) { Console.Out.WriteLine("Begin Create RSA Keys."); rsa.GenerateKey(privateKeyfile, publicKeyfile); Console.Out.WriteLine("End Create RSA Keys."); } /** * RSA keys are asynchronous; there is a public and private key. Each * key can only decrypt data encrypted with the other key. A client * process would not have both keys, this is only for demonstration * purposes. */ Console.Out.WriteLine("Begin Read RSA Keys."); RSAPrivateKey privateKey = rsa.ReadPrivateKey(privateKeyfile); RSAPublicKey publicKey = rsa.ReadPublicKey(publicKeyfile); Console.Out.WriteLine("End Read RSA Keys."); /** * Read the test data into a byte array. Be sure to use UTF-8 when * converting between strings and byte arrays. */ Console.Out.WriteLine("Begin Read Data."); string testdata = File.ReadAllText(TESTDATA_FILE); byte[] testdata_bytes = UTF8Encoding.UTF8.GetBytes(testdata); Console.Out.WriteLine("End Read Data."); /** * Use public key to encrypt a byte array to another byte array. */ Console.Out.WriteLine("Begin Encrypt Data."); byte[] testdata_enc = rsa.Encode(testdata_bytes, publicKey); Console.Out.WriteLine("End Encrypt Data."); /** * Now decrypt the encrypted file using the private key. */ Console.Out.WriteLine("Begin Decrypt Data."); byte[] testdata_dec = rsa.Decode(testdata_enc, privateKey); Console.Out.WriteLine("End Decrypt Data."); /** * Compare the original and decrypted files. */ String shaOriginal = DigestSHA.Sha256(testdata_bytes); String shaDecripted = DigestSHA.Sha256(testdata_dec); if (Compare.SafeEquals(UTF8Encoding.UTF8.GetBytes(shaOriginal), UTF8Encoding.UTF8.GetBytes(shaDecripted))) { Console.Out.WriteLine("Encrypted and decrypted files are the same."); } else { Console.Out.WriteLine("Encrypted and decrypted files are NOT the same."); } Console.Out.WriteLine("End Example_020."); }