Exemple #1
0
        private string CryptoTransform(string input, bool base64in, bool base64out, Aes cipher, CMode mode)
        {
            byte[] bytes;
            if (base64in)
                bytes = decode64(input);
            else
                bytes = Encoding.UTF8.GetBytes(input);


            using (var c = mode == CMode.ENCRYPT ? cipher.CreateEncryptor() : cipher.CreateDecryptor()) {
            var buf = c.TransformFinalBlock(bytes, 0, bytes.Length);
            return base64out ? encode64(buf) : Encoding.UTF8.GetString(buf);
            }
        }
Exemple #2
0
        public CIPHER(CType type, CMode mode, bool encrypt)
        {
            EVP_CIPHER *    cipher = null; // Statically allocated (MT), don't free
            EVP_CIPHER_CTX *handle = null;

            try {
                cipher = (EVP_CIPHER *)_ciphers[new Tuple <CType, CMode>(type, mode)]();
            } catch (KeyNotFoundException) {}

            if (cipher == null)
            {
                goto Bailout;
            }

            if ((handle = _CIPHER.EVP_CIPHER_CTX_new()) == null)
            {
                goto Bailout;
            }
            _CIPHER.EVP_CIPHER_CTX_init(handle);
            if (_CIPHER.EVP_CipherInit_ex(handle, cipher, IntPtr.Zero, null, null, encrypt ? 1 : 0) == 0)
            {
                goto Bailout;
            }
            _CIPHER.EVP_CIPHER_CTX_set_padding(handle, 0);

            this._handle  = handle;
            this._type    = type;
            this._mode    = mode;
            this._encrypt = encrypt;

            return;

Bailout:
            if (handle != null)
            {
                _CIPHER.EVP_CIPHER_CTX_free(handle);
            }
            throw new EVPException();
        }
Exemple #3
0
        private string CryptoTransform(string input, bool base64in, bool base64out, Aes cipher, CMode mode)
        {
            byte[] bytes;
            if (base64in)
            {
                bytes = decode64(input);
            }
            else
            {
                bytes = Encoding.UTF8.GetBytes(input);
            }


            using (var c = mode == CMode.ENCRYPT ? cipher.CreateEncryptor() : cipher.CreateDecryptor()) {
                var buf = c.TransformFinalBlock(bytes, 0, bytes.Length);
                return(base64out ? encode64(buf) : Encoding.UTF8.GetString(buf));
            }
        }
Exemple #4
0
        public CIPHER(CType type, CMode mode, bool encrypt)
        {
            EVP_CIPHER *cipher = null; // Statically allocated (MT), don't free
            EVP_CIPHER_CTX *handle = null;

            try {
                cipher = (EVP_CIPHER*) _ciphers[new Tuple<CType, CMode>(type, mode)]();
            } catch (KeyNotFoundException) {}

            if (cipher == null)
                goto Bailout;

            if ((handle = _CIPHER.EVP_CIPHER_CTX_new()) == null)
                goto Bailout;
            _CIPHER.EVP_CIPHER_CTX_init(handle);
            if (_CIPHER.EVP_CipherInit_ex(handle, cipher, IntPtr.Zero, null, null, encrypt ? 1 : 0) == 0)
                goto Bailout;
            _CIPHER.EVP_CIPHER_CTX_set_padding(handle, 0);

            this._handle  = handle;
            this._type    = type;
            this._mode    = mode;
            this._encrypt = encrypt;

            return ;

        Bailout:
            if (handle != null)
                _CIPHER.EVP_CIPHER_CTX_free(handle);
            throw new EVPException();
        }