Example #1
0
        public void TestOFB_MultiBlock_2()
        {
            SymmetricAlgorithmPlus algo = new CamelliaManaged();

            algo.ModePlus = CipherModePlus.OFB;
            Test_MultiBlock_2(algo);
        }
Example #2
0
        public void TestCBC_MultiBlock_1()
        {
            SymmetricAlgorithmPlus algo = new CamelliaManaged();

            algo.ModePlus = CipherModePlus.CBC;
            Test_MultiBlock_1(algo);
        }
Example #3
0
        static byte[] ParsePrivateKey(string str_key, string str_passwd, out ECDomainNames domain)
        {
            try {
                string str_domain = null;
                byte[] key        = null;
                if (!char.IsDigit(str_key[0]))
                {
                    if (str_passwd.Length == 0)
                    {
                        throw new CryptographicException("秘密鍵は暗号化されています。パスフレーズを入力してください。");
                    }
                    byte[] pass = ComputeHash(new SHA256Managed(), Encoding.UTF8.GetBytes(str_passwd), true);
                    byte[] iv   = ComputeHash(new SHA1Managed(), Encoding.UTF8.GetBytes(str_passwd), true);
                    Array.Resize <byte> (ref iv, 128 >> 3);
                    string encType = str_key.Substring(0, str_key.IndexOf('='));
                    str_key    = str_key.Substring(str_key.IndexOf('=') + 1);
                    str_domain = str_key.Substring(0, str_key.IndexOf('='));
                    str_key    = str_key.Substring(str_key.IndexOf('=') + 1);
                    byte[] encrypted = Convert.FromBase64String(str_key);
                    try {
                        SymmetricAlgorithm algo = null;
                        switch (encType)
                        {
                        case "camellia256":
                            algo = new CamelliaManaged();
                            break;

                        case "rijndael256":
                            algo = new openCrypto.RijndaelManaged();
                            break;

                        default:
                            throw new CryptographicException("秘密鍵の暗号化タイプを認識できません");
                        }
                        key = Decrypt(algo, CipherMode.CBC, pass, iv, encrypted);
                    } catch {
                        throw new CryptographicException("パスフレーズが違います");
                    }
                }
                else
                {
                    str_domain = str_key.Substring(0, str_key.IndexOf('='));
                    str_key    = str_key.Substring(str_key.IndexOf('=') + 1);
                    key        = Convert.FromBase64String(str_key);
                }
                str_domain = "secp" + str_domain;
                domain     = (ECDomainNames)Enum.Parse(typeof(ECDomainNames), str_domain);
                return(key);
            } catch (CryptographicException) {
                throw;
            } catch {
                throw new CryptographicException("秘密鍵として認識することができません");
            }
        }
Example #4
0
        string ToPrivateKeyString(byte[] privateKey, string passphrase, ECDomainNames domain)
        {
            string domainName = domain.ToString().Substring(4);

            if (passphrase.Length > 0)
            {
                byte[] pass = ComputeHash(new SHA256Managed(), Encoding.UTF8.GetBytes(txtGeneratedKeyPass.Text), true);
                byte[] iv   = ComputeHash(new SHA1Managed(), Encoding.UTF8.GetBytes(txtGeneratedKeyPass.Text), true);
                Array.Resize <byte> (ref iv, 128 >> 3);
                string             encType = null;
                SymmetricAlgorithm algo    = null;
                switch (cbPassEncryptType.SelectedIndex)
                {
                case 0:
                    encType = "camellia256";
                    algo    = new CamelliaManaged();
                    break;

                case 1:
                    encType = "rijndael256";
                    algo    = new openCrypto.RijndaelManaged();
                    break;

                default:
                    throw new CryptographicException("暗号化の種類を認識できません");
                }
                byte[] encrypted      = Encrypt(algo, CipherMode.CBC, pass, iv, privateKey);
                string privateKeyText = Convert.ToBase64String(encrypted);
                return(encType + "=" + domainName + "=" + privateKeyText);
            }
            else
            {
                string privateKeyText = Convert.ToBase64String(privateKey);
                return(domainName + "=" + privateKeyText);
            }
        }
Example #5
0
        private void btnEncryptText_Click(object sender, EventArgs e)
        {
            if (txtEncryptPlain.Text.Length == 0)
            {
                return;
            }
            try {
                KeyEntry publicKeyEntry = cbPublicKeys2.SelectedItem as KeyEntry;
                if (publicKeyEntry == null)
                {
                    throw new Exception("暗号化に利用する公開鍵を選択してください");
                }
                ECDomainNames      domain;
                byte[]             publicKey   = ParsePublicKey(publicKeyEntry.Key, out domain);
                string             encryptType = null;
                SymmetricAlgorithm algo        = null;
                switch (cbEncryptCrypto.SelectedIndex)
                {
                case 0:
                    encryptType = "ecies+xor";
                    algo        = null;
                    break;

                case 1:
                case 2:
                    encryptType    = "ecies+camellia";
                    algo           = new CamelliaManaged();
                    algo.BlockSize = 128;
                    if (cbEncryptCrypto.SelectedIndex == 1)
                    {
                        encryptType += "128";
                        algo.KeySize = 128;
                    }
                    else
                    {
                        encryptType += "256";
                        algo.KeySize = 256;
                    }
                    break;

                case 3:
                case 4:
                    encryptType    = "ecies+rijndael";
                    algo           = new openCrypto.RijndaelManaged();
                    algo.BlockSize = 128;
                    if (cbEncryptCrypto.SelectedIndex == 3)
                    {
                        encryptType += "128";
                        algo.KeySize = 128;
                    }
                    else
                    {
                        encryptType += "256";
                        algo.KeySize = 256;
                    }
                    break;

                default:
                    throw new CryptographicException("Unknown");
                }
                if (algo != null)
                {
                    algo.Mode    = CipherMode.CBC;
                    algo.Padding = PaddingMode.PKCS7;
                }
                ECIES ecies = new ECIES(domain, algo);
                ecies.Parameters.PublicKey = publicKey;
                string encrypted = Convert.ToBase64String(ecies.Encrypt(Encoding.UTF8.GetBytes(txtEncryptPlain.Text)));
                txtEncryptCipher.Text = encryptType + "=" + encrypted;
            } catch (Exception ex) {
                MessageBox.Show(ex.Message);
            }
        }
Example #6
0
		public void TestCFB_MultiBlock_1 ()
		{
			SymmetricAlgorithmPlus algo = new CamelliaManaged ();
			algo.ModePlus = CipherModePlus.CFB;
			Test_MultiBlock_1 (algo);
		}
Example #7
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");
			}
		}
Example #8
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");
                }
            }
        }