码迷,mamicode.com
首页 > 其他好文 > 详细

LoadRunner中Base64编码解码

时间:2020-06-28 15:36:30      阅读:61      评论:0      收藏:0      [点我收藏+]

标签:encode   系统用户   param   ESS   div   return   信息   rac   src   

在测试系统中,有些系统用户登录信息采用了base64进行编码,测试脚本里面,就需要对用户账号和密码进行编码处理,下面介绍一下LoadRunner中base64的编码和解码方法;

1、新建base64.h文件

base64.h文件用于编写base64编码和解码的方法,具体内容如下:

// Encoding lookup table
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‘,‘+‘,‘/‘,‘=‘};

// Decode lookup table
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)
// Encodes a buffer to base64
{
int i=0, slen=strlen(src);
for(i=0;i<slen && i<len;i+=3,src+=3)
{ // Enc next 4 characters
*(dest++)=base64encode_lut[(*src&0xFC)>>0x2];
*(dest++)=base64encode_lut[(*src&0x3)<<0x4|(*(src+1)&0xF0)>>0x4];
*(dest++)=((i+1)<slen)?base64encode_lut[(*(src+1)&0xF)<<0x2|(*(src+2)&0xC0)>>0x6]:‘=‘;
*(dest++)=((i+2)<slen)?base64encode_lut[*(src+2)&0x3F]:‘=‘;
}
*dest=‘/0‘; // Append terminator
}

void base64decode(char *src, char *dest, int len)
// Encodes a buffer to base64
{
int i=0, slen=strlen(src);
for(i=0;i<slen&&i<len;i+=4,src+=4)
{ // Store next 4 chars in vars for faster access
char c1=base64decode_lut[*src], c2=base64decode_lut[*(src+1)], c3=base64decode_lut[*(src+2)], c4=base64decode_lut[*(src+3)];
// Decode to 3 chars
*(dest++)=(c1&0x3F)<<0x2|(c2&0x30)>>0x4;
*(dest++)=(c3!=64)?((c2&0xF)<<0x4|(c3&0x3C)>>0x2):‘/0‘;
*(dest++)=(c4!=64)?((c3&0x3)<<0x6|c4&0x3F):‘/0‘;
}
*dest=‘/0‘; // Append terminator
}

int b64_encode_string( char *source, char *lrvar )
// ----------------------------------------------------------------------------
// Encodes a string to base64 format
//
// Parameters:
//        source    Pointer to source string to encode
//        lrvar     LR variable where base64 encoded string is stored
//
// Example:
//
//        b64_encode_string( "Encode Me!", "b64" )
// ----------------------------------------------------------------------------
{
int dest_size;
int res;
char *dest;
// Allocate dest buffer
dest_size = 1 + ((strlen(source)+2)/3*4);
dest = (char *)malloc(dest_size);
memset(dest,0,dest_size);
// Encode & Save
base64encode(source, dest, dest_size);
lr_save_string( dest, lrvar );
// Free dest buffer
res = strlen(dest);
free(dest);
// Return length of dest string
return res;
}

int b64_decode_string( char *source, char *lrvar )
// ----------------------------------------------------------------------------
// Decodes a base64 string to plaintext
//
// Parameters:
//        source    Pointer to source base64 encoded string
//        lrvar     LR variable where decoded string is stored
//
// Example:
//
//        b64_decode_string( lr_eval_string("{b64}"), "Plain" )
// ----------------------------------------------------------------------------
{
int dest_size;
int res;
char *dest;
// Allocate dest buffer
dest_size = strlen(source);
dest = (char *)malloc(dest_size);
memset(dest,0,dest_size);
// Encode & Save
base64decode(source, dest, dest_size);
lr_save_string( dest, lrvar );
// Free dest buffer
res = strlen(dest);
free(dest);
// Return length of dest string
return res;
}

2、新建loadrunner脚本

  右键选中globals.h,点击【add files to script】,添加base64.h文件到脚本中;

3、编辑globals.h

  打开globals.h,添加头文件:

  #include "base64.h" 

4、编辑action脚本

  打开action,编辑编码解码脚本,脚本内容如下: 

Action()
{
	lr_save_string("13111110001","Plain");

	//base64加密
	b64_encode_string(lr_eval_string("{Plain}"),"B64str");
	lr_output_message("Base64编码结果:%s",lr_eval_string("{B64str}"));

	//base64解密
	b64_decode_string(lr_eval_string("{B64str}"),"Plain2");
	lr_output_message("Base64解码结果:%s",lr_eval_string("{Plain2}"));

	return 0;
}

5、编译执行脚本

  编译执行脚本,执行结果如下;  

Starting iteration 1.
Starting action Action.
Action.c(7): Base64编码结果:MTMxMTExMTAwMDE=/
Action.c(11): Base64解码结果:13111110001
Ending action Action.
Ending iteration 1.

  

 

LoadRunner中Base64编码解码

标签:encode   系统用户   param   ESS   div   return   信息   rac   src   

原文地址:https://www.cnblogs.com/wzl0916/p/13202719.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!