/// <summary> /// DES主要加密方法 输入输出64位 /// </summary> /// <param name="qwPlain">64bit明文</param> /// <param name="qwKey">64bit密钥</param> /// <returns>64bit密文</returns> public static QWORD Encrypt(QWORD qwPlain, QWORD qwKey) { QWORD L0R0 = DES_Table.Swap_IP(qwPlain); QWORD T = DES_Table.Swap_PC1(qwKey); DWORD C = (DWORD)((T & 0xFFFFFFF000000000) >> 32); DWORD D = (DWORD)((T & 0x0000000FFFFFFF00) >> 4); DWORD L = (DWORD)(L0R0 >> 32); DWORD R = (DWORD)(L0R0); int[] shiftTable = { 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 }; for (int round = 0; round < 16; ++round) { C = DES_Convert.RoundShiftLeft28(C, shiftTable[round]); D = DES_Convert.RoundShiftLeft28(D, shiftTable[round]); T = ((QWORD)(C & 0xFFFFFFF0) << 32) | ((QWORD)D << 4); QWORD Ki = DES_Table.Swap_PC2(T); DWORD temp = R; R = DES_Table.Swap_P(DES_Table.Swap_S(DES_Table.Expand_R(R) ^ Ki)) ^ L; L = temp; } QWORD R16L16 = ((QWORD)R << 32) | (QWORD)L; QWORD qwCipher = DES_Table.Swap_T_IP(R16L16); return(qwCipher); }
/// <summary> /// DES分组解密方法 输入byte数组 返回byte数组 /// </summary> /// <param name="baCipher">明文byte数组</param> /// <param name="qwKey">64bit密钥</param> /// <param name="qwIV">64bit初始化向量,ECB模式无效</param> /// <param name="mode">操作模式</param> /// <returns>密文byte数组</returns> public static byte[] DecryptBlockB(byte[] baCipher, QWORD qwKey, QWORD qwIV, Mode mode) { if (mode == Mode.CFB) { return(DES_Convert.QWORDToBytes_CFB(DecryptBlockQ(baCipher, qwKey, qwIV, mode))); } return(DES_Convert.QWORDToBytes(DecryptBlockQ(baCipher, qwKey, qwIV, mode))); }
/// <summary> /// DES分组加密方法 输入byte数组 返回byte数组 /// </summary> /// <param name="baPlain">输入byte数组</param> /// <param name="qwKey">64bit密钥</param> /// <param name="qwIV">64bit初始化向量,ECB模式无效</param> /// <param name="mode">操作模式</param> /// <returns>密文byte数组</returns> public static byte[] EncryptBlockB(byte[] baPlain, QWORD qwKey, QWORD qwIV, Mode mode) { if (mode == Mode.CFB) { return(DES_Convert.QWORDToBytes_CFB(EncryptBlockQ(baPlain, qwKey, qwIV, mode))); } return(DES_Convert.QWORDToBytes(EncryptBlockQ(baPlain, qwKey, qwIV, mode))); }
/// <summary> /// DES主要解密方法 输入输出64位 /// </summary> /// <param name="qwCipher">64bit密文</param> /// <param name="qwKey">64bit密钥</param> /// <returns>64bit明文</returns> public static QWORD Decrypt(QWORD qwCipher, QWORD qwKey) { QWORD L16R16 = DES_Table.Swap_IP(qwCipher); DWORD L = (DWORD)L16R16; DWORD R = (DWORD)(L16R16 >> 32); QWORD T = DES_Table.Swap_PC1(qwKey); DWORD C = (DWORD)((T & 0xFFFFFFF000000000) >> 32); DWORD D = (DWORD)((T & 0x0000000FFFFFFF00) >> 4); int[] shiftTable = { 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 }; List <QWORD> lsKey = new List <QWORD>(); for (int round = 0; round < 16; ++round) { C = DES_Convert.RoundShiftLeft28(C, shiftTable[round]); D = DES_Convert.RoundShiftLeft28(D, shiftTable[round]); T = ((QWORD)(C & 0xFFFFFFF0) << 32) | ((QWORD)D << 4); lsKey.Add(DES_Table.Swap_PC2(T)); } for (int round = 15; round >= 0; --round) { DWORD temp = R; R = L; L = DES_Table.Swap_P(DES_Table.Swap_S(DES_Table.Expand_R(R) ^ lsKey[round])) ^ temp; } QWORD L0R0 = ((QWORD)L << 32) | (QWORD)R; QWORD qwPlain = DES_Table.Swap_T_IP(L0R0); return(qwPlain); }