如何把英文字母等非特殊字符也url编码

炒鸡简单!上次做到一个CTF的题需要把admin转化成url编码(转化两次),我网上找半天的网站,都没有,写代码又不想写,后面看了一下url编码的介绍发现urlencode其实就是字符的十六进制前面加了个百分号。
在这里插入图片描述
0x01
写代码太慢了,所以就想着把字符串进行hex编码再加个%
在这里插入图片描述0x02
再进行一次hex编码,得到如图下!:
在这里插入图片描述再加上百分号就好。

0x03
但是如果字符串的量太大的话还是写代码,其实代码也不长。

#include "stdafx.h"
#include using namespace std;unsigned int utf8_decode( char *s, unsigned int *pi )
{unsigned int c;int i = *pi;/* one digit utf-8 */if ((s[i] & 128)== 0 ) {c = (unsigned int) s[i];i += 1;} else if ((s[i] & 224)== 192 ) { /* 110xxxxx & 111xxxxx == 110xxxxx */c = (( (unsigned int) s[i] & 31 ) << 6) +( (unsigned int) s[i+1] & 63 );i += 2;} else if ((s[i] & 240)== 224 ) { /* 1110xxxx & 1111xxxx == 1110xxxx */c = ( ( (unsigned int) s[i] & 15 ) << 12 ) +( ( (unsigned int) s[i+1] & 63 ) << 6 ) +( (unsigned int) s[i+2] & 63 );i += 3;} else if ((s[i] & 248)== 240 ) { /* 11110xxx & 11111xxx == 11110xxx */c =  ( ( (unsigned int) s[i] & 7 ) << 18 ) +( ( (unsigned int) s[i+1] & 63 ) << 12 ) +( ( (unsigned int) s[i+2] & 63 ) << 6 ) +( (unsigned int) s[i+3] & 63 );i+= 4;} else if ((s[i] & 252)== 248 ) { /* 111110xx & 111111xx == 111110xx */c = ( ( (unsigned int) s[i] & 3 ) << 24 ) +( ( (unsigned int) s[i+1] & 63 ) << 18 ) +( ( (unsigned int) s[i+2] & 63 ) << 12 ) +( ( (unsigned int) s[i+3] & 63 ) << 6 ) +( (unsigned int) s[i+4] & 63 );i += 5;} else if ((s[i] & 254)== 252 ) { /* 1111110x & 1111111x == 1111110x */c = ( ( (unsigned int) s[i] & 1 ) << 30 ) +( ( (unsigned int) s[i+1] & 63 ) << 24 ) +( ( (unsigned int) s[i+2] & 63 ) << 18 ) +( ( (unsigned int) s[i+3] & 63 ) << 12 ) +( ( (unsigned int) s[i+4] & 63 ) << 6 ) +( (unsigned int) s[i+5] & 63 );i += 6;} else {c = '?';i++;}*pi = i;return c;
}std::string UrlEncode(const std::string& src)
{static    char hex[] = "0123456789ABCDEF";std::string dst;for (size_t i = 0; i < src.size(); i++){unsigned char ch = src[i];if (isalnum(ch)){dst += ch;}elseif (src[i] == ' '){dst += '+';}else{unsigned char c = static_cast<unsigned char>(src[i]);dst += '%';dst += hex[c / 16];dst += hex[c % 16];}}return dst;
}std::string UrlDecode(const std::string& src)
{std::string dst, dsturl;int srclen = src.size();for (size_t i = 0; i < srclen; i++){if (src[i] == '%'){if(isxdigit(src[i + 1]) && isxdigit(src[i + 2])){char c1 = src[++i];char c2 = src[++i];c1 = c1 - 48 - ((c1 >= 'A') ? 7 : 0) - ((c1 >= 'a') ? 32 : 0);c2 = c2 - 48 - ((c2 >= 'A') ? 7 : 0) - ((c2 >= 'a') ? 32 : 0);dst += (unsigned char)(c1 * 16 + c2);}}elseif (src[i] == '+'){dst += ' ';}else{dst += src[i];}}int len = dst.size();for(unsigned int pos = 0; pos < len;){unsigned int nvalue = utf8_decode((char *)dst.c_str(), &pos);dsturl += (unsigned char)nvalue;}return dsturl;
}// 测试程序int main(int argc, char* argv[])
{string str1 = "VC知识库 vckbase.com";string str2 = "www.vckbase.com/sql.asp?id=2%20update and sele%%ct%fc%80%80%80%80%af";printf("%s ", UrlEncode(str1).c_str());    // URL编码printf("%s ", UrlDecode(str2).c_str());    // URL解码return 0;
}
代码作者:https://www.cctry.com/space-uid-51.html


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部