/// <summary>
        ///   encrypts a string
        /// </summary>
        /// <remarks>
        ///   for efficiency the given string will be UTF8 encoded
        ///   and padded to the next block border, the CBC IV plus
        ///   the encrypted data will then be BASE64 encoded and
        ///   returned as an string
        /// </remarks>
        /// <param name="sPlainText">
        ///   the string to encrypt
        /// </param>
        /// <returns>
        ///   the encrypted string
        /// </returns>
        public String Encrypt
            (String sPlainText)
        {
            int nI;

            // convert string
            byte[] ueData = m_ue.GetBytes(sPlainText);

            // prepare the input and the output buffer
            int nOrigLen = ueData.Length;
            int nLen     = nOrigLen;

            // (we need aligned and "pad-able" buffers)
            int nMod = nLen % Blowfish.BLOCKSIZE;

            nLen = (nLen - nMod) + Blowfish.BLOCKSIZE;

            // allocate the input buffer
            byte[] inBuf = new byte[nLen];

            // copy the data and do the padding
            Array.Copy(ueData, 0, inBuf, 0, nOrigLen);

            nI = nLen - (Blowfish.BLOCKSIZE - nMod);
            while (nI < nLen)
            {
                inBuf[nI++] = (byte)nMod;
            }

            // allocate the output buffer
            byte[] outBuf = new byte[inBuf.Length + Blowfish.BLOCKSIZE];

            // create and set a new IV
            byte[] iv = new byte[Blowfish.BLOCKSIZE];
            m_rng.GetBytes(iv);
            m_bfc.Iv = iv;

            // do the encryption
            m_bfc.Encrypt(inBuf,
                          outBuf,
                          0,
                          Blowfish.BLOCKSIZE,
                          inBuf.Length);

            // copy the IV
            Array.Copy(iv, 0, outBuf, 0, Blowfish.BLOCKSIZE);

            // BASE64 encode the whole thing
            String sResult = Convert.ToBase64String(outBuf);

            // finally clear the plaintext buffer
            Array.Clear(inBuf, 0, inBuf.Length);

            return(sResult);
        }
Exemple #2
0
        public int TransformBlock
        (
            byte[] bufIn,
            int nOfsIn,
            int nCount,
            byte[] bufOut,
            int nOfsOut
        )
        {
            // NOTE: we assume that the caller understands the meaning of
            //	     this method and that only even byte boundaries are
            //       given, thus we do not cache any data left internally

            int nResult = 0;

            if (null != m_bfc)
            {
                if (m_blIsEncryptor)
                {
                    nResult = m_bfc.Encrypt(bufIn, bufOut, nOfsIn, nOfsOut, nCount);
                }
                else
                {
                    nResult = m_bfc.Decrypt(bufIn, bufOut, nOfsIn, nOfsOut, nCount);
                }
            }
            else if (null != m_bf)
            {
                if (m_blIsEncryptor)
                {
                    nResult = m_bf.Encrypt(bufIn, bufOut, nOfsIn, nOfsOut, nCount);
                }
                else
                {
                    nResult = m_bf.Decrypt(bufIn, bufOut, nOfsIn, nOfsOut, nCount);
                }
            }
            else
            {
                nResult = 0;
            }

            return(nResult * Blowfish.BLOCKSIZE);
        }
Exemple #3
0
        public int TransformBlock
        (
            byte[] bufIn,
            int nOfsIn,
            int nCount,
            byte[] bufOut,
            int nOfsOut
        )
        {
            int nResult = 0;

            if (null != m_bfc)
            {
                if (m_blIsEncryptor)
                {
                    nResult = m_bfc.Encrypt(bufIn, bufOut, nOfsIn, nOfsOut, nCount);
                }
                else
                {
                    nResult = m_bfc.Decrypt(bufIn, bufOut, nOfsIn, nOfsOut, nCount);
                }
            }
            else if (null != m_bf)
            {
                if (m_blIsEncryptor)
                {
                    nResult = m_bf.Encrypt(bufIn, bufOut, nOfsIn, nOfsOut, nCount);
                }
                else
                {
                    nResult = m_bf.Decrypt(bufIn, bufOut, nOfsIn, nOfsOut, nCount);
                }
            }
            else
            {
                nResult = 0;
            }

            return(nResult * Blowfish.BLOCKSIZE);
        }