Beispiel #1
0
        public string decryptString(string encrypted)
        {
            byte[] iv        = getbytes("12345678");
            byte[] plain     = getbytes(encrypted);
            byte[] decrypted = new byte[plain.Length];
            Array.Clear(decrypted, 0, decrypted.Length);
            byte[]      key = getbytes("p1zz@p1zz@p1zz@p1zz@p1zz@p1zz@p1");
            BlowfishCBC bfc = new BlowfishCBC();

            bfc.IV = iv;
            bfc.Initialize(key, 0, key.Length);
            bfc.Decrypt(plain, 0, decrypted, 0, plain.Length);
            return(getstring(decrypted));
        }
        /// <summary>Creates a new BlowfishEasy instance. Notice that this ctor
        /// supports only the new way of how string are set up. There is no support
        /// for the old BlowfishJ key setup available right now (which had a
        /// design flaw by not using the full Unicode character space).</summary>
        /// <param name="password">The password used for encryption and decryption.</param>
        public BlowfishEasy(String password)
        {
            int c = password.Length;

            byte[] passw = new byte[c << 1];

            for (int i = 0, j = 0; i < c; i++)
            {
                int chr = (int)password[i];
                passw[j++] = (byte)((chr >> 8) & 0x0ff);
                passw[j++] = (byte)(chr & 0x0ff);
            }

            SHA1 sha = new SHA1Managed();

            byte[] key = sha.ComputeHash(passw);

            this.bfc = new BlowfishCBC(key, 0, key.Length);
        }
Beispiel #3
0
    public static void TestBlowfishCBC()
    {
        String sDemo = "The Blowfish encryption algorithm was introduced in 1994.";

        System.Console.WriteLine(sDemo);

        byte[] ptext = StringToBlocks(sDemo);

        byte[] key = new byte[16];

        byte bI;

        for (bI = 0; bI < key.Length; bI++)
        {
            key[bI] = bI;
        }

        byte[] iv = new byte[Blowfish.BLOCKSIZE];
        for (bI = 0; bI < iv.Length; bI++)
        {
            iv[bI] = (byte)bI;
        }

        BlowfishCBC bfc = new BlowfishCBC(key, iv);

        byte[] ctext = new byte[ptext.Length];

        bfc.Encrypt(ptext, ctext, 0, 0, ptext.Length);

        System.Console.WriteLine(BlocksToString(ctext));

        bfc.Iv = iv;

        bfc.Decrypt(ctext, ctext, 0, 0, ctext.Length);

        bfc.Burn();

        System.Console.WriteLine(BlocksToString(ctext));
    }
Beispiel #4
0
        public string encryptString(string decrypted, string password)
        {
            byte[] iv        = getbytes("12345678");
            int    blocksize = iv.Length;
            int    numblocks = (decrypted.Length / blocksize) + 1;
            int    decSize   = decrypted.Length;

            //pad the string with nulls so that it is a multiple of blocksize
            for (int i = decSize; i < numblocks * blocksize; i++)
            {
                decrypted += '\0';
            }

            byte[] plain     = getbytes(decrypted);
            byte[] encrypted = new byte[plain.Length];
            Array.Clear(encrypted, 0, encrypted.Length);
            byte[]      key = getbytes(password);
            BlowfishCBC bfc = new BlowfishCBC();

            bfc.IV = iv;
            bfc.Initialize(key, 0, key.Length);
            bfc.Encrypt(plain, 0, encrypted, 0, plain.Length);
            return(string2hex(getstring(encrypted)));
        }