简体   繁体   English

qt base64编码/解码添加转义字符

[英]qt base64 encode/decode adds escape characters

I have a method that returns an unsigned char * array and I am trying to encode this as base64 and decode it later. 我有一个返回unsigned char *数组的方法,我试图将其编码为base64并在以后对其进行解码。 So what I am doing is as follows: 所以我在做如下:

unsigned char * val = myMethod();
char * encodedMsg = reinterpret_cast<char *>(val);
std::cout << "Returned message: " << val << std::endl;
QByteArray raw = QByteArray(encodedMsg).toBase64(QByteArray::Base64Encoding | QByteArray::OmitTrailingEquals);

The output from the method is \z\\ri!i, and the encoded value is XHUwMDAxelxyaSFpLA . 该方法的输出为\z\\ri!i,编码值为XHUwMDAxelxyaSFpLA

Now, I decode it as follows: 现在,我将其解码如下:

QByteArray decoded = QByteArray::fromBase64(raw,
                            QByteArray::Base64Encoding | QByteArray::OmitTrailingEquals);

qDebug() << decoded;

Now this returns \\\z\\\\ri!i, . 现在,这将返回\\\z\\\\ri!i, Notice that the slashes have been escaped. 请注意,斜杠已被转义。

I know I can replace this as a post processing step but is there a way to avoid this. 我知道我可以将其替换为后期处理步骤,但是有办法避免这种情况。 Perhaps I am using the encoding/decoding incorrectly? 也许我使用的编码/解码不正确?

Try like below, 尝试如下

Base 64 Encode : Base 64编码:

unsigned char* Ciphertext = Encypt(); QByteArray qByteArray((char*)(Ciphertext),(int)strlen((char *)Ciphertext)); qDebug()<<"Ciphertext : "<< qByteArray.toBase64();

Output : 输出:

Ciphertext :  "WLdC+ri94z940BiAven6qXQH6rPbE64nQXt5aFByLGPFh1n//tOGLG02zBSoZ79qMA"

Base 64 Decode : Base 64解码:

QString data = QString::fromLatin1(QByteArray::fromBase64(Base64QByteArray.toBase64()).data());

in my case i used this base64 conversion for AES encyption, make it shoultable for yours. 在我的情况下,我将此base64转换用于AES加密,因此对您来说应该可以转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM