예제 #1
0
        public Boolean Run()
        {
            Console.WriteLine($"RSA performance test :");
            Console.WriteLine($"1. 建立金鑰: AB雙方各自產生一組成對的(公鑰及私鑰).");
            string sContainer_A = "A";
            string sContainer_B = "B";

            if (!ZRSA.CreateContainer(sContainer_A))
            {
                Console.WriteLine(ZRSA.msError);
                return(false);
            }
            if (!ZRSA.CreateContainer(sContainer_B))
            {
                Console.WriteLine(ZRSA.msError);
                return(false);
            }
            Console.WriteLine($"{sContainer_A} = {ZRSA.ExistingContainer(sContainer_A)}, {ZRSA.GetContainerFileFullName(sContainer_A)}");
            Console.WriteLine($"{sContainer_B} = {ZRSA.ExistingContainer(sContainer_B)}, {ZRSA.GetContainerFileFullName(sContainer_B)}");
            Console.WriteLine();

            Console.WriteLine($"2. 取得公鑰: A取得(B的公鑰), 並確保(B的公鑰)是來自於B.");
            string sPublicKeyXML_B = ZRSA.GetPublicKey(sContainer_B);

            if (string.IsNullOrEmpty(sPublicKeyXML_B))
            {
                Console.WriteLine(ZRSA.msError);
                return(false);
            }
            Console.WriteLine($"(B的公鑰) = {sPublicKeyXML_B.Length}, {sPublicKeyXML_B}");
            Console.WriteLine();

            Console.WriteLine($"3. 加密原文: A利用(B的公鑰), 加密(原文)後, 產生(加密訊息).");
            string sPlainText;

            byte[] baPlainText;
            byte[] baEncrypt;
            string sPrivateKeyXML_A = ZRSA.GetPrivateKey(sContainer_A);

            byte[] baSignatureUTF8;
            string sPublicKeyXML_A  = ZRSA.GetPublicKey(sContainer_A);
            string sPrivateKeyXML_B = ZRSA.GetPrivateKey(sContainer_B);

            byte[] baDecrypt;

            int       iTestCount      = 300;
            Stopwatch swEncryptRSA    = new Stopwatch();
            Stopwatch swDecryptRSA    = new Stopwatch();
            Stopwatch swSignRSA       = new Stopwatch();
            Stopwatch swVerifySignRSA = new Stopwatch();

            for (int i = 0; i < iTestCount; i++)
            {
                swEncryptRSA.Start();
                sPlainText  = new string('H', i + 1);
                baPlainText = ZByte.GetBytesUTF8(sPlainText);

                // 原文長度最多 117 bytes.
                baEncrypt = ZRSA.Encrypt(baPlainText, sPublicKeyXML_B);
                if (baEncrypt == null)
                {
                    Console.WriteLine("Encrypt " + ZRSA.msError);
                    break;
                }
                swEncryptRSA.Stop();

                swSignRSA.Start();
                baSignatureUTF8 = ZRSA.SignDataSHA1(baPlainText, sPrivateKeyXML_A);
                if (baSignatureUTF8 == null)
                {
                    Console.WriteLine("Sign " + ZRSA.msError);
                    break;
                }
                swSignRSA.Stop();

                swDecryptRSA.Start();
                baDecrypt = ZRSA.Decrypt(baEncrypt, sPrivateKeyXML_B);
                if (baDecrypt == null)
                {
                    Console.WriteLine("Decrypt " + ZRSA.msError);
                    break;
                }
                swDecryptRSA.Stop();

                swVerifySignRSA.Start();
                //Console.WriteLine($"驗證簽章 = {ZRSA.VerifyDataSHA1(baDecrypt, sPublicKeyXML_A, baSignatureUTF8)}.");
                if (!ZRSA.VerifyDataSHA1(baDecrypt, sPublicKeyXML_A, baSignatureUTF8))
                {
                    Console.WriteLine("Verify " + ZRSA.msError);
                    return(false);
                }
                swVerifySignRSA.Stop();

                Console.WriteLine("{0}, Encrypt={1}, Sign={2}, Decrypt={3}, Verify={4}, EncryptLen={5}, DecryptLen={6}.",
                                  i + 1,
                                  swEncryptRSA.ElapsedMilliseconds,
                                  swDecryptRSA.ElapsedMilliseconds,
                                  swSignRSA.ElapsedMilliseconds,
                                  swVerifySignRSA.ElapsedMilliseconds,
                                  baEncrypt.Length,
                                  baDecrypt.Length);

                /*
                 *
                 * 1, Encrypt=1, Sign=6, Decrypt=9, Verify=0, EncryptLen=128, DecryptLen=1.
                 * 2, Encrypt=1, Sign=11, Decrypt=14, Verify=0, EncryptLen=128, DecryptLen=2.
                 * 3, Encrypt=1, Sign=15, Decrypt=17, Verify=1, EncryptLen=128, DecryptLen=3.
                 * 4, Encrypt=1, Sign=22, Decrypt=22, Verify=1, EncryptLen=128, DecryptLen=4.
                 * 5, Encrypt=1, Sign=28, Decrypt=25, Verify=1, EncryptLen=128, DecryptLen=5.
                 * 6, Encrypt=2, Sign=31, Decrypt=30, Verify=1, EncryptLen=128, DecryptLen=6.
                 * 7, Encrypt=2, Sign=36, Decrypt=34, Verify=2, EncryptLen=128, DecryptLen=7.
                 * 8, Encrypt=2, Sign=41, Decrypt=41, Verify=2, EncryptLen=128, DecryptLen=8.
                 * 9, Encrypt=2, Sign=46, Decrypt=46, Verify=2, EncryptLen=128, DecryptLen=9.
                 * 10, Encrypt=3, Sign=51, Decrypt=54, Verify=2, EncryptLen=128, DecryptLen=10.
                 * 20, Encrypt=6, Sign=106, Decrypt=115, Verify=5, EncryptLen=128, DecryptLen=20.
                 * 30, Encrypt=10, Sign=155, Decrypt=180, Verify=8, EncryptLen=128, DecryptLen=30.
                 * 40, Encrypt=29, Sign=206, Decrypt=260, Verify=11, EncryptLen=128, DecryptLen=40.
                 * 50, Encrypt=33, Sign=249, Decrypt=323, Verify=15, EncryptLen=128, DecryptLen=50.
                 * 60, Encrypt=37, Sign=298, Decrypt=394, Verify=18, EncryptLen=128, DecryptLen=60.
                 * 70, Encrypt=43, Sign=353, Decrypt=474, Verify=22, EncryptLen=128, DecryptLen=70.
                 * 80, Encrypt=47, Sign=411, Decrypt=557, Verify=26, EncryptLen=128, DecryptLen=80.
                 * 90, Encrypt=54, Sign=456, Decrypt=619, Verify=28, EncryptLen=128, DecryptLen=90.
                 * 100, Encrypt=57, Sign=495, Decrypt=669, Verify=30, EncryptLen=128, DecryptLen=100.
                 * 117, Encrypt=62, Sign=571, Decrypt=762, Verify=34, EncryptLen=128, DecryptLen=117.
                 * Encrypt 長度錯誤。
                 *
                 */
            }

            return(true);
        }
예제 #2
0
        public Boolean Run()
        {
            Console.WriteLine($"RSA 測試:");
            Console.WriteLine($"1. 建立金鑰: AB雙方各自產生一組成對的(公鑰及私鑰).");
            string sContainer_A = "A";
            string sContainer_B = "B";

            if (!ZRSA.CreateContainer(sContainer_A))
            {
                Console.WriteLine(ZRSA.msError);
                return(false);
            }
            if (!ZRSA.CreateContainer(sContainer_B))
            {
                Console.WriteLine(ZRSA.msError);
                return(false);
            }
            Console.WriteLine($"{sContainer_A} = {ZRSA.ExistingContainer(sContainer_A)}, {ZRSA.GetContainerFileFullName(sContainer_A)}");
            Console.WriteLine($"{sContainer_B} = {ZRSA.ExistingContainer(sContainer_B)}, {ZRSA.GetContainerFileFullName(sContainer_B)}");
            Console.WriteLine();

            Console.WriteLine($"2. 取得公鑰: A取得(B的公鑰), 並確保(B的公鑰)是來自於B.");
            string sPublicKeyXML_B = ZRSA.GetPublicKey(sContainer_B);

            if (string.IsNullOrEmpty(sPublicKeyXML_B))
            {
                Console.WriteLine(ZRSA.msError);
                return(false);
            }
            Console.WriteLine($"(B的公鑰) = {sPublicKeyXML_B.Length}, {sPublicKeyXML_B}");
            Console.WriteLine();

            Console.WriteLine($"3. 加密原文: A利用(B的公鑰), 加密(原文)後, 產生(加密訊息).");
            string sPlainTextSalt = DateTime.Now.ToString("yyyyMMddHHmmssfff");
            string sPlainText     = "123, 到台灣, 台灣有個阿里山. ~!@#$%^&*()<>{}[]:;\"'*%!#\\/ABCD." + sPlainTextSalt;

            byte[] baPlainText = ZByte.GetBytesUTF8(sPlainText);
            byte[] baEncrypt   = ZRSA.Encrypt(baPlainText, sPublicKeyXML_B);
            if (baEncrypt == null)
            {
                Console.WriteLine(ZRSA.msError);
                return(false);
            }
            Console.WriteLine($"原文: {baPlainText.Length}, {sPlainText}");
            Console.WriteLine($"密文: {baEncrypt.Length}, {baEncrypt.ZGetStringHex()}");
            Console.WriteLine();

            Console.WriteLine($"4. 製作簽章: A利用(A的私鑰), 簽章(原文)後, 產生(簽章訊息).");
            string sPrivateKeyXML_A = ZRSA.GetPrivateKey(sContainer_A);

            byte[] baSignatureUTF8 = ZRSA.SignDataSHA1(baPlainText, sPrivateKeyXML_A);
            if (baSignatureUTF8 == null)
            {
                Console.WriteLine(ZRSA.msError);
                return(false);
            }
            Console.WriteLine($"簽章: {baSignatureUTF8.Length}, {baSignatureUTF8.ZGetStringHex()}");
            Console.WriteLine();

            Console.WriteLine($"5. 傳送訊息: B取得(A的公鑰), (加密訊息), (簽章訊息).");
            string sPublicKeyXML_A = ZRSA.GetPublicKey(sContainer_A);

            if (string.IsNullOrEmpty(sPublicKeyXML_A))
            {
                Console.WriteLine(ZRSA.msError);
                return(false);
            }
            Console.WriteLine($"(A的公鑰) = {sPublicKeyXML_A.Length}, {sPublicKeyXML_A}");
            Console.WriteLine();

            Console.WriteLine($"6. 解密訊息: B利用(B的私鑰), 解密(加密訊息), 取得(原文).");
            string sPrivateKeyXML_B = ZRSA.GetPrivateKey(sContainer_B);

            if (string.IsNullOrEmpty(sPrivateKeyXML_B))
            {
                Console.WriteLine(ZRSA.msError);
                return(false);
            }
            Console.WriteLine($"(B的私鑰) = {sPrivateKeyXML_B.Length}, {sPrivateKeyXML_B}");

            byte[] baDecrypt = ZRSA.Decrypt(baEncrypt, sPrivateKeyXML_B);
            if (baDecrypt == null)
            {
                Console.WriteLine(ZRSA.msError);
                return(false);
            }
            string sDecrypt = baDecrypt.ZGetStringUTF8();

            Console.WriteLine($"解密訊息 = {baDecrypt.Length}, {sDecrypt}");
            Console.WriteLine($"解密結果 = {sDecrypt == sPlainText}.");
            Console.WriteLine();

            Console.WriteLine($"7. 驗證簽章: B取得(A的公鑰), 驗證(簽章訊息), 確定(加密訊息)沒有被竄改..");
            Console.WriteLine($"驗證簽章 = {ZRSA.VerifyDataSHA1(baDecrypt, sPublicKeyXML_A, baSignatureUTF8)}.");
            Console.WriteLine();

            Console.WriteLine($"刪除金鑰: 刪除存放在本機的金鑰.");
            if (ZRSA.DeleteContainer(sContainer_A))
            {
                Console.WriteLine("刪除A");
            }
            if (ZRSA.DeleteContainer(sContainer_B))
            {
                Console.WriteLine("刪除B");
            }

            Console.WriteLine("實際刪除金鑰是由 garbage collection 執行, 不會立刻刪除.");
            Console.WriteLine($"{sContainer_A} = {ZRSA.ExistingContainer(sContainer_A)}, {ZRSA.GetContainerFileFullName(sContainer_A)}");
            Console.WriteLine($"{sContainer_B} = {ZRSA.ExistingContainer(sContainer_B)}, {ZRSA.GetContainerFileFullName(sContainer_B)}");
            Console.WriteLine();
            return(true);
        }