loadrunner调用base64加解密

在测试公司短信业务中遇到,短信验证的参数值传的是base64加密后的内容,在测试的时候最开始传的4位数字,遇到不成功最后查到的就是密文的问题。

loadrunner函数库中没有直接base64加密函数可以用,只能自己搞定。

base64加解密程序,保存为base64.h,将此文件放在脚本目录下


char base64encode_lut[] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/','='};

char base64decode_lut[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0,62, 0, 0, 0,63,52,53,54,55,56,57,58,59,60,61, 0, 0,0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25, 0, 0, 0, 0, 0, 0,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, };void base64encode(char *src, char *dest, int len){ int i=0, slen=strlen(src); for(i=0;i>0x2]; *(dest++)=base64encode_lut[(*src&0x3)<<0x4|(*(src+1)&0xF0)>>0x4]; *(dest++)=((i+1)>0x6]:'='; *(dest++)=((i+2)>0x4;

*(dest++)=(c3!=64)?((c2&0xF)<<0x4|(c3&0x3C)>>0x2):'/0';

*(dest++)=(c4!=64)?((c3&0x3)<<0x6|c4&0x3F):'/0';

}

*dest='/0';

}

int b64_encode_string( char *source, char *lrvar )

{

int dest_size;

int res;

char *dest;

dest_size = 1 + ((strlen(source)+2)/3*4);

dest = (char *)malloc(dest_size);

memset(dest,0,dest_size);

base64encode(source, dest, dest_size);

lr_save_string( dest, lrvar );

res = strlen(dest);

free(dest);

return res;

}

int b64_decode_string( char *source, char *lrvar )

{

int dest_size;

int res;

char *dest;

dest_size = strlen(source);

dest = (char *)malloc(dest_size);

memset(dest,0,dest_size);

base64decode(source, dest, dest_size);

lr_save_string( dest, lrvar );

res = strlen(dest);

free(dest);

return res;

}


loadrunner中调用怎么写:

#include "base64.h"

Action()

{

int res;

lr_save_string(",你好!","Str");

//lr_save_string("8645","Str");

b64_encode_string( lr_eval_string("{Str}"), "B64str" );// 编码

lr_output_message("************编码结果: %s*************", lr_eval_string("{B64str}") );

b64_decode_string( lr_eval_string("{B64str}"), "Des" );// 解码

lr_output_message("************解码结果: %s**************", lr_eval_string("{Des}" ));

res = strcmp( lr_eval_string("{Str}"), lr_eval_string("{Des}") );// 校验

if (res == 0) lr_output_message("校验通过!");

return 0;

}

你可能感兴趣的:(loadrunner调用base64加解密)