WEB开发网
开发学院软件开发C语言 分组密码SAFER+的C#实现 阅读

分组密码SAFER+的C#实现

 2009-02-27 08:18:34 来源:WEB开发网   
核心提示: 注意程序的第一句,此处不能对表达式作0xFF的与操作,分组密码SAFER+的C#实现(8),否则运行结果不对,这里笔者百思不解,否则应将二者之差加上256,然后将整个结果与0xFF做与(&)操作,笔者认为,从input[0] ^ n_LocKeyExpandBox[16 * m_nChip

注意程序的第一句,此处不能对表达式作0xFF的与操作,否则运行结果不对,这里笔者百思不解。笔者认为,从input[0] ^ n_LocKeyExpandBox[16 * m_nChipherLen]本身来讲,其结果经强制类型转换成byte后就已经保证了结果的高位为零,从而满足模256运算的需要。

最后将input置入输出数组output中,同样的,与明文m_sInClearText置入byte型的字节数组input中相似,output数组的偶数位下标,即output[0],output[2],……,output[30]中均为密文数据,而奇数位下标,即output[1],output[3],……,output[31]均为0,从而保证byte型数组能转换成string型密文m_sCryptedText。

解密算法

该算法其实就是简单地将加密操作逆向进行,首先进行初始变换,即加密算法输出变换的逆向操作,因为对于模256“—”运算而已言,其结果有可能小于0,因而此处要采用三目运算符 ?:来进行表达式结果判断,例如对input[2]而言,若input[2] - n_LocKeyExpandBox[16 * m_nChipherLen + 1]大于等于零,则结果为其自身,否则应将二者之差加上256,然后将整个结果与0xFF做与(&)操作,从而保证模256“—”运算的正确性,代码如下:

input[0] = (byte)((input[0] ^ n_LocKeyExpandBox[16 * m_nChipherLen]) & 0xFF);

 

                input[2] = (byte)(((input[2] - n_LocKeyExpandBox[16 * m_nChipherLen + 1]) >= 0 ? (input[2] - n_LocKeyExpandBox[16 * m_nChipherLen + 1]) : (input[2] - n_LocKeyExpandBox[16 * m_nChipherLen + 1] + 256)) & 0xFF);

                input[4] = (byte)(((input[4] - n_LocKeyExpandBox[16 * m_nChipherLen + 2]) >= 0 ? (input[4] - n_LocKeyExpandBox[16 * m_nChipherLen + 2]) : (input[4] - n_LocKeyExpandBox[16 * m_nChipherLen + 2] + 256)) & 0xFF);

               input[6] = (byte)((input[6] ^ n_LocKeyExpandBox[16 * m_nChipherLen + 3]) & 0xFF);

input[8] = (byte)((input[8] ^ n_LocKeyExpandBox[16 * m_nChipherLen + 4]) & 0xFF);

 input[10] = (byte)(((input[10] - n_LocKeyExpandBox[16 * m_nChipherLen + 5]) >= 0 ? (input[10] - n_LocKeyExpandBox[16 * m_nChipherLen + 5]) : (input[10] - n_LocKeyExpandBox[16 * m_nChipherLen + 5] + 256)) & 0xFF);

                input[12] = (byte)(((input[12] - n_LocKeyExpandBox[16 * m_nChipherLen + 6]) >= 0 ? (input[12] - n_LocKeyExpandBox[16 * m_nChipherLen + 6]) : (input[12] - n_LocKeyExpandBox[16 * m_nChipherLen + 6] + 256)) & 0xFF);

                input[14] = (byte)((input[14] ^ n_LocKeyExpandBox[16 * m_nChipherLen + 7]) & 0xFF);

input[16] = (byte)((input[16] ^ n_LocKeyExpandBox[16 * m_nChipherLen + 8]) & 0xFF);

input[18] = (byte)(((input[18] - n_LocKeyExpandBox[16 * m_nChipherLen + 9]) >= 0 ? (input[18] - n_LocKeyExpandBox[16 * m_nChipherLen + 9]) : (input[18] - n_LocKeyExpandBox[16 * m_nChipherLen + 9] + 256)) & 0xFF);

                input[20] = (byte)(((input[20] - n_LocKeyExpandBox[16 * m_nChipherLen + 10]) >= 0 ? (input[20] - n_LocKeyExpandBox[16 * m_nChipherLen + 10]) : (input[20] - n_LocKeyExpandBox[16 * m_nChipherLen + 10] + 256)) & 0xFF);

                input[22] = (byte)((input[22] ^ n_LocKeyExpandBox[16 * m_nChipherLen + 11]) & 0xFF);

input[24] = (byte)((input[24] ^ n_LocKeyExpandBox[16 * m_nChipherLen + 12]) & 0xFF);

input[26] = (byte)(((input[26] - n_LocKeyExpandBox[16 * m_nChipherLen + 13]) >= 0 ? (input[26] - n_LocKeyExpandBox[16 * m_nChipherLen + 13]) : (input[26] - n_LocKeyExpandBox[16 * m_nChipherLen + 13] + 256)) & 0xFF);

                input[28] = (byte)(((input[28] - n_LocKeyExpandBox[16 * m_nChipherLen + 14]) >= 0 ? (input[28] - n_LocKeyExpandBox[16 * m_nChipherLen + 14]) : (input[28] - n_LocKeyExpandBox[16 * m_nChipherLen + 14] + 256)) & 0xFF);

                input[30] = (byte)((input[30] ^ n_LocKeyExpandBox[16 * m_nChipherLen + 15]) & 0xFF);

上一页  3 4 5 6 7 8 9 10  下一页

Tags:分组 密码 SAFER

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接