LoadRunner实现DES加密(一)

LoadRunner实现DES加密


公司有一款产品,对登录这块做了加密,用户进入登录页面,会被分配一个key(16位),用户输入完用户名密码,点击登录后,前台页面js会用des将输入的密码和key进行加密,将加密后的结果传给服务端。使用LoadRunner对登录进行测试,需要有des加密方法对动态的key和密码进行加密。

加密方法的介绍


开发的代码主要参考:http://ahomeeye.iteye.com/blog/2310049
可以实现三重DES加密,而我们的产品只加密了一次。
加密算法的原理参考:http://blog.csdn.net/fenghao_5555/article/details/1589464
这里大概描述下处理的过程:
先将16位的key扩展成一个4*64的二维数组key[ 4 ] [64](即每4个字符转换成一个64位二进制数组),将要加密的data,取前4位字符,扩展成64位二进制数组,把data数组和key[ 1 ]数组经过一系列转换(上面参考的加密算法),得到一个新的64位data,再与key[ 2 ]数组进行上述转换,四轮转换后,得到的data即为加密后的数据,将该加密后的64位数组转换成16位的字符串。这样就对原始的data前4位字符进行了加密,再循环取4位进行加密(不足4位进行扩展),直到加密完所有的data数据,加密结束。

C语言实现

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int finallyPermute(int endByte[64],int fpByte[64]);
int generateKeys(int keys[64],int newkeys[16][48]);
int initPermute(int data[64],int ipByte[64]);
int expandPermute(int rightData[32],int epByte[48]);
int xor(int byteone[48],int bytetwo[48],int xorByte[48]);
int sBoxPermute(int expandByte[48],int sBoxByte[32]);
int pPermute(int sBoxByte[32],int pBoxPermute[32]);

int ByteToBit(char ch, int bit[8]) {//字符转成二进制数组
    int cnt;
    int i;
    for (cnt = 0, i = 7; cnt < 8; cnt++, i--) {
        *(bit + i) = (int) (ch >> cnt) & 1;
    }
    return 0;
}
char BitToByte(int bit[4], char *chs) {//二进制数组转成字符
    int cnt;
    cnt = (int) bit[0] * 1000 + (int) bit[1] * 100 + (int) bit[2] * 10 + (int) bit[3];
    switch (cnt) {
    case 0:
        *chs = '0';
        break;
    case 1:
        *chs = '1';
        break;
    case 10:
        *chs = '2';
        break;
    case 11:
        *chs = '3';
        break;
    case 100:
        *chs = '4';
        break;
    case 101:
        *chs = '5';
        break;
    case 110:
        *chs = '6';
        break;
    case 111:
        *chs = '7';
        break;
    case 1000:
        *chs = '8';
        break;
    case 1001:
        *chs = '9';
        break;
    case 1010:
        *chs = 'A';
        break;
    case 1011:
        *chs = 'B';
        break;
    case 1100:
        *chs = 'C';
        break;
    case 1101:
        *chs = 'D';
        break;
    case 1110:
        *chs = 'E';
        break;
    case 1111:
        *chs = 'F';
        break;
    }
    return 0;
}
int Char4ToBit64(char *ch, int bit[64]) {//4位字符转成64位数组
    int cnt;
    int i;
    int chLen=strlen(ch);
    if (chLen<4) {//不满4位,进行补位
        for (cnt = 0; cnt < chLen; cnt++) {
            for (i = 0; i < 8; i++) {
                *(bit + (cnt << 3) + i) = 0;
            }
        }
        for (cnt = 0; cnt < chLen; cnt++) {
            ByteToBit(*(ch + cnt), bit + 8 + (cnt << 4));
        }
        for (cnt = chLen; cnt < 4; cnt++) {
            for (i = 0; i < 16; i++) {
                *(bit + (cnt << 4) + i) = 0;
            }
        }

    } else {
        for (cnt = 0; cnt < 4; cnt++) {
            for (i = 0; i < 8; i++) {
                *(bit + (cnt << 4) + i) = 0;
            }
        }
        for (cnt = 0; cnt < 4; cnt++) {
            ByteToBit(*(ch + cnt), bit + 8 + (cnt << 4));
        }
    }
    return 0;
}
int Bit64ToChar16(int bit[64], char ch[16]) {//64位二进制数组转成16位字符
    int cnt;
    //memset(ch,0,8);
    for (cnt = 0; cnt < 16; cnt++) {
        BitToByte(bit + (cnt * 4), ch + cnt);
    }
    return 0;
}

int enc(int data[64],int key[64],int fpByte[64]){//加密
    int i = 0, j = 0, k = 0, m = 0, n = 0;
    int ipLeft[32],ipRight[32],tempLeft[32];
    int keys[16][48];
    int ipByte[64],finalData[64],newkey[48],epByte[48],xorByte[48],sBoxByte[32],pBoxPermute[32],tempRight[32];
    generateKeys(key,keys);
    initPermute(data,ipByte);
    for (k = 0; k < 32; k++) {
        ipLeft[k] = ipByte[k];
        ipRight[k] = ipByte[32 + k];
    }

    for (i = 0; i < 16; i++) {
        for (j = 0; j < 32; j++) {
            tempLeft[j] = ipLeft[j];
            ipLeft[j] = ipRight[j];
        }
        for (m = 0; m < 48; m++) {
            newkey[m] = keys[i][m];
        }
        expandPermute(ipRight,epByte);

        xor(epByte,newkey,xorByte);

        sBoxPermute(xorByte,sBoxByte);

        pPermute(sBoxByte,pBoxPermute);

        xor(pBoxPermute,tempLeft,tempRight);

        for (n = 0; n < 32; n++) {
            ipRight[n] = tempRight[n];
        }
    }
    for (i = 0; i < 32; i++) {
        finalData[i] = ipRight[i];
        finalData[32 + i] = ipLeft[i];
    }
    finallyPermute(finalData,fpByte);
    return 0;
}

int finallyPermute(int endByte[64],int fpByte[64]){
            fpByte[0] = endByte[39];
            fpByte[1] = endByte[7];
            fpByte[2] = endByte[47];
            fpByte[3] = endByte[15];
            fpByte[4] = endByte[55];
            fpByte[5] = endByte[23];
            fpByte[6] = endByte[63];
            fpByte[7] = endByte[31];
            fpByte[8] = endByte[38];
            fpByte[9] = endByte[6];
            fpByte[10] = endByte[46];
            fpByte[11] = endByte[14];
            fpByte[12] = endByte[54];
            fpByte[13] = endByte[22];
            fpByte[14] = endByte[62];
            fpByte[15] = endByte[30];
            fpByte[16] = endByte[37];
            fpByte[17] = endByte[5];
            fpByte[18] = endByte[45];
            fpByte[19] = endByte[13];
            fpByte[20] = endByte[53];
            fpByte[21] = endByte[21];
            fpByte[22] = endByte[61];
            fpByte[23] = endByte[29];
            fpByte[24] = endByte[36];
            fpByte[25] = endByte[4];
            fpByte[26] = endByte[44];
            fpByte[27] = endByte[12];
            fpByte[28] = endByte[52];
            fpByte[29] = endByte[20];
            fpByte[30] = endByte[60];
            fpByte[31] = endByte[28];
            fpByte[32] = endByte[35];
            fpByte[33] = endByte[3];
            fpByte[34] = endByte[43];
            fpByte[35] = endByte[11];
            fpByte[36] = endByte[51];
            fpByte[37] = endByte[19];
            fpByte[38] = endByte[59];
            fpByte[39] = endByte[27];
            fpByte[40] = endByte[34];
            fpByte[41] = endByte[2];
            fpByte[42] = endByte[42];
            fpByte[43] = endByte[10];
            fpByte[44] = endByte[50];
            fpByte[45] = endByte[18];
            fpByte[46] = endByte[58];
            fpByte[47] = endByte[26];
            fpByte[48] = endByte[33];
            fpByte[49] = endByte[1];
            fpByte[50] = endByte[41];
            fpByte[51] = endByte[9];
            fpByte[52] = endByte[49];
            fpByte[53] = endByte[17];
            fpByte[54] = endByte[57];
            fpByte[55] = endByte[25];
            fpByte[56] = endByte[32];
            fpByte[57] = endByte[0];
            fpByte[58] = endByte[40];
            fpByte[59] = endByte[8];
            fpByte[60] = endByte[48];
            fpByte[61] = endByte[16];
            fpByte[62] = endByte[56];
            fpByte[63] = endByte[24];
        return 0;
}

int pPermute(int sBoxByte[32],int pBoxPermute[32]){
    pBoxPermute[0] = sBoxByte[15];
            pBoxPermute[1] = sBoxByte[6];
            pBoxPermute[2] = sBoxByte[19];
            pBoxPermute[3] = sBoxByte[20];
            pBoxPermute[4] = sBoxByte[28];
            pBoxPermute[5] = sBoxByte[11];
            pBoxPermute[6] = sBoxByte[27];
            pBoxPermute[7] = sBoxByte[16];
            pBoxPermute[8] = sBoxByte[0];
            pBoxPermute[9] = sBoxByte[14];
            pBoxPermute[10] = sBoxByte[22];
            pBoxPermute[11] = sBoxByte[25];
            pBoxPermute[12] = sBoxByte[4];
            pBoxPermute[13] = sBoxByte[17];
            pBoxPermute[14] = sBoxByte[30];
            pBoxPermute[15] = sBoxByte[9];
            pBoxPermute[16] = sBoxByte[1];
            pBoxPermute[17] = sBoxByte[7];
            pBoxPermute[18] = sBoxByte[23];
            pBoxPermute[19] = sBoxByte[13];
            pBoxPermute[20] = sBoxByte[31];
            pBoxPermute[21] = sBoxByte[26];
            pBoxPermute[22] = sBoxByte[2];
            pBoxPermute[23] = sBoxByte[8];
            pBoxPermute[24] = sBoxByte[18];
            pBoxPermute[25] = sBoxByte[12];
            pBoxPermute[26] = sBoxByte[29];
            pBoxPermute[27] = sBoxByte[5];
            pBoxPermute[28] = sBoxByte[21];
            pBoxPermute[29] = sBoxByte[10];
            pBoxPermute[30] = sBoxByte[3];
            pBoxPermute[31] = sBoxByte[24];
        return 0;
}
int  getBoxBinary(int i,int binary[4]){
    switch (i) {
            case 0:
                binary[0] = 0;
                binary[1] = 0;
                binary[2] = 0;
                binary[3] = 0;
                break;
            case 1:
                binary[0] = 0;
                binary[1] = 0;
                binary[2] = 0;
                binary[3] = 1;
                break;
            case 2:
                binary[0] = 0;
                binary[1] = 0;
                binary[2] = 1;
                binary[3] = 0;
                break;
            case 3:
                binary[0] = 0;
                binary[1] = 0;
                binary[2] = 1;
                binary[3] = 1;
                break;
            case 4:
                binary[0] = 0;
                binary[1] = 1;
                binary[2] = 0;
                binary[3] = 0;
                break;
            case 5:
                binary[0] = 0;
                binary[1] = 1;
                binary[2] = 0;
                binary[3] = 1;
                break;
            case 6:
                binary[0] = 0;
                binary[1] = 1;
                binary[2] = 1;
                binary[3] = 0;
                break;
            case 7:
                binary[0] = 0;
                binary[1] = 1;
                binary[2] = 1;
                binary[3] = 1;
                break;
            case 8:
                binary[0] = 1;
                binary[1] = 0;
                binary[2] = 0;
                binary[3] = 0;
                break;
            case 9:
                binary[0] = 1;
                binary[1] = 0;
                binary[2] = 0;
                binary[3] = 1;;
                break;
            case 10:
                binary[0] = 1;
                binary[1] = 0;
                binary[2] = 1;
                binary[3] = 0;
                break;
            case 11:
                binary[0] = 1;
                binary[1] = 0;
                binary[2] = 1;
                binary[3] = 1;
                break;
            case 12:
                binary[0] = 1;
                binary[1] = 1;
                binary[2] = 0;
                binary[3] = 0;
                break;
            case 13:
                binary[0] = 1;
                binary[1] = 1;
                binary[2] = 0;
                binary[3] = 1;
                break;
            case 14:
                binary[0] = 1;
                binary[1] = 1;
                binary[2] = 1;
                binary[3] = 0;
                break;
            case 15:
                binary[0] = 1;
                binary[1] = 1;
                binary[2] = 1;
                binary[3] = 1;
                break;
            }
    return 0;
}
int sBoxPermute(int expandByte[48],int sBoxByte[32]){
    int binary[4];
    int s1[4][16] = {
                    { 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7 },
                    { 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8 },
                    { 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0 },
                    { 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 } };
    int s2[4][16] = {
            { 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10 },
            { 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5 },
            { 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15 },
            { 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 } };
    int  s3[4][16] = {
            { 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8 },
            { 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1 },
            { 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7 },
            { 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 } };
    int s4[4][16] = {
            { 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15 },
            { 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9 },
            { 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4 },
            { 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 } };
    int s5[4][16] = {
            { 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9 },
            { 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6 },
            { 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14 },
            { 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 } };
    int s6[4][16] = {
            { 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11 },
            { 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8 },
            { 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6 },
            { 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 } };
    int s7[4][16] = {
            { 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1 },
            { 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6 },
            { 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2 },
            { 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 } };
    int s8[4][16] = {
            { 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7 },
            { 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2 },
            { 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8 },
            { 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 } };

    for (int m = 0; m < 8; m++) {
        int i = 0, j = 0;
        i = expandByte[m * 6 + 0] * 2 + expandByte[m * 6 + 5];
        j = expandByte[m * 6 + 1] * 2 * 2 * 2 + expandByte[m * 6 + 2] * 2
                * 2 + expandByte[m * 6 + 3] * 2 + expandByte[m * 6 + 4];
        switch (m) {
        case 0:
            getBoxBinary(s1[i][j],binary);
            break;
        case 1:
            getBoxBinary(s2[i][j],binary);
            break;
        case 2:
            getBoxBinary(s3[i][j],binary);
            break;
        case 3:
            getBoxBinary(s4[i][j],binary);
            break;
        case 4:
            getBoxBinary(s5[i][j],binary);
            break;
        case 5:
            getBoxBinary(s6[i][j],binary);
            break;
        case 6:
            getBoxBinary(s7[i][j],binary);
            break;
        case 7:
            getBoxBinary(s8[i][j],binary);
            break;
        }
        sBoxByte[m * 4 + 0] = binary[0];
        sBoxByte[m * 4 + 1] = binary[1];
        sBoxByte[m * 4 + 2] = binary[2];
        sBoxByte[m * 4 + 3] = binary[3];
    }
    return 0;
}


int xor(int byteone[48],int bytetwo[48],int xorByte[48]){
    for (int i = 0; i < 48; i++) {
        xorByte[i] = byteone[i] ^ bytetwo[i];
    }
    return 0;

}
int expandPermute(int rightData[32],int epByte[48]){
    int i;
    for (i = 0; i < 8; i++) {
        if (i == 0) {
            epByte[i * 6 + 0] = rightData[31];
        } else {
            epByte[i * 6 + 0] = rightData[i * 4 - 1];
        }
        epByte[i * 6 + 1] = rightData[i * 4 + 0];
        epByte[i * 6 + 2] = rightData[i * 4 + 1];
        epByte[i * 6 + 3] = rightData[i * 4 + 2];
        epByte[i * 6 + 4] = rightData[i * 4 + 3];
        if (i == 7) {
            epByte[i * 6 + 5] = rightData[0];
        } else {
            epByte[i * 6 + 5] = rightData[i * 4 + 4];
        }
    }
    return 0;
}
int generateKeys(int keys[64],int newkeys[16][48]){
    int key[56];
    int tempKey[48];
    int i,j,k,m;
    int loop[]={ 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 };
    for ( i = 0; i < 7; i++) {
        for ( j = 0, k = 7; j < 8; j++, k--) {
            key[i * 8 + j] = keys[8 * k + i];
        }
    }
    for (i = 0; i < 16; i++) {
        int tempLeft = 0;
        int tempRight = 0;
        for (int j = 0; j < loop[i]; j++) {
            tempLeft = key[0];
            tempRight = key[28];
            for (int k = 0; k < 27; k++) {
                key[k] = key[k + 1];
                key[28 + k] = key[29 + k];
            }
            key[27] = tempLeft;
            key[55] = tempRight;
        }
        tempKey[0] = key[13];
        tempKey[1] = key[16];
        tempKey[2] = key[10];
        tempKey[3] = key[23];
        tempKey[4] = key[0];
        tempKey[5] = key[4];
        tempKey[6] = key[2];
        tempKey[7] = key[27];
        tempKey[8] = key[14];
        tempKey[9] = key[5];
        tempKey[10] = key[20];
        tempKey[11] = key[9];
        tempKey[12] = key[22];
        tempKey[13] = key[18];
        tempKey[14] = key[11];
        tempKey[15] = key[3];
        tempKey[16] = key[25];
        tempKey[17] = key[7];
        tempKey[18] = key[15];
        tempKey[19] = key[6];
        tempKey[20] = key[26];
        tempKey[21] = key[19];
        tempKey[22] = key[12];
        tempKey[23] = key[1];
        tempKey[24] = key[40];
        tempKey[25] = key[51];
        tempKey[26] = key[30];
        tempKey[27] = key[36];
        tempKey[28] = key[46];
        tempKey[29] = key[54];
        tempKey[30] = key[29];
        tempKey[31] = key[39];
        tempKey[32] = key[50];
        tempKey[33] = key[44];
        tempKey[34] = key[32];
        tempKey[35] = key[47];
        tempKey[36] = key[43];
        tempKey[37] = key[48];
        tempKey[38] = key[38];
        tempKey[39] = key[55];
        tempKey[40] = key[33];
        tempKey[41] = key[52];
        tempKey[42] = key[45];
        tempKey[43] = key[41];
        tempKey[44] = key[49];
        tempKey[45] = key[35];
        tempKey[46] = key[28];
        tempKey[47] = key[31];
        switch (i) {
        case 0:
            for (m = 0; m < 48; m++) {
                newkeys[0][m] = tempKey[m];
            }
            break;
        case 1:
            for (m = 0; m < 48; m++) {
                newkeys[1][m] = tempKey[m];
            }
            break;
        case 2:
            for (m = 0; m < 48; m++) {
                newkeys[2][m] = tempKey[m];
            }
            break;
        case 3:
            for (m = 0; m < 48; m++) {
                newkeys[3][m] = tempKey[m];
            }
            break;
        case 4:
            for (m = 0; m < 48; m++) {
                newkeys[4][m] = tempKey[m];
            }
            break;
        case 5:
            for (m = 0; m < 48; m++) {
                newkeys[5][m] = tempKey[m];
            }
            break;
        case 6:
            for (m = 0; m < 48; m++) {
                newkeys[6][m] = tempKey[m];
            }
            break;
        case 7:
            for (m = 0; m < 48; m++) {
                newkeys[7][m] = tempKey[m];
            }
            break;
        case 8:
            for (m = 0; m < 48; m++) {
                newkeys[8][m] = tempKey[m];
            }
            break;
        case 9:
            for (m = 0; m < 48; m++) {
                newkeys[9][m] = tempKey[m];
            }
            break;
        case 10:
            for (m = 0; m < 48; m++) {
                newkeys[10][m] = tempKey[m];
            }
            break;
        case 11:
            for (m = 0; m < 48; m++) {
                newkeys[11][m] = tempKey[m];
            }
            break;
        case 12:
            for (m = 0; m < 48; m++) {
                newkeys[12][m] = tempKey[m];
            }
            break;
        case 13:
            for (m = 0; m < 48; m++) {
                newkeys[13][m] = tempKey[m];
            }
            break;
        case 14:
            for (m = 0; m < 48; m++) {
                newkeys[14][m] = tempKey[m];
            }
            break;
        case 15:
            for (m = 0; m < 48; m++) {
                newkeys[15][m] = tempKey[m];
            }
            break;
        }
    }
    return 0;
}

int initPermute(int data[64],int ipByte[64]){
    int i = 0, m = 1, n = 0, j, k;
            for (i = 0, m = 1, n = 0; i < 4; i++, m += 2, n += 2) {
                for (j = 7, k = 0; j >= 0; j--, k++) {
                    ipByte[i * 8 + k] = data[j * 8 + m];
                    ipByte[i * 8 + k + 32] = data[j * 8 + n];
                }
            }
    return 0;
}

int strEnc(char *data, char *key,char *result) {
    int i, j, z,_len;
    int frsitKeyBt[4][64];
    int databt[64],fpByte[64];
    char subChar[5] ;
    char res[17];
    int datalen = strlen(data);
    int iterator = datalen / 4;
    int remainder= datalen % 4;
    _len=iterator*16+1;
    if(remainder>0){
        _len+=16;
    }
    for (i = 0; i < 4; i++) {//将key转换成数组
        for (j = 0; j < 4; j++) {
            subChar[j] = *(key + (4 * i) + j);
        }
        subChar[4]='\0';
        Char4ToBit64(subChar, frsitKeyBt[i]);
    }
    for (z = 0; z < iterator; z++) {//循环将data进行加密
        for (j = 0; j < 4; j++) {
            subChar[j] = *(data + (4 * z) + j);
        }
        subChar[4] = '\0';
        Char4ToBit64(subChar, databt);
        for (i = 0; i < 4; i++) {
            enc(databt, frsitKeyBt[i], fpByte);
            for (j = 0; j < 64; j++) {
                databt[j]= fpByte[j];
            }
        }
        Bit64ToChar16(fpByte, res);
        res[16]='\0';
        if(z == 0) {
            strcpy(result, res);
        } else {
            strcat(result, res);
        }
    }
    result[_len-1]='\0';
    if(remainder>0){//加密最后不足4位字符的data
        for (i = 0; i <remainder; i++) {
                subChar[i] = *(data + (4 * iterator) + i);
        }
        subChar[remainder]='\0';
        Char4ToBit64(subChar, databt);
        for (i = 0; i < 4; i++) {
            enc(databt, frsitKeyBt[i], fpByte);
            for (j = 0; j < 64; j++) {
                databt[j]= fpByte[j];
            }
        }
        Bit64ToChar16(fpByte, res);
        res[16]='\0';
        if(iterator == 0) {
            strcpy(result, res);
        } else {
            strcat(result, res);
        }
    }
    for(i=0;i<strlen(result);i++){//打印加密结果
        printf("%c",result[i]);
    }
    return 0;
}

char *DES(char *data,char *key){
    int datalen = strlen(data);
    int iterator = datalen / 4; 
    int remainder= datalen % 4; 
    int _len=iterator*16;
    if(remainder>0){
        _len+=16;
    }
    char res[_len];
    strEnc(data,key,res);
    return res;
}
/*
int main() {//加密测试
    char *s2 = "admin12345";
    char *s4 = "cghlszd435sierau";
    //char res[49];
    DES(s2, s4);
    return 0;
}
*/

LoadRunner引用

写成DES.h文件,引入到LoadRunner中,进行加密测试。
global文件中引入
这里写图片描述
对DES.h文件进行处理
这里写图片描述
加密字符串进行测试
这里写图片描述
从上图可以看出,得到了加密结果。但是,加密比较耗时,一共用了30s,对于测试来说肯定不行,于是生成了一个dll文件,解决了耗时的问题,下篇再分享。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值