本文共 3826 字,大约阅读时间需要 12 分钟。
在进行PBOC圈存时,我使用了3DES加密解密以及MAC计算。这部分内容对我来说相当重要,但由于没有深入研究,只是通过复制和学习他人代码来掌握相关知识。以下是我总结和整理后的内容,便于以后使用和学习。
3DES是一种基于对称密钥加密的加密方法,分为双倍长和三倍长两种模式。在我的项目中,我使用了双倍长的3DES加密,采用ECB(电子锁车锁)加密模式。代码如下:
using System;using System.Security.Cryptography;using System.Text;public static class EncryptUtils{ private static SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider(); private static byte[] IV = { 0xB0, 0xA2, 0xB8, 0xA3, 0xDA, 0xCC, 0xDA, 0xCC }; public static string Encrypt3DES(string strKey, string strValue) { byte[] key = TransTools.strToToHexByte(strKey); byte[] value = TransTools.strToToHexByte(strValue); TripleDESCryptoServiceProvider tdsc = new TripleDESCryptoServiceProvider(); tdsc.KeySize = 128; tdsc.Key = key; tdsc.Mode = CipherMode.ECB; tdsc.Padding = PaddingMode.None; ICryptoTransform ct = tdsc.CreateEncryptor(); byte[] data = ct.TransformFinalBlock(value, 0, 16); string results = string.Join("", data.Select(b => b.ToString("X2"))); return results; } public static string Decrypt3DES(string strKey, string strValue) { byte[] key = TransTools.strToToHexByte(strKey); byte[] value = TransTools.strToToHexByte(strValue); TripleDESCryptoServiceProvider tdsc = new TripleDESCryptoServiceProvider(); tdsc.KeySize = 128; tdsc.Key = key; tdsc.Mode = CipherMode.ECB; tdsc.Padding = PaddingMode.None; ICryptoTransform ct = tdsc.CreateDecryptor(); byte[] data = ct.TransformFinalBlock(value, 0, 16); string results = string.Join("", data.Select(b => b.ToString("X2"))); return results; }} MAC(密文认证码)是一种常用的数据完整性验证方法。在这个项目中,我使用了一个自定义的MAC计算算法,代码如下:
public class Mac3Encrypt{ private static int[] iSelePM1 = { 57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18, 10, 2, 59, 51, 43, 35, 27, 19, 11, 3, 60, 52, 44, 36, 63, 55, 47, 39, 31, 23, 15, 7, 62, 54, 46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4 }; private static int[] iSelePM2 = { 14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10, 23, 19, 12, 4, 26, 8, 16, 7, 27, 20, 13, 2, 41, 52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48, 44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32 }; private static int[] iROLtime = { 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 }; // 其他初始置换、逆置换、选择运算矩阵等定义类似上述方式。 private int[] iCipherKey = new int[64]; private int[] iCKTemp = new int[56]; private int[] iPlaintext = new int[64]; private int[] iCiphertext = new int[64]; private int[] iPKTemp = new int[64]; private int[] iL = new int[32]; private int[] iR = new int[32]; private void permu(int[] iSource, int[] iDest, int[] iPM) { if (iDest == null) iDest = new int[iPM.Length]; for (int i = 0; i < iPM.Length; i++) iDest[i] = iSource[iPM[i] - 1]; } // 其他辅助方法如arrayBitToI、arrayIToBit、arrayM2Add等定义如下。 public byte[] MAC16(int[][] iSubKeys1, int[][] iSubKeys2, byte[] bInit, byte[] bCiphertext) { // 代码逻辑较为复杂,涉及多个步骤和子函数,具体实现请参考完整代码。 } public String Str3MAC(String strKey, String strInitData, String strMacData) { // 代码逻辑较为复杂,涉及多个步骤和子函数,具体实现请参考完整代码。 } public String calculatorMac(String communicationKey, String strData) { return this.Str3MAC(communicationKey, "0000000000000000", strData); } public String StrDe3DES(String strKey, String strEncData) { // 代码逻辑较为复杂,涉及多个步骤和子函数,具体实现请参考完整代码。 }} 在实际项目中,我主要使用了以下步骤进行3DES加密与解密以及MAC计算:
3DES加密:
3DES解密:
MAC计算:
在整个过程中,我参考了他人代码,逐步理解和修改,确保实现的正确性和安全性。通过实际项目的应用,我对3DES加密解密以及MAC计算有了更深入的了解,同时也掌握了相关代码的编写和优化技巧。
转载地址:http://cxzuz.baihongyu.com/