Beispiel #1
0
 /// <summary>
 /// The hybrid encrypt.
 /// </summary>
 /// <param name="message">
 /// The message.
 /// </param>
 /// <param name="key">
 /// The key.
 /// </param>
 /// <returns>
 /// The <see cref="Message"/>.
 /// </returns>
 public Message HybridEncrypt(string message, PublicKey key)
 {
     SecretKey secretKey = this.keyMaster.GenerateSecretKey();
     string encryptedKey = this.Encrypt(secretKey, key);
     string encryptedMsg = this.Encrypt(message, secretKey);
     return new Message(encryptedKey, encryptedMsg);
 }
Beispiel #2
0
 /// <summary>
 /// The encode public key.
 /// </summary>
 /// <param name="key">
 /// The key.
 /// </param>
 /// <returns>
 /// The <see cref="string"/>.
 /// </returns>
 public string EncodePublicKey(PublicKey key)
 {
     SubjectPublicKeyInfo publicKeyInfo = key.GetPublicKeyInfo();
     DerBitString bitString = publicKeyInfo.PublicKeyData;
     byte[] data = bitString.GetBytes();
     string encoded = Convert.ToBase64String(data);
     return encoded;
 }
Beispiel #3
0
        /// <summary>
        /// The update public key.
        /// </summary>
        /// <param name="keyId">
        /// The key id.
        /// </param>
        /// <param name="key">
        /// The key.
        /// </param>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        public string UpdatePublicKey(string keyId, PublicKey key)
        {
            string result = null;
            if (key != null)
            {
                string url = string.Format(UrlUpdateKey, this.baseUrl, keyId);
                Log.Info("updatePublicKey : " + url);
                string keyString = this.keyMaster.EncodePublicKey(key);
                Log.Info("key encoded : " + keyString);
                try
                {
                    result = HttpUtils.PostRaw(url, keyString);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }

            return result;
        }
Beispiel #4
0
        /// <summary>
        /// The post public key.
        /// </summary>
        /// <param name="key">
        /// The key.
        /// </param>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        public string PostPublicKey(PublicKey key)
        {
            string result = null;
            if (key != null)
            {
                string url = string.Format(UrlPostKey, BaseUrl);
                Log.Info("postPublicKey : " + url);
                string keyString = this.keyMaster.EncodePublicKey(key);
                Log.Info("key encoded : " + keyString);
                result = HttpUtils.PostRaw(url, keyString);
            }

            return result;
        }
Beispiel #5
0
 /// <summary>
 /// generates a key pair.
 /// </summary>
 /// <param name="keySize">
 /// The key Size in bits
 /// </param>
 /// <returns>
 /// The <see cref="KeyPair"/>.
 /// </returns>
 public KeyPair GenerateKeyPair(int keySize)
 {
     var g = new RsaKeyPairGenerator();
     g.Init(new KeyGenerationParameters(new SecureRandom(), keySize));
     var kp = g.GenerateKeyPair();
     var puk = new PublicKey(kp.Public);
     var prk = new PrivateKey(kp.Private);
     var keyPair = new KeyPair { PublicKey = puk, PrivateKey = prk };
     return keyPair;
 }
Beispiel #6
0
 /// <summary>
 /// The equals.
 /// </summary>
 /// <param name="other">
 /// The other.
 /// </param>
 /// <returns>
 /// The <see cref="bool"/>.
 /// </returns>
 protected bool Equals(PublicKey other)
 {
     return this.publicKeyParam.Equals(other.publicKeyParam);
 }
Beispiel #7
0
 /// <summary>
 /// The encrypt.
 /// </summary>
 /// <param name="data">
 /// The data.
 /// </param>
 /// <param name="pk">
 /// The pk.
 /// </param>
 /// <returns>
 /// The <see cref="string"/>.
 /// </returns>
 private string Encrypt(string data, PublicKey pk)
 {
     byte[] bytes = Encoding.UTF8.GetBytes(data);
     var keyParam = pk.GetPublicKeyParam();
     var engine = new Pkcs1Encoding(new RsaEngine());
     engine.Init(true, keyParam);
     var blockSize = bytes.Length; // engine.GetInputBlockSize();
     byte[] enc = engine.ProcessBlock(bytes, 0, blockSize);
     return Convert.ToBase64String(enc);
 }
Beispiel #8
0
 /// <summary>
 /// encrypts the given secret key with the given public key
 /// </summary>
 /// <param name="secretKey">
 /// The secret key.
 /// </param>
 /// <param name="key">
 /// The key.
 /// </param>
 /// <returns>
 /// The <see cref="string"/>.
 /// </returns>
 private string Encrypt(SecretKey secretKey, PublicKey key)
 {
     string data = this.keyMaster.EncodeSecretKey(secretKey);
     return this.Encrypt(data, key);
 }
Beispiel #9
0
 /// <summary>
 /// The public encrypt.
 /// </summary>
 /// <param name="message">
 /// The message.
 /// </param>
 /// <param name="key">
 /// The key.
 /// </param>
 /// <returns>
 /// The <see cref="Message"/>.
 /// </returns>
 public Message PublicEncrypt(string message, PublicKey key)
 {
     return new Message(null, this.Encrypt(message, key));
 }