Exemple #1
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 #2
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);
        }
        /// <summary>
        ///   decrypts a string
        /// </summary>
        /// <remarks>
        ///   The string has to be decrypted with the same key,
        ///   otherwise the result will be just garbage. If you
        ///   want to check if the key is the right one use the
        ///   VerifyKey() method.
        /// </remarks>
        /// <param name="sCipherText">
        ///   the string to decrypt
        /// </param>
        /// <returns>
        ///   the decrypted (original) string (null on error)
        /// </returns>
        public String Decrypt
            (String sCipherText)
        {
            byte[] cdata = Convert.FromBase64String(sCipherText);

            if (cdata.Length < Blowfish.BLOCKSIZE)
            {
                return(null);
            }

            // set the cbc IV
            m_bfc.Iv = cdata;

            // decrypt the data
            byte[] outBuf = new byte[cdata.Length];

            int nDataAbs = cdata.Length - Blowfish.BLOCKSIZE;

            nDataAbs /= Blowfish.BLOCKSIZE;
            nDataAbs *= Blowfish.BLOCKSIZE;

            m_bfc.Decrypt(cdata,
                          outBuf,
                          Blowfish.BLOCKSIZE,
                          0,
                          nDataAbs);

            // calculate the original size
            int nOrigSize = nDataAbs -
                            Blowfish.BLOCKSIZE +
                            outBuf[nDataAbs - 1];

            // UTF8 decode the result and pass it back

            return(m_ue.GetString(outBuf, 0, nOrigSize));
        }