博客
关于我
PBOC圈存时用到3DES加密解密以及MAC计算方法
阅读量:428 次
发布时间:2019-03-06

本文共 3826 字,大约阅读时间需要 12 分钟。

3DES加密解密与MAC计算总结

在进行PBOC圈存时,我使用了3DES加密解密以及MAC计算。这部分内容对我来说相当重要,但由于没有深入研究,只是通过复制和学习他人代码来掌握相关知识。以下是我总结和整理后的内容,便于以后使用和学习。

3DES加密与解密

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(密文认证码)是一种常用的数据完整性验证方法。在这个项目中,我使用了一个自定义的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加密

    • 将密钥和明文转换为字节数组。
    • 初始化TripleDESCryptoServiceProvider,设置加密模式为ECB。
    • 使用指定的密钥和初始向量进行加密。
    • 将加密结果转换为十六进制字符串。
  • 3DES解密

    • 使用相同的密钥和初始向量进行解密。
    • 将解密结果转换为十六进制字符串。
  • MAC计算

    • 初始化初始数据和密文。
    • 对每个块数据进行加密和解密,生成最终的MAC数据。
    • 将MAC数据转换为十六进制字符串。
  • 在整个过程中,我参考了他人代码,逐步理解和修改,确保实现的正确性和安全性。通过实际项目的应用,我对3DES加密解密以及MAC计算有了更深入的了解,同时也掌握了相关代码的编写和优化技巧。

    转载地址:http://cxzuz.baihongyu.com/

    你可能感兴趣的文章
    Objective-C实现Julia集算法(附完整源码)
    查看>>
    Objective-C实现jump search跳转搜索算法(附完整源码)
    查看>>
    Objective-C实现jumpSearch跳转搜索算法(附完整源码)
    查看>>
    Objective-C实现k nearest neighbours k最近邻分类算法(附完整源码)
    查看>>
    Objective-C实现k-means clustering均值聚类算法(附完整源码)
    查看>>
    Objective-C实现k-Means算法(附完整源码)
    查看>>
    Objective-C实现k-nearest算法(附完整源码)
    查看>>
    Objective-C实现KadaneAlgo计算给定数组的最大连续子数组和算法(附完整源码)
    查看>>
    Objective-C实现kahns algorithm卡恩算法(附完整源码)
    查看>>
    Objective-C实现karatsuba大数相乘算法(附完整源码)
    查看>>
    Objective-C实现karger算法(附完整源码)
    查看>>
    Objective-C实现KMP搜索算法(附完整源码)
    查看>>
    Objective-C实现Knapsack problem背包问题算法(附完整源码)
    查看>>
    Objective-C实现knapsack背包问题算法(附完整源码)
    查看>>
    Objective-C实现knapsack背包问题算法(附完整源码)
    查看>>
    Objective-C实现knight tour骑士之旅算法(附完整源码)
    查看>>
    Objective-C实现knight Tour骑士之旅算法(附完整源码)
    查看>>
    Objective-C实现KNN算法(附完整源码)
    查看>>
    Objective-C实现KNN算法(附完整源码)
    查看>>
    Objective-C实现KNN算法(附完整源码)
    查看>>