Base64 Encode/Decode for LoadRunner》这篇文章介绍了如何在LoadRunner中对字符串进行Base64的编码和解码:

http://ptfrontline.wordpress.com/2009/09/30/base64-encodedecode-for-loadrunner/

在头文件中封装b64_encode_string和b64_decode_string函数:

/*
Base 64 Encode and Decode functions for LoadRunner
==================================================
This include file provides functions to Encode and Decode
LoadRunner variables. It's based on source codes found on the
internet and has been modified to work in LoadRunner.

Created by Kim Sandell / Celarius - www.celarius.com
*/
// 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:
//&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;source&nbsp;&nbsp;&nbsp; Pointer to source string to encode
//&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;lrvar&nbsp;&nbsp;&nbsp;&nbsp; LR variable where base64 encoded string is stored
//
// Example:
//
//&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;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:
//&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;source&nbsp;&nbsp;&nbsp; Pointer to source base64 encoded string
//&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;lrvar&nbsp;&nbsp;&nbsp;&nbsp; LR variable where decoded string is stored
//
// Example:
//
//&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;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;
}

使用的例子如下所示:

#include "base64.h"

vuser_init()
{
 int res;

// ENCODE  
 lr_save_string("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789","plain");  
 b64_encode_string( lr_eval_string("{plain}"), "b64str" );  
 lr_output_message("Encoded: %s", lr_eval_string("{b64str}") );

// DECODE  
 b64_decode_string( lr_eval_string("{b64str}"), "plain2" );  
 lr_output_message("Decoded: %s", lr_eval_string("{plain2}") );  
   
 // Verify decoded matches original plain text  
 res = strcmp( lr_eval_string("{plain}"), lr_eval_string("{plain2}") );  
 if (res==0) lr_output_message("Decoded matches original plain text");  
 
 return 0;

}

在LoadRunner中进行Base64的编码和解码的更多相关文章

  1. javascript中的Base64.UTF8编码与解码详解

    javascript中的Base64.UTF8编码与解码详解 本文给大家介绍的是javascript中的Base64.UTF8编码与解码的函数源码分享以及使用范例,十分实用,推荐给小伙伴们,希望大家能 ...

  2. 【项目分析】利用C#改写JAVA中的Base64.DecodeBase64以及Inflater解码

    原文:[项目分析]利用C#改写JAVA中的Base64.DecodeBase64以及Inflater解码 最近正在进行项目服务的移植工作,即将JAVA服务的程序移植到DotNet平台中. 在JAVA程 ...

  3. 利用openssl进行base64的编码与解码

    openssl可以直接使用命令对文件件进行base64的编码与解码,利用openssl提供的API同样可以做到这一点. 废话不多说,直接上代码了.需要注意的是通过base64编码后的字符每64个字节都 ...

  4. Python2/3的中、英文字符编码与解码输出: UnicodeDecodeError: 'ascii' codec can't decode/encode

    摘要:Python中文虐我千百遍,我待Python如初恋.本文主要介绍在Python2/3交互模式下,通过对中文.英文的处理输出,理解Python的字符编码与解码问题(以点破面). 前言:字符串的编码 ...

  5. PHP对Url中的汉字进行编码和解码

    有的新手朋友们对于url编码解码这个概念,或许有点陌生.但是如果这么说,当我们在浏览各大网页时,可能发现有的url里有一些特殊符号比如#号,&号,_号或者汉字等等,那么为了符合url的规范,存 ...

  6. Python 中的 is 和 == 编码和解码

    一   is   与   ==   区别 ==    比较            比较的是值 例如: a = 'alex' b = 'alex' print(a == b) #True a = 10 ...

  7. 利用C#改写JAVA中的Base64.DecodeBase64以及Inflater解码

    最近正在进行项目服务的移植工作,即将JAVA服务的程序移植到DotNet平台中. 在JAVA程序中,有个HTTP请求数据头中,包含一个BASE64编码的字符串,例如: eJyVjMENgDAMA1fp ...

  8. 利用window对象自带atob和btoa方法进行base64的编码和解码

    项目中一般需要将表单中的数据进行编码之后再进行传输到服务器,这个时候就需要base64编码 现在可以使用window自带的方法window.atob() 和  window.btoa()  方法进行 ...

  9. [Swift]扩展String类:Base64的编码和解码

    扩展方式1: extension String { //Base64编码 func encodBase64() -> String? { if let data = self.data(usin ...

随机推荐

  1. CodeForces 740C Alyona and mex

    构造. 比较骚的构造题.肯定可以构造出$min(R-L+1)$,只要$0$ $1$ $2$ $...$ $R-L$ $0$ $1$ $2$ $...$ $R-L$填数字即可,这样任意一段区间都包含了$ ...

  2. POJ3666 Making the Grade [DP,离散化]

    题目传送门 Making the Grade Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9090   Accepted: ...

  3. 1、编译安装Nginx

    1.1 如何选择web服务器 在实际工作中,我们需要根据业务需求来选择合适的业务服务软件,有关web服务,选择建议如下: 静态业务:若是高并发场景,尽量采用nginx或lighttpd,二者首选ngi ...

  4. 桌面笔记工具KeepNote

    桌面笔记工具KeepNote   在渗透测试过程中,安全人员经常需要记录各种数据,如输出结果.运行截图.测试心得.这类信息格式多样,可能是图片.文字.文件等.为了便于管理这些内容,Kali Linux ...

  5. 20162312 实验一 Linux基础与Java开发环境

    实 验 报 告 课程:程序设计与数据结构 姓名:张家铖 班级:1623 学号:20162312 实验名称: Linux基础与Java开发环境 实验器材:msi GL62M 7RD 实验目的与要求:1. ...

  6. Problem C: 指针:自定义函数length,调用它计算字符串的长度

    #include<stdio.h> int length(char*s) { int i,count; while(*s!='\0') { *(s++); count++; } retur ...

  7. 【MySQL笔记】字符串、时间日期转换

    1.新增一列,将字符串日期(年.月.日)转换为Date类型   报错:Error Code: 1175. You are using safe update:http://jingyan.baidu. ...

  8. Python的函数参数和递归参数

    位置参数 def power(x): return x*x; 默认参数 指的是在函数定义的时候,就赋予一些参数默认值,在调用这个函数的时候不必多次传入重复的参数值. 如定义一个多次输出同一个年龄阶段和 ...

  9. 最好的拖拽js

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. QJSON封装好的序列和还原方法

    QJSON封装好的序列和还原方法 {*******************************************************}{ }{ QJSON与数据集互转 }{ }{ 版权所 ...