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. 设计模式-命令模式(Command Pattern)

    本文由@呆代待殆原创,转载请注明出处:http://www.cnblogs.com/coffeeSS/ 命令模式简述 命令模式的主要作用是将“行为请求者”和“行为实现者”解耦.举个例子,假如我们现在要 ...

  2. python strip() 函数和 split() 函数的详解及实例

    strip是删除的意思:split则是分割的意思.strip可以删除字符串的某些字符,split则是根据规定的字符将字符串进行分割. 1.Python strip()函数 介绍 函数原型 声明:s为字 ...

  3. NOIP2018训练题集

    1. CZday3C 给定有m个数的集合,从其中任选一个子集满足全部&后不为零,问方案数. 考虑对二进制位容斥,问题转化为求包含某个二进制位集合的数的个数,通过类似FMT的DP求解. #inc ...

  4. window下命令行的方式安装svn服务端

    下载Binary Packages类型的 安装文件  https://www.visualsvn.com/server/download/  自己选择版本 第一步 :开始安装到 c:/software ...

  5. #iOS问题记录#UITableView加载后直接滑动倒最底部

    类似QQ的聊天框,当进入聊天框,直接滑动倒最底部: 需要先将以他变了view滚动倒底部,再来移动NSIndexPath, 代码如下: -(void) doForceScrollToBottom { d ...

  6. PHP温故知新(二)

    2.安装和配置 安装这里要注意两点,是之前没有在意的: 1.将php.ini文件中的 cgi.fix_pathinfo设置为0 设置为0是为了解决一个安全漏洞,假如我们现在有这样一个URL:http: ...

  7. <摘录>cocos2d-x 从环境搭建到win32项目移植android平台

    软件:cocos2d-x-2.2.3:android-ndk-r9d:adt-bundle-windows-x86_64-20131030:python-2.7.6: 1安装配置python 安装没什 ...

  8. 新浪微博宋琦:PHP在微博优化中的“大显身手”

    摘要:2013中国软件开发者大会编程语言与工具专题论坛中,新浪微博架构师宋琦介绍了PHP在新浪微博中的应用,并且分享了很多微博主站所做的性能优化的工作. [CSDN报道] 2013中国软件开发者大会( ...

  9. all objects of the same class share the same set of class methods

    #include <iostream> #include "First.h" void Test(); int main() { std::cerr<<&q ...

  10. Laravel简⃣单⃣的⃣路⃣由⃣

    在⃣routes.php文⃣件⃣中⃣写⃣如⃣下⃣几⃣个⃣函⃣数⃣: Route::get('/', function () { return view('welcome'); }); // 获⃣取⃣a ...