Beispiel #1
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 #2
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 #3
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 #4
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 #5
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 #6
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;
        }