Example #1
0
        public Boolean Run()
        {
            Console.WriteLine($"SHA1 測試:");

            Console.WriteLine($"1. 取得byte[]雜湊值.");
            string sSalt      = DateTime.Now.ToString("yyyyMMddHHmmssfff");
            string sPlainText = "123, 到台灣, 台灣有個阿里山. ~!@#$%^&*()<>{}[]:;\"'*%!#\\/ABCD." + sSalt;

            byte[] baPlainText = ZByte.GetBytesUTF8(sPlainText);
            byte[] baHash      = ZSecurity.GetHashSHA1(baPlainText);
            if (baHash == null)
            {
                Console.WriteLine(ZSecurity.msError);
                return(false);
            }
            Console.WriteLine($"原文: {baPlainText.Length}, {sPlainText}");
            Console.WriteLine($"雜湊值: {baHash.Length}, {baHash.ZGetStringHex()}");
            Console.WriteLine($"驗證: {ZSecurity.VerifyHashSHA1(baPlainText, baHash)}");
            Console.WriteLine();

            Console.WriteLine($"2. 取得檔案雜湊值.");
            string sFile = "SHA1.txt";

            File.WriteAllText(sFile, sPlainText);
            baHash = ZSecurity.GetHashSHA1(sFile);
            if (baHash == null)
            {
                Console.WriteLine(ZSecurity.msError);
                return(false);
            }
            Console.WriteLine($"雜湊值: {baHash.Length}, {baHash.ZGetStringHex()}");
            Console.WriteLine($"驗證: {ZSecurity.VerifyHashSHA1(sFile, baHash)}");
            Console.WriteLine();
            return(true);
        }
        public Boolean Run()
        {
            Console.WriteLine($"AES performance test:");
            Console.WriteLine($"1. 加密.");

            string sPlainText;

            byte[] baPlainText;
            byte[] baEncrypt;
            byte[] baDecrypt;
            string sKey = "12345678901234567890123456789012";
            //string sKey = "1234567890123456";
            string sIV = "1234567890123456";

            byte[] baKey = ZByte.GetBytesUTF8(sKey);
            byte[] baIV  = ZByte.GetBytesUTF8(sIV);

            int       iTestCount = 3002;
            Stopwatch swEncrypt  = new Stopwatch();
            Stopwatch swDecrypt  = new Stopwatch();

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

                baEncrypt = ZSecurity.EncryptAES(baPlainText, baKey, baIV);
                if (baEncrypt == null)
                {
                    Console.WriteLine("Encrypt " + ZRSA.msError);
                    break;
                }
                swEncrypt.Stop();

                swDecrypt.Start();
                baDecrypt = ZSecurity.DecryptAES(baEncrypt, baKey, baIV);
                if (baDecrypt == null)
                {
                    Console.WriteLine("Decrypt " + ZRSA.msError);
                    break;
                }
                swDecrypt.Stop();

                if (baEncrypt.ZEquals(baDecrypt))
                {
                    Console.WriteLine("驗證錯誤");
                    return(false);
                }

                Console.WriteLine("{0}, Encrypt={1}, Decrypt={2}, EncryptLen={3}, DecryptLen={4}.",
                                  i + 1,
                                  swEncrypt.ElapsedMilliseconds,
                                  swDecrypt.ElapsedMilliseconds,
                                  baEncrypt.Length,
                                  baDecrypt.Length);

                /*
                 *
                 * 1, Encrypt=3, Decrypt=10, EncryptLen=16, DecryptLen=1.
                 * 101, Encrypt=5, Decrypt=15, EncryptLen=112, DecryptLen=101.
                 * 201, Encrypt=5, Decrypt=19, EncryptLen=208, DecryptLen=201.
                 * 301, Encrypt=6, Decrypt=22, EncryptLen=304, DecryptLen=301.
                 * 401, Encrypt=6, Decrypt=25, EncryptLen=416, DecryptLen=401.
                 * 501, Encrypt=7, Decrypt=29, EncryptLen=512, DecryptLen=501.
                 * 601, Encrypt=7, Decrypt=32, EncryptLen=608, DecryptLen=601.
                 * 701, Encrypt=8, Decrypt=34, EncryptLen=704, DecryptLen=701.
                 * 801, Encrypt=8, Decrypt=37, EncryptLen=816, DecryptLen=801.
                 * 901, Encrypt=9, Decrypt=39, EncryptLen=912, DecryptLen=901.
                 * 1001, Encrypt=9, Decrypt=42, EncryptLen=1008, DecryptLen=1001.
                 * 1101, Encrypt=9, Decrypt=43, EncryptLen=1104, DecryptLen=1101.
                 * 1201, Encrypt=10, Decrypt=45, EncryptLen=1216, DecryptLen=1201.
                 * 1301, Encrypt=10, Decrypt=48, EncryptLen=1312, DecryptLen=1301.
                 * 1401, Encrypt=12, Decrypt=50, EncryptLen=1408, DecryptLen=1401.
                 * 1501, Encrypt=12, Decrypt=52, EncryptLen=1504, DecryptLen=1501.
                 * 1601, Encrypt=12, Decrypt=55, EncryptLen=1616, DecryptLen=1601.
                 * 1701, Encrypt=13, Decrypt=57, EncryptLen=1712, DecryptLen=1701.
                 * 1801, Encrypt=13, Decrypt=59, EncryptLen=1808, DecryptLen=1801.
                 * 1901, Encrypt=14, Decrypt=62, EncryptLen=1904, DecryptLen=1901.
                 * 2001, Encrypt=14, Decrypt=65, EncryptLen=2016, DecryptLen=2001.
                 * 2101, Encrypt=15, Decrypt=67, EncryptLen=2112, DecryptLen=2101.
                 * 2201, Encrypt=15, Decrypt=69, EncryptLen=2208, DecryptLen=2201.
                 * 2301, Encrypt=15, Decrypt=72, EncryptLen=2304, DecryptLen=2301.
                 * 2401, Encrypt=16, Decrypt=74, EncryptLen=2416, DecryptLen=2401.
                 * 2501, Encrypt=16, Decrypt=77, EncryptLen=2512, DecryptLen=2501.
                 * 2601, Encrypt=17, Decrypt=79, EncryptLen=2608, DecryptLen=2601.
                 * 2701, Encrypt=17, Decrypt=83, EncryptLen=2704, DecryptLen=2701.
                 * 2801, Encrypt=18, Decrypt=85, EncryptLen=2816, DecryptLen=2801.
                 * 2901, Encrypt=19, Decrypt=88, EncryptLen=2912, DecryptLen=2901.
                 * 3001, Encrypt=21, Decrypt=90, EncryptLen=3008, DecryptLen=3001.
                 *
                 *
                 *
                 */
            }

            return(true);
        }
Example #3
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);
        }
Example #4
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);
        }
Example #5
0
        public Boolean Run()
        {
            Console.WriteLine($"DES 測試:");

            Console.WriteLine($"1. 加密.");
            string sPlainTextSalt = DateTime.Now.ToString("yyyyMMddHHmmssfff");
            string sPlainText     = "123, 到台灣, 台灣有個阿里山. ~!@#$%^&*()<>{}[]:;\"'*%!#\\/ABCD." + sPlainTextSalt;
            string sKey           = "12345678";
            string sIV            = "12345678";

            byte[] baPlainText = ZByte.GetBytesUTF8(sPlainText);
            byte[] baKey       = ZByte.GetBytesUTF8(sKey);
            byte[] baIV        = ZByte.GetBytesUTF8(sIV);
            byte[] baEncrypt   = ZSecurity.EncryptDES(baPlainText, baKey, baIV);
            if (baEncrypt == null)
            {
                Console.WriteLine(ZSecurity.msError);
                return(false);
            }
            Console.WriteLine($"原文: {baPlainText.Length}, {sPlainText}");
            Console.WriteLine($"Key: {baKey.Length}, {baKey.ZGetStringHex()}");
            Console.WriteLine($"IV: {baIV.Length}, {baIV.ZGetStringHex()}");
            Console.WriteLine($"密文: {baEncrypt.Length}, {baEncrypt.ZGetStringHex()}");
            Console.WriteLine();

            Console.WriteLine($"2. 解密.");
            byte[] baDecrypt = ZSecurity.DecryptDES(baEncrypt, baKey, baIV);
            if (baDecrypt == null)
            {
                Console.WriteLine(ZSecurity.msError);
                return(false);
            }
            string sDecrypt = ZByte.ZGetStringUTF8(baDecrypt);

            Console.WriteLine($"解密: {baDecrypt.Length}, {sDecrypt}");
            Console.WriteLine($"驗證: {baPlainText.ZEquals(baDecrypt)}");
            Console.WriteLine();

            Console.WriteLine($"3. 加密-金鑰加鹽");
            Console.WriteLine($"以RFC2898 標準 產生 Key 和 IV.");
            string sSalt = "12345678";

            byte[] baSalt   = ZByte.GetBytesUTF8(sSalt);
            var    vRFC2898 = ZSecurity.CreateRFC2898(baKey, baSalt);

            byte[] baKey_RFC2898 = vRFC2898.GetBytes(8);
            byte[] baIV_RFC2898  = vRFC2898.GetBytes(8);
            baEncrypt = ZSecurity.EncryptDES(baPlainText, baKey_RFC2898, baIV_RFC2898);
            if (baEncrypt == null)
            {
                Console.WriteLine(ZSecurity.msError);
                return(false);
            }

            Console.WriteLine($"Key Salted: {baKey.Length}, {baKey_RFC2898.ZGetStringHex()}");
            Console.WriteLine($"IV Salted: {baIV.Length}, {baIV_RFC2898.ZGetStringHex()}");
            Console.WriteLine($"密文: {baEncrypt.Length}, {baEncrypt.ZGetStringHex()}");
            Console.WriteLine();

            Console.WriteLine($"4. 解密-金鑰加鹽.");
            baDecrypt = ZSecurity.DecryptDES(baEncrypt, baKey_RFC2898, baIV_RFC2898);
            if (baDecrypt == null)
            {
                Console.WriteLine(ZSecurity.msError);
                return(false);
            }
            sDecrypt = ZByte.ZGetStringUTF8(baDecrypt);
            Console.WriteLine($"解密: {baDecrypt.Length}, {sDecrypt}");
            Console.WriteLine($"驗證: {baPlainText.ZEquals(baDecrypt)}");
            Console.WriteLine();

            Console.WriteLine($"5. 加密-檔案");
            string sFilePlainText = "DESPlainText.dat";
            string sFileEncrypt   = "DESEncrypt.dat";

            File.WriteAllBytes(sFilePlainText, baPlainText);
            if (!ZSecurity.EncryptDES(sFilePlainText, sFileEncrypt, baKey_RFC2898, baIV_RFC2898))
            {
                Console.WriteLine(ZSecurity.msError);
                return(false);
            }

            Console.WriteLine($"6. 解密-檔案");
            string sFileDecrypt = "DESDecrypt.dat";

            if (!ZSecurity.DecryptDES(sFileEncrypt, sFileDecrypt, baKey_RFC2898, baIV_RFC2898))
            {
                Console.WriteLine(ZSecurity.msError);
                return(false);
            }
            baDecrypt = File.ReadAllBytes(sFileDecrypt);
            Console.WriteLine($"驗證: {baPlainText.ZEquals(baDecrypt)}");
            Console.WriteLine();

            return(true);
        }