Beispiel #1
0
 /// <summary>
 /// The equals.
 /// </summary>
 /// <param name="other">
 /// The other.
 /// </param>
 /// <returns>
 /// The <see cref="bool"/>.
 /// </returns>
 protected bool Equals(SecretKey other)
 {
     return(this.bytes.SequenceEqual(other.bytes));
 }
Beispiel #2
0
 /// <summary>
 /// The generate secret key.
 /// </summary>
 /// <returns>
 /// The <see cref="SecretKey"/>.
 /// </returns>
 public SecretKey GenerateSecretKey()
 {
     var kgp = new KeyGenerationParameters(new SecureRandom(), DesEdeParameters.DesEdeKeyLength * 8);
     var kg = new DesEdeKeyGenerator();
     kg.Init(kgp);
     var key = new SecretKey(kg.GenerateKey());
     return key;
 }
Beispiel #3
0
 /// <summary>
 /// The encode secret key.
 /// </summary>
 /// <param name="key">
 /// The key.
 /// </param>
 /// <returns>
 /// The <see cref="string"/>.
 /// </returns>
 public string EncodeSecretKey(SecretKey key)
 {
     var data = key.GetBytes();
     var encoded = Convert.ToBase64String(data);
     return encoded;
 }
Beispiel #4
0
        /// <summary>
        /// The symmetric decrypt.
        /// </summary>
        /// <param name="crypted">
        /// The crypted.
        /// </param>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        public string SymmetricDecrypt(Message crypted)
        {
            SecretKey key = this.keyMaster.DecodeSecretKey(crypted.Key);

            return(this.Decrypt(crypted.Data, key));
        }
Beispiel #5
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 #6
0
        /// <summary>
        /// The encrypt.
        /// </summary>
        /// <param name="data">
        /// The data.
        /// </param>
        /// <param name="key">
        /// The key.
        /// </param>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        private string Encrypt(string data, SecretKey key)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(data);

            // Setup the DESede cipher engine, create a PaddedBufferedBlockCipher in CBC mode.
            byte[] keyBytes = key.GetBytes();
            var cipher = new PaddedBufferedBlockCipher(new CbcBlockCipher(new DesEdeEngine()));

            // initialise the cipher with the key bytes, for encryption
            cipher.Init(true, new KeyParameter(keyBytes));

            int inBlockSize = bytes.Length;
            int outBlockSize = cipher.GetOutputSize(inBlockSize);

            var inblock = bytes;
            var outblock = new byte[outBlockSize];

            cipher.ProcessBytes(inblock, 0, inBlockSize, outblock, 0);
            cipher.DoFinal(outblock, 0);

            return Convert.ToBase64String(outblock);
        }
Beispiel #7
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 #8
0
        /// <summary>
        /// The decrypt.
        /// </summary>
        /// <param name="encrypted">
        /// The encrypted.
        /// </param>
        /// <param name="key">
        /// The key.
        /// </param>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        private string Decrypt(string encrypted, SecretKey key)
        {
            byte[] bytes = Convert.FromBase64String(encrypted);
            byte[] keyBytes = key.GetBytes();

            // initialise the cipher for decryption
            var cipher = new PaddedBufferedBlockCipher(new CbcBlockCipher(new DesEdeEngine()));
            cipher.Init(false, new KeyParameter(keyBytes));

            int inBlockSize = bytes.Length;
            int outBlockSize = cipher.GetOutputSize(inBlockSize);

            var inblock = bytes;
            var outblock = new byte[outBlockSize];

            cipher.ProcessBytes(inblock, 0, inBlockSize, outblock, 0);
            cipher.DoFinal(outblock, 0);

            var clear = this.ToUTF8String(outblock);
            return clear;
        }
Beispiel #9
0
 /// <summary>
 /// The symmetric encrypt.
 /// </summary>
 /// <param name="message">
 /// The message.
 /// </param>
 /// <param name="key">
 /// The key.
 /// </param>
 /// <returns>
 /// The <see cref="Message"/>.
 /// </returns>
 public Message SymmetricEncrypt(string message, SecretKey key)
 {
     string dataString = this.Encrypt(message, key);
     string keyString = this.keyMaster.EncodeSecretKey(key);
     return new Message(keyString, dataString);
 }
Beispiel #10
0
 /// <summary>
 /// The equals.
 /// </summary>
 /// <param name="other">
 /// The other.
 /// </param>
 /// <returns>
 /// The <see cref="bool"/>.
 /// </returns>
 protected bool Equals(SecretKey other)
 {
     return this.bytes.SequenceEqual(other.bytes);
 }