Example #1
0
        public override void Encrypt(byte[] buf, int length, byte[] outbuf, out int outlength)
        {
            if (_encryptCtx == IntPtr.Zero)
            {
                randBytes(outbuf, ivLen);
                InitCipher(ref _encryptCtx, outbuf, true);
                outlength = length + ivLen;
                lock (tempbuf)
                {
                    // C# could be multi-threaded
                    if (_disposed)
                    {
                        throw new ObjectDisposedException(this.ToString());
                    }
                    switch (_cipher)
                    {
                    case CIPHER_AES:
                        PolarSSL.aes_crypt_cfb128(_encryptCtx, PolarSSL.AES_ENCRYPT, length, ref _encryptIVOffset, _encryptIV, buf, tempbuf);
                        break;

                    case CIPHER_BF:
                        PolarSSL.blowfish_crypt_cfb64(_encryptCtx, PolarSSL.BLOWFISH_ENCRYPT, length, ref _encryptIVOffset, _encryptIV, buf, tempbuf);
                        break;

                    case CIPHER_RC4:
                        PolarSSL.arc4_crypt(_encryptCtx, length, buf, tempbuf);
                        break;
                    }
                    outlength = length + ivLen;
                    Buffer.BlockCopy(tempbuf, 0, outbuf, ivLen, length);
                }
            }
            else
            {
                outlength = length;
                if (_disposed)
                {
                    throw new ObjectDisposedException(this.ToString());
                }
                switch (_cipher)
                {
                case CIPHER_AES:
                    PolarSSL.aes_crypt_cfb128(_encryptCtx, PolarSSL.AES_ENCRYPT, length, ref _encryptIVOffset, _encryptIV, buf, outbuf);
                    break;

                case CIPHER_BF:
                    PolarSSL.blowfish_crypt_cfb64(_encryptCtx, PolarSSL.BLOWFISH_ENCRYPT, length, ref _encryptIVOffset, _encryptIV, buf, outbuf);
                    break;

                case CIPHER_RC4:
                    PolarSSL.arc4_crypt(_encryptCtx, length, buf, outbuf);
                    break;
                }
            }
        }
        public override void Decrypt(byte[] buf, int length, byte[] outbuf, out int outlength)
        {
            if (_decryptCtx == IntPtr.Zero)
            {
                InitCipher(ref _decryptCtx, buf, false);
                outlength = length - ivLen;
                lock (tempbuf)
                {
                    // C# could be multi-threaded
                    Buffer.BlockCopy(buf, ivLen, tempbuf, 0, length - ivLen);
                    if (_disposed)
                    {
                        throw new ObjectDisposedException(this.ToString());
                    }
                    switch (_cipher)
                    {
                    case CIPHER_AES:
                        PolarSSL.aes_crypt_cfb128(_decryptCtx, PolarSSL.AES_DECRYPT, length - ivLen, ref _decryptIVOffset, _decryptIV, tempbuf, outbuf);
                        break;

                    case CIPHER_RC4:
                        PolarSSL.arc4_crypt(_decryptCtx, length - ivLen, tempbuf, outbuf);
                        break;
                    }
                }
            }
            else
            {
                outlength = length;
                if (_disposed)
                {
                    throw new ObjectDisposedException(this.ToString());
                }
                switch (_cipher)
                {
                case CIPHER_AES:
                    PolarSSL.aes_crypt_cfb128(_decryptCtx, PolarSSL.AES_DECRYPT, length, ref _decryptIVOffset, _decryptIV, buf, outbuf);
                    break;

                case CIPHER_RC4:
                    PolarSSL.arc4_crypt(_decryptCtx, length, buf, outbuf);
                    break;
                }
            }
        }
        protected override void cipherUpdate(bool isCipher, int length, byte[] buf, byte[] outbuf)
        {
            // C# could be multi-threaded
            if (_disposed)
            {
                throw new ObjectDisposedException(this.ToString());
            }
            byte[] iv;
            int    ivOffset;
            IntPtr ctx;

            if (isCipher)
            {
                iv       = _encryptIV;
                ivOffset = _encryptIVOffset;
                ctx      = _encryptCtx;
            }
            else
            {
                iv       = _decryptIV;
                ivOffset = _decryptIVOffset;
                ctx      = _decryptCtx;
            }
            switch (_cipher)
            {
            case CIPHER_AES:
                PolarSSL.aes_crypt_cfb128(ctx, isCipher ? PolarSSL.AES_ENCRYPT : PolarSSL.AES_DECRYPT, length, ref ivOffset, iv, buf, outbuf);
                if (isCipher)
                {
                    _encryptIVOffset = ivOffset;
                }
                else
                {
                    _decryptIVOffset = ivOffset;
                }
                break;

            case CIPHER_RC4:
                PolarSSL.arc4_crypt(ctx, length, buf, outbuf);
                break;
            }
        }