当前位置:  开发笔记 > 编程语言 > 正文

C#:Blowfish使用较少的字母

如何解决《C#:Blowfish使用较少的字母》经验,为你挑选了1个好方法。

我正在使用Blowfish.NET最新版本,但有一个问题.

responce = new byte[6]
{
     0x00, 0x80 ,0x01, 0x61, 0x00, 0x00
};
byte[] encrypted = new byte[responce.Length];
blowfish.Encrypt(responce, 2, encrypted, 2, input.Length - 2);

我以正确的方式调用它,我希望它从第三个字节开始读/写,长度是6 - 2,因为我不使用两个字节.

问题:

        public int Encrypt(
        byte[] dataIn,
        int posIn,
        byte[] dataOut,
        int posOut,
        int count) 
    {
        uint[] sbox1 = this.sbox1;
        uint[] sbox2 = this.sbox2;
        uint[] sbox3 = this.sbox3;
        uint[] sbox4 = this.sbox4;

        uint[] pbox = this.pbox;

        uint pbox00 = pbox[ 0];
        uint pbox01 = pbox[ 1];
        uint pbox02 = pbox[ 2];
        uint pbox03 = pbox[ 3];
        uint pbox04 = pbox[ 4];
        uint pbox05 = pbox[ 5];
        uint pbox06 = pbox[ 6];
        uint pbox07 = pbox[ 7];
        uint pbox08 = pbox[ 8];
        uint pbox09 = pbox[ 9];
        uint pbox10 = pbox[10];
        uint pbox11 = pbox[11];
        uint pbox12 = pbox[12];
        uint pbox13 = pbox[13];
        uint pbox14 = pbox[14];
        uint pbox15 = pbox[15];
        uint pbox16 = pbox[16];
        uint pbox17 = pbox[17]; // till this line count is 4

        count &= ~(BLOCK_SIZE - 1); //count becomes 0 after that calc :((

        int end = posIn + count; // 2 + 0 = 2

        while (posIn < end) //no loop :[
        {
            uint hi = (((uint)dataIn[posIn + 3]) << 24) |
                      (((uint)dataIn[posIn + 2]) << 16) |
                      (((uint)dataIn[posIn + 1]) <<  8) |
                              dataIn[posIn    ];

            uint lo = (((uint)dataIn[posIn + 7]) << 24) |
                      (((uint)dataIn[posIn + 6]) << 16) |
                      (((uint)dataIn[posIn + 5]) <<  8) |
                              dataIn[posIn + 4];
            posIn += 8; 

            hi ^= pbox00;
            lo ^= (((sbox1[(int)(hi >> 24)] + sbox2[(int)((hi >> 16) & 0x0ff)]) ^ sbox3[(int)((hi >> 8) & 0x0ff)]) + sbox4[(int)(hi & 0x0ff)]) ^ pbox01;
            hi ^= (((sbox1[(int)(lo >> 24)] + sbox2[(int)((lo >> 16) & 0x0ff)]) ^ sbox3[(int)((lo >> 8) & 0x0ff)]) + sbox4[(int)(lo & 0x0ff)]) ^ pbox02;
            lo ^= (((sbox1[(int)(hi >> 24)] + sbox2[(int)((hi >> 16) & 0x0ff)]) ^ sbox3[(int)((hi >> 8) & 0x0ff)]) + sbox4[(int)(hi & 0x0ff)]) ^ pbox03;
            hi ^= (((sbox1[(int)(lo >> 24)] + sbox2[(int)((lo >> 16) & 0x0ff)]) ^ sbox3[(int)((lo >> 8) & 0x0ff)]) + sbox4[(int)(lo & 0x0ff)]) ^ pbox04;
            lo ^= (((sbox1[(int)(hi >> 24)] + sbox2[(int)((hi >> 16) & 0x0ff)]) ^ sbox3[(int)((hi >> 8) & 0x0ff)]) + sbox4[(int)(hi & 0x0ff)]) ^ pbox05;
            hi ^= (((sbox1[(int)(lo >> 24)] + sbox2[(int)((lo >> 16) & 0x0ff)]) ^ sbox3[(int)((lo >> 8) & 0x0ff)]) + sbox4[(int)(lo & 0x0ff)]) ^ pbox06;
            lo ^= (((sbox1[(int)(hi >> 24)] + sbox2[(int)((hi >> 16) & 0x0ff)]) ^ sbox3[(int)((hi >> 8) & 0x0ff)]) + sbox4[(int)(hi & 0x0ff)]) ^ pbox07;
            hi ^= (((sbox1[(int)(lo >> 24)] + sbox2[(int)((lo >> 16) & 0x0ff)]) ^ sbox3[(int)((lo >> 8) & 0x0ff)]) + sbox4[(int)(lo & 0x0ff)]) ^ pbox08;
            lo ^= (((sbox1[(int)(hi >> 24)] + sbox2[(int)((hi >> 16) & 0x0ff)]) ^ sbox3[(int)((hi >> 8) & 0x0ff)]) + sbox4[(int)(hi & 0x0ff)]) ^ pbox09;
            hi ^= (((sbox1[(int)(lo >> 24)] + sbox2[(int)((lo >> 16) & 0x0ff)]) ^ sbox3[(int)((lo >> 8) & 0x0ff)]) + sbox4[(int)(lo & 0x0ff)]) ^ pbox10;
            lo ^= (((sbox1[(int)(hi >> 24)] + sbox2[(int)((hi >> 16) & 0x0ff)]) ^ sbox3[(int)((hi >> 8) & 0x0ff)]) + sbox4[(int)(hi & 0x0ff)]) ^ pbox11;
            hi ^= (((sbox1[(int)(lo >> 24)] + sbox2[(int)((lo >> 16) & 0x0ff)]) ^ sbox3[(int)((lo >> 8) & 0x0ff)]) + sbox4[(int)(lo & 0x0ff)]) ^ pbox12;
            lo ^= (((sbox1[(int)(hi >> 24)] + sbox2[(int)((hi >> 16) & 0x0ff)]) ^ sbox3[(int)((hi >> 8) & 0x0ff)]) + sbox4[(int)(hi & 0x0ff)]) ^ pbox13;
            hi ^= (((sbox1[(int)(lo >> 24)] + sbox2[(int)((lo >> 16) & 0x0ff)]) ^ sbox3[(int)((lo >> 8) & 0x0ff)]) + sbox4[(int)(lo & 0x0ff)]) ^ pbox14;
            lo ^= (((sbox1[(int)(hi >> 24)] + sbox2[(int)((hi >> 16) & 0x0ff)]) ^ sbox3[(int)((hi >> 8) & 0x0ff)]) + sbox4[(int)(hi & 0x0ff)]) ^ pbox15;
            hi ^= (((sbox1[(int)(lo >> 24)] + sbox2[(int)((lo >> 16) & 0x0ff)]) ^ sbox3[(int)((lo >> 8) & 0x0ff)]) + sbox4[(int)(lo & 0x0ff)]) ^ pbox16;

            lo ^= pbox17;

            dataOut[posOut + 3] = (byte)(lo >> 24);
            dataOut[posOut + 2] = (byte)(lo >> 16);
            dataOut[posOut + 1] = (byte)(lo >>  8);
            dataOut[posOut    ] = (byte) lo;

            dataOut[posOut + 7] = (byte)(hi >> 24);
            dataOut[posOut + 6] = (byte)(hi >> 16);
            dataOut[posOut + 5] = (byte)(hi >>  8);
            dataOut[posOut + 4] = (byte) hi;

            posOut += 8;
        }

        return count;
    }

如果我传递更长的数据,河豚工作正常,但我需要加密这个.我们发现了问题,但我的问题是:如何解决它?!



1> dustyburwell..:

我不太熟悉Blowfish算法,或者就此而言,你正在使用的库.但是,一般情况下,我会说,如果您的数据问题不够长,无法加密,您可以填写它.换句话说......如果数据不够长,在加密数据之前,在数据的开头或结尾添加一堆零(或其他一些字符,如果零对您有特殊意义).然后,当您解密它时,修剪填充字符.

推荐阅读
手机用户2502851955
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有