Beispiel #1
0
        public CaptchaContainer(EndPoint ep, int id, Key pubKey, object payload)
        {
            _ep = ep;
            _id = id;

            using (SymmetricAlgorithm algo = new CamelliaManaged ())
            using (ECIES ecies = new ECIES (DefaultAlgorithm.ECDomainName, algo)) {
                ecies.Parameters.PublicKey = pubKey.GetByteArray ();
                _encrypted = ecies.Encrypt (Serializer.Instance.Serialize (payload));
            }
        }
Beispiel #2
0
 public static object Decrypt(ECKeyPair privateKey, byte[] encrypted)
 {
     using (SymmetricAlgorithm algo = new CamelliaManaged ())
     using (ECIES ecies = new ECIES (DefaultAlgorithm.ECDomainName, algo)) {
         ecies.Parameters.PrivateKey = privateKey.PrivateKey;
         try {
             return Serializer.Instance.Deserialize (ecies.Decrypt (encrypted));
         } catch {
             return null;
         }
     }
 }
Beispiel #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 ("秘密鍵として認識することができません");
			}
		}
Beispiel #4
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);
			}
		}
Beispiel #5
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;
			}
		}