MAC加密算法·银联标准 C语言实现

这几天因为项目的关系需要用到。以下的des算法和mac算法都来源于网络,我修改了下输入输出格式、改掉了原来mac算法里面的一个错误。

需要特别注意的是,des算法有很多种,不同实现方法的结果可能不一样。在我调试的时候一开始找到的是不合适的des加密算法,最终结果一直不正确。

以下是mac加密中要调用的8位des加密算法:

//调试环境:ubuntu14.04+Eclipse Kepler+cdt8.2.1
#include <stdio.h>

#define EN0 0   // MODE == encrypt
#define DE1 1   // MODE == decrypt

typedef union {
    unsigned long blok[2];
    unsigned short word[4];
    unsigned char byte[8];
    } M68K;

extern void des8(unsigned char *InData,unsigned char *key,unsigned char *OutData,short Mode,int readlen);
extern void des16(unsigned char *InData,unsigned char *key,unsigned char *OutData,short Mode,int readlen);


static void scrunch(unsigned char *, unsigned long *);
static void unscrun(unsigned long *, unsigned char *);
static void desfunc(unsigned long *, unsigned long *);
static void cookey(unsigned long *);

static unsigned long KnL[32] = { 0L };
static unsigned long KnR[32] = { 0L };
static unsigned long Kn3[32] = { 0L };
static unsigned char Df_Key[24] = {
    0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
    0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10,
    0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67 };

static unsigned short bytebit[8]    = {
    0200, 0100, 040, 020, 010, 04, 02, 01 };

static unsigned long bigbyte[24] = {
    0x800000L,  0x400000L,  0x200000L,  0x100000L,
    0x80000L,   0x40000L,   0x20000L,   0x10000L,
    0x8000L,    0x4000L,    0x2000L,    0x1000L,
    0x800L,     0x400L,     0x200L,     0x100L,
    0x80L,      0x40L,      0x20L,      0x10L,
    0x8L,       0x4L,       0x2L,       0x1L    };

static unsigned char pc1[56] = {
    56, 48, 40, 32, 24, 16,  8,  0, 57, 49, 41, 33, 25, 17,
     9,  1, 58, 50, 42, 34, 26, 18, 10,  2, 59, 51, 43, 35,
    62, 54, 46, 38, 30, 22, 14,  6, 61, 53, 45, 37, 29, 21,
    13,  5, 60, 52, 44, 36, 28, 20, 12,  4, 27, 19, 11,  3 };

static unsigned char totrot[16] = {
    1,2,4,6,8,10,12,14,15,17,19,21,23,25,27,28 };

static unsigned char pc2[48] = {
    13, 16, 10, 23,  0,  4,  2, 27, 14,  5, 20,  9,
    22, 18, 11,  3, 25,  7, 15,  6, 26, 19, 12,  1,
    40, 51, 30, 36, 46, 54, 29, 39, 50, 44, 32, 47,
    43, 48, 38, 55, 33, 52, 45, 41, 49, 35, 28, 31 };

void deskey(unsigned char * key,short edf)
{
    register int i, j, l, m, n;
    unsigned char pc1m[56], pcr[56];
    unsigned long kn[32];

    for ( j = 0; j < 56; j++ ) {
        l = pc1[j];
        m = l & 07;
        pc1m[j] = (key[l >> 3] & bytebit[m]) ? 1 : 0;
        }
    for( i = 0; i < 16; i++ ) {
        if( edf == DE1 ) m = (15 - i) << 1;
        else m = 
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
SPN加密算法(Substitution-Permutation Network)是一种分组加密算法,它由替换和置换两个步骤组成。下面是SPN加密算法C语言实现: ```c #include <stdio.h> #define SBOX_SIZE 16 // S-盒的大小 #define NUM_ROUNDS 4 // 加密轮数 // S-盒的定义 int sBox[SBOX_SIZE] = {0x9, 0x4, 0xa, 0xb, 0xd, 0x1, 0x8, 0x5, 0x6, 0x2, 0x0, 0x3, 0xc, 0xe, 0xf, 0x7}; // 加密函数 int encrypt(int plaintext, int key) { int i, j; int cipher = plaintext ^ key; // 异或运算 for (i = 0; i < NUM_ROUNDS; i++) { // 替换步骤 int sBoxInput = 0; for (j = 0; j < SBOX_SIZE; j++) { if ((cipher >> j) & 1) { sBoxInput ^= j; } } int sBoxOutput = sBox[sBoxInput]; // 置换步骤 int permutationOutput = 0; for (j = 0; j < SBOX_SIZE; j++) { permutationOutput ^= ((sBoxOutput >> j) & 1) << ((4 * j) % SBOX_SIZE); } cipher = permutationOutput; } return cipher; } int main() { int plaintext = 0x8; // 明文 int key = 0x3; // 密钥 int ciphertext = encrypt(plaintext, key); printf("Ciphertext: 0x%x\n", ciphertext); return 0; } ``` 以上是SPN加密算法C语言实现示例。具体实现中,使用了一个S-盒作为替代步骤,并进行了4轮的替代和置换操作。在 `encrypt` 函数中,首先将明文和密钥进行异或运算得到初始密文,在每一轮中依次进行替代和置换操作得到下一轮的密文。最后得到的密文即为加密结果。 在示例中,明文和密钥分别使用了十六进制表示法,可以根据需要进行修改。运行程序后,将输出加密结果的十六进制表示。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值