示例#1
0
        public void Test_Random_with_SharedInfo1()
        {
            ECDomainNames domainName = ECDomainNames.secp256r1;

            for (int i = 0; i < 5; i++)
            {
                ECIES  ecies1     = new ECIES(domainName);
                ECIES  ecies2     = new ECIES(domainName);
                byte[] sharedInfo = RNG.GetBytes(RNG.GetBytes(1)[0] + 1);
                byte[] plainText  = RNG.GetBytes(RNG.GetBytes(1)[0] + RNG.GetBytes(1)[0] + 1);

                // setup shared info 1
                ecies1.SharedInfo1 = sharedInfo;
                ecies2.SharedInfo1 = sharedInfo;

                // ecies2 exports public key.
                byte[] publicKey = ecies2.Parameters.ExportPublicKey(true);

                // ecies1 imports public key.
                ecies1.Parameters.PublicKey = publicKey;

                // ecies1 encrypt plainText.
                byte[] cipherText = ecies1.Encrypt(plainText);

                // ecies2 decrypt cipherText.
                byte[] decrypted = ecies2.Decrypt(cipherText);

                // Check !
                Assert.AreEqual(plainText, decrypted);
            }
        }
示例#2
0
        public void Test_Random()
        {
            ECDomainNames domainName = ECDomainNames.secp160r1;

            for (int i = 0; i < 10; i++)
            {
                ECIES  ecies1    = new ECIES(domainName);
                ECIES  ecies2    = new ECIES(domainName);
                byte[] plainText = RNG.GetBytes(RNG.GetBytes(1)[0] + RNG.GetBytes(1)[0]);

                // ecies2 exports public key.
                byte[] publicKey = ecies2.Parameters.ExportPublicKey(true);

                // ecies1 imports public key.
                ecies1.Parameters.PublicKey = publicKey;

                // ecies1 encrypt plainText.
                byte[] cipherText = ecies1.Encrypt(plainText);

                // ecies2 decrypt cipherText.
                byte[] decrypted = ecies2.Decrypt(cipherText);

                // Check !
                Assert.AreEqual(plainText, decrypted);
            }
        }
示例#3
0
        private void btnDecryptText_Click(object sender, EventArgs e)
        {
            try {
                KeyEntry privateKeyEntry = cbPrivateKeys2.SelectedItem as KeyEntry;
                if (privateKeyEntry == null)
                {
                    throw new Exception("復号に利用する秘密鍵を指定してください");
                }
                ECDomainNames domain;
                byte[]        privateKey = ParsePrivateKey(privateKeyEntry.Key, txtDecryptKeyPass.Text, out domain);
                string        text       = txtDecryptCipher.Text;
                string        encrypt_type;
                byte[]        encrypted;
                try {
                    encrypt_type = text.Substring(0, text.IndexOf('='));
                    text         = text.Substring(text.IndexOf('=') + 1);
                    encrypted    = Convert.FromBase64String(text);
                } catch {
                    throw new CryptographicException("暗号文のフォーマットを認識できません");
                }
                if (encrypt_type.StartsWith("ecies+"))
                {
                    encrypt_type = encrypt_type.Substring(6);
                    SymmetricAlgorithm algo = null;
                    switch (encrypt_type)
                    {
                    case "xor":
                        break;

                    case "camellia128":
                    case "camellia256":
                    case "rijndael128":
                    case "rijndael256":
                        algo           = encrypt_type.StartsWith("camellia") ? (SymmetricAlgorithm) new CamelliaManaged() : (SymmetricAlgorithm) new openCrypto.RijndaelManaged();
                        algo.BlockSize = 128;
                        algo.KeySize   = encrypt_type.EndsWith("128") ? 128 : 256;
                        algo.Mode      = CipherMode.CBC;
                        algo.Padding   = PaddingMode.PKCS7;
                        break;

                    default:
                        throw new CryptographicException("対応していない暗号化形式です");
                    }
                    ECIES ecies = new ECIES(domain, algo);
                    ecies.Parameters.PrivateKey = privateKey;
                    txtDecryptPlain.Text        = Encoding.UTF8.GetString(ecies.Decrypt(encrypted));
                }
                else
                {
                    throw new CryptographicException("対応していない暗号化形式です");
                }
            } catch (Exception ex) {
                MessageBox.Show(ex.Message);
            }
        }
示例#4
0
        public void Test_GEC2()
        {
            ECDomainNames      domainName = ECDomainNames.secp160r1;
            ECDomainParameters domain     = ECDomains.GetDomainParameter(domainName);
            ECIES     ecies     = new ECIES(domainName);
            Number    V_Private = Number.Parse("45FB58A92A17AD4B15101C66E74F277E2B460866", 16);
            ECKeyPair pair      = new ECKeyPair(V_Private, null, domain);

            pair.CreatePublicKeyFromPrivateKey();
            ecies.Parameters._Q = pair._Q;
            byte[] M         = System.Text.Encoding.ASCII.GetBytes("abcdefghijklmnopqrst");
            byte[] k         = Number.Parse("702232148019446860144825009548118511996283736794", 10).ToByteArray(20, false);
            byte[] C         = ecies.Encrypt(M, k);
            byte[] expectedC = new byte[] { 0x02, 0xCE, 0x28, 0x73, 0xE5, 0xBE, 0x44, 0x95, 0x63, 0x39, 0x1F, 0xEB, 0x47, 0xDD, 0xCB, 0xA2, 0xDC, 0x16, 0x37, 0x91, 0x91, 0x71, 0x23, 0xC8, 0x70, 0xA3, 0x1A, 0x81, 0xEA, 0x75, 0x83, 0x29, 0x0D, 0x1B, 0xA1, 0x7B, 0xC8, 0x75, 0x94, 0x35, 0xED, 0x1C, 0xCD, 0xA9, 0xEB, 0x4E, 0xD2, 0x73, 0x60, 0xBE, 0x89, 0x67, 0x29, 0xAD, 0x18, 0x54, 0x93, 0x62, 0x25, 0x91, 0xE5 };
            Assert.AreEqual(expectedC, C, "Encryption");

            ecies = new ECIES(domainName);
            ecies.Parameters._d = V_Private;
            byte[] M2 = ecies.Decrypt(C);
            Assert.AreEqual(M, M2, "Decryption");
        }
示例#5
0
        public void Test_Camellia()
        {
            using (SymmetricAlgorithmPlus algo = new CamelliaManaged()) {
                // Generate test data
                byte[] plain = RNG.GetBytes(16 * 8);
                byte[] cipher, decrypted;
                ECIES  ecies;

                // Test.1 128bit ECB Encryption with No-padding
                algo.KeySize   = 128;
                algo.BlockSize = 128;
                algo.Mode      = System.Security.Cryptography.CipherMode.ECB;
                algo.Padding   = System.Security.Cryptography.PaddingMode.None;
                ecies          = new ECIES(ECDomainNames.secp192r1, algo);
                cipher         = ecies.Encrypt(plain);
                decrypted      = ecies.Decrypt(cipher);
                Assert.AreEqual(plain, decrypted, "#1");

                // Test.2 128bit CBC Encryption with No-padding
                algo.Mode = System.Security.Cryptography.CipherMode.CBC;
                ecies     = new ECIES(ECDomainNames.secp192r1, algo);
                cipher    = ecies.Encrypt(plain);
                decrypted = ecies.Decrypt(cipher);
                Assert.AreEqual(plain, decrypted, "#2");

                // Test.3 128bit CBC Encryption with PKCS7 Padding
                algo.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
                ecies        = new ECIES(ECDomainNames.secp192r1, algo);
                cipher       = ecies.Encrypt(plain);
                decrypted    = ecies.Decrypt(cipher);
                Assert.AreEqual(plain, decrypted, "#3");

                // Test.4 128bit CBC Encryption with PKCS7 Padding
                plain        = RNG.GetBytes(16 * 8 + 3);
                algo.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
                ecies        = new ECIES(ECDomainNames.secp192r1, algo);
                cipher       = ecies.Encrypt(plain);
                decrypted    = ecies.Decrypt(cipher);
                Assert.AreEqual(plain, decrypted, "#4");

                // Test.5 128bit CBC Encryption with ANSIX923 Padding
                plain        = RNG.GetBytes(16 * 8 + 7);
                algo.Padding = System.Security.Cryptography.PaddingMode.ANSIX923;
                ecies        = new ECIES(ECDomainNames.secp192r1, algo);
                cipher       = ecies.Encrypt(plain);
                decrypted    = ecies.Decrypt(cipher);
                Assert.AreEqual(plain, decrypted, "#5");

                // Test.6 128bit CBC Encryption with ISO10126 Padding
                plain        = RNG.GetBytes(16 * 8 + 9);
                algo.Padding = System.Security.Cryptography.PaddingMode.ISO10126;
                ecies        = new ECIES(ECDomainNames.secp192r1, algo);
                cipher       = ecies.Encrypt(plain);
                decrypted    = ecies.Decrypt(cipher);
                Assert.AreEqual(plain, decrypted, "#6");

                // Test.7 128bit CBC Encryption with Zeros Padding
                plain        = RNG.GetBytes(16 * 8 + 11);
                algo.Padding = System.Security.Cryptography.PaddingMode.Zeros;
                ecies        = new ECIES(ECDomainNames.secp192r1, algo);
                cipher       = ecies.Encrypt(plain);
                decrypted    = ecies.Decrypt(cipher);
                for (int i = 0; i < plain.Length; i++)
                {
                    Assert.AreEqual(plain[i], decrypted[i], "#7.1");
                }
                for (int i = plain.Length; i < decrypted.Length; i++)
                {
                    Assert.AreEqual(0, decrypted[i], "#7.2");
                }
            }
        }