c++Builder XE6 MD5 加密算法 BASE64 URL 编码

xe6,xe7 BASE64
XE6 MD5 加密算法

Delphi

function MD5(const texto: string): string; var idmd5: TIdHashMessageDigest5; begin idmd5 := TIdHashMessageDigest5.Create; try result := idmd5.HashStringAsHex(texto); finally idmd5.Free; end; end;


c++builder
#include <IdHashMessageDigest.hpp>

String __fastcall MD5(const String texto)
{
    String result;
    TIdHashMessageDigest5 *idmd5= new TIdHashMessageDigest5();
    try
    {
       result= idmd5->HashStringAsHex(texto,IndyTextEncoding_UTF8());//IndyTextEncoding_UTF8()可忽略
    }
    __finally
    {
       delete idmd5;
    }
    return result;
}

 



texto是汉字有bug,
中国:EA03FCB8C47822BCE772CF6C07D0EBBB
北京:EA03FCB8C47822BCE772CF6C07D0EBBB
这2个串是相等的啊!!,难道xe6的HashStringAsHex方法有问题?
最终原因是加上参数IndyTextEncoding_UTF8()就可以了。字符编码不正确。
  • BASE64
XE6的base64编码与xe7不同,区别在于对汉字的处理,汉字的编码值不一样。
XE7:move base64 function to System.NetEncoding.hpp
Soap::Encddecd::EncodeString默认是UnicodeString,汉字也是Unicode再进行编码。
XE6:EncodeString默认是AnsiString,汉字也是按ascii码进行b64编码的。
所以xe6、xe7对汉字的b64编码不一样。

在XE7里如果用旧的ascii编码方式,可以调用AnsiString __fastcall EncodeBase64(const void * Input, int Size)函数实现。
    String ustring = Memo1->Text;
    AnsiString as = AnsiString(ustring);
    Memo2->Text = Soap::Encddecd::EncodeBase64(as.c_str(), as.Length());

 

XE里没有idbase64decoder控件了。

Delphi 自带了 Base64 编解码的单元,叫 EncdDecd,这名字很拗口而且不直观,估计这是一直很少人关注和知道的原因。

Berlin,XE7 base64,2016.10.8

xe7中对Encode重载了3种方法。

int __fastcall Encode(TStream*  Input, TStream*  Output)/* overload */;
DynamicArray<Byte> __fastcall Encode(Byte  *Input,  int Input_High)/* overload */;
UnicodeString __fastcall Encode( UnicodeString Input)/* overload */;
   LEncoder := TBase64Encoding.Create(0);
  Result :=  LEncoder.Encode('hello');

 

#include <System.NetEncoding.hpp>

Memo2->Text = TNetEncoding::Base64->Encode(Memo1->Text);

Memo1->Text = TNetEncoding::Base64->Decode(Memo2->Text);

 

To encode: 

bytes: TBytes;
base64 :String;
bytes := TEncoding.UTF8.GetBytes(str);
base64 := TNetEncoding.Base64.EncodeBytesToString(bytes);


use String type
b64:=  TNetEncoding.Base64.Encode(astr);

To decode:

bytes := TNetEncoding.Base64.DecodeStringToBytes(base64);
str := TEncoding.UTF8.GetString(bytes);

 

c++

TBytes bytes;//TByteDynArray bytes;

TNetEncoding::Base64->EncodeBytesToString(&bytes[0], bytes.High);

 

Soap.EncdDecd.pas

XE 自带的 MD5 单元 MessageDigest_5.pas


这个单元提供两套四个公开函数:

对流的编解码:
procedure EncodeStream(Input, Output: TStream); // 编码
procedure DecodeStream(Input, Output: TStream); // 解码

// 对字符串的编解码:
function EncodeString(const Input: string): string; // 编码
function DecodeString(const Input: string): string; // 解码

这几个函数在帮助中没有。应该不算是标准库中的函数。

c++builder

#include <EncdDecd.hpp>

Soap::Encddecd::EncodeString( edtkl->Text);

String es=Soap::Encddecd::EncodeString("ee");

Soap::Encddecd::EncodeString(es);

http://codeverge.com/embarcadero.delphi.tools/indy-tidhashmessagedigest5-changed-f/1061390

 

根据RFC822规定,每76个字符,还需要加上一个回车换行

如果不用换行符,自己创建类,用MaxInt

TBase64Encoding *b64e=new TBase64Encoding(MaxInt);
mmoBase->Text = b64e->EncodeBytesToString(&bta[0], bta.High);
delete b64e;

 

 New Unit for Web Encoding and Decoding

The RTL provides a new unit, System.NetEncoding, that provides encoding and decoding features for Base64, HTML, and URL.

The following table shows how methods from previous versions of RAD Studio map to methods in System.NetEncoding:

 

HTTPEncode、URLEncode、URL编码

HZ=HTTPEncode( "查询功能");//解决在中文汉字编码URL地址乱码的问题

比如TIdHTTP.Get(URL,stream);这个URL里含有汉字需要转换一下

 

XE6XE7

Soap.EncdDecd.EncodeBase64

TNetEncoding.Base64.EncodeBytesToString

Soap.EncdDecd.DecodeBase64

TNetEncoding.Base64.DecodeStringToBytes

Soap.EncdDecd.EncodeStream
Soap.EncdDecd.EncodeString

TNetEncoding.Base64.Encode

Soap.EncdDecd.DecodeStream
Soap.EncdDecd.DecodeString

TNetEncoding.Base64.Decode

Web.HTTPApp.HTMLEncode

TNetEncoding.HTML.Encode

Web.HTTPApp.HTMLDecode

TNetEncoding.HTML.Decode

Web.HTTPApp.HTTPEncode

TNetEncoding.URL.Encode

Web.HTTPApp.HTTPDecode

TNetEncoding.URL.Decode

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值