Ejemplo n.º 1
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));
    }
Ejemplo n.º 2
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)));
        }