RC4加密中的其他字符
尝试使用打开的SSL RC4函数加密字符串时,我将面临问题。 somme添加字符在加密字符串的末尾添加。
这是我的代码:
RC4_KEY rc4_key;
QString key= "612c207468652066";
QByteArray keyData=key.QString::toUtf8();
unsigned char * mykey=reinterpret_cast<unsigned char *>(keyData.data());
QString input = "7447806cc997966f6b8b59a57c01bfe6fe8381d4fed5628b531fbeb1c4629151722ee712fa906dfea1c68b7015243bfcdd42d3e990c1bd6daa56df620a9d1e441f4ba308da5584e032f06fe925ee9df328fadab0ad2a5869fdea366189a397f72b320a4f11b0cc41cbfaa3b6923f20cc8c4c80d9c9b69eb5e2a6cc2fb9ee2e72ca79f08617b3b5812eddb3f58b68";
qDebug()<< "input"<<input << input.size();
QByteArray inputData=input.QString::toUtf8();
unsigned char * ciphertext1= (unsigned char*)malloc(input.size());
memset(ciphertext1, 0, input.size());
ciphertext1=reinterpret_cast<unsigned char *>(QByteArray::fromHex(inputData).data());
printf("ciphertext1: %s\n", ciphertext1);
RC4_set_key(&rc4_key,keyData.size() , mykey);
unsigned char * firsthashJson= (unsigned char*)malloc(input.size());
firsthashJson[input.size()-1]=0x00;
memset(firsthashJson, 0, input.size());
RC4(&rc4_key,input.length(),ciphertext1, firsthashJson);
printf("Decrypted: %s\n", firsthashJson);
QString str1 = QString::fromUtf8((char*)firsthashJson);
我获得了以下结果: B036FE3EAE7881FE135562335F9A ## 5265705250 ## B2A475F7A99CF400D34DA436D52222D9B C5566D8B7A015C7A16EE2D5E730199ACF1F61FE2F3D6B9525E1C45ACB36383253B4FC&lt; ú««««í ☻§@ı2w5▒ktâuæb¢¢dôxúx。°«▓j)→8èê╔┤║视@│K↕▬↨[
║gtuë B036FE3EAE7881FE135562335F9A ## 5265705250 ## B2A475F7A99CF400D34DA436D522D 9BC5566D8B7A015C7A16EE2D5E730199ACF1F61FE2F3D6B9525E1C45ACB36383253B4FC
来源有任何想法
。
您对此问题的 #####################################
感谢您的帮助。
不幸的是,我在这里遇到了同样的问题:新代码:
RC4_KEY rc4_key;
QString key= "612c207468652066";
QByteArray keyData=key.QString::toUtf8();
unsigned char * mykey=reinterpret_cast<unsigned char *>(keyData.data());
QString input = "7447806cc997966f6b8b59a57c01bfe6fe8381d4fed5628b531fbeb1c4629151722ee712fa906dfea1c68b7015243bfcdd42d3e990c1bd6daa56df620a9d1e441f4ba308da5584e032f06fe925ee9df328fadab0ad2a5869fdea366189a397f72b320a4f11b0cc41cbfaa3b6923f20cc8c4c80d9c9b69eb5e2a6cc2fb9ee2e72ca79f08617b3b5812eddb3f58b68";
qDebug()<< "input"<<input << input.size();
QByteArray inputData=input.QString::toUtf8();
unsigned char * ciphertext1= (unsigned char*)malloc(input.size());
memset(ciphertext1, 0, input.size());
ciphertext1=reinterpret_cast<unsigned char *>(QByteArray::fromHex(inputData).data());
printf("ciphertext1: %s\n", ciphertext1);
RC4_set_key(&rc4_key,keyData.size() , mykey);
unsigned char * firsthashJson= (unsigned char*)malloc(input.size()+1);
memset(firsthashJson, 0, input.size()+1);
firsthashJson[input.size()]=0;
RC4(&rc4_key,input.size(),ciphertext1, firsthashJson);
printf("Decrypted: %s\n", firsthashJson);
firsthashJson[input.size()]=0;
QString str1 = QString::fromUtf8((char*)firsthashJson);
qDebug()<<"final string "<<str1<< str1.size();
I am facing an issue when trying to encrypt a string using Open SSL RC4 function. Somme additionnal characters where added at the end of the encrypted string.
Here is my code :
RC4_KEY rc4_key;
QString key= "612c207468652066";
QByteArray keyData=key.QString::toUtf8();
unsigned char * mykey=reinterpret_cast<unsigned char *>(keyData.data());
QString input = "7447806cc997966f6b8b59a57c01bfe6fe8381d4fed5628b531fbeb1c4629151722ee712fa906dfea1c68b7015243bfcdd42d3e990c1bd6daa56df620a9d1e441f4ba308da5584e032f06fe925ee9df328fadab0ad2a5869fdea366189a397f72b320a4f11b0cc41cbfaa3b6923f20cc8c4c80d9c9b69eb5e2a6cc2fb9ee2e72ca79f08617b3b5812eddb3f58b68";
qDebug()<< "input"<<input << input.size();
QByteArray inputData=input.QString::toUtf8();
unsigned char * ciphertext1= (unsigned char*)malloc(input.size());
memset(ciphertext1, 0, input.size());
ciphertext1=reinterpret_cast<unsigned char *>(QByteArray::fromHex(inputData).data());
printf("ciphertext1: %s\n", ciphertext1);
RC4_set_key(&rc4_key,keyData.size() , mykey);
unsigned char * firsthashJson= (unsigned char*)malloc(input.size());
firsthashJson[input.size()-1]=0x00;
memset(firsthashJson, 0, input.size());
RC4(&rc4_key,input.length(),ciphertext1, firsthashJson);
printf("Decrypted: %s\n", firsthashJson);
QString str1 = QString::fromUtf8((char*)firsthashJson);
I obtained the following result: b036fe3eae7881fe135562335f9a##5265705250##b2a475f7a99cf400d34da436d522d9bc5566d8b7a015c7a16ee2d5e730199acf1f61fe2f3d6b9525e1c45acb36383253b4fc< Ú«Í`ã¿rßÖQÕ§ìj‗¤Ì&Öâ¤#>Xϸ0õ+▲®╩Ä=╚líUm"ód☻Ô1ı┘©ÜLÔ»âH÷/o┐öñ▄|▄ÐÄ┤N ☻§@ı2w5▒KTâUÆB¢ÏdÔxÚ⌂ê☺°¹╔«▓J)→8èê╔┤║ù@│k ↕▬↨[║GtUË(♂D
the expected one is : b036fe3eae7881fe135562335f9a##5265705250##b2a475f7a99cf400d34da436d522d9bc5566d8b7a015c7a16ee2d5e730199acf1f61fe2f3d6b9525e1c45acb36383253b4fc
Do you have any idea of the source cause of this issue.
Thanks in advance for your help.
###############################################################
@paddy
Thank you for your help.
Unfortunately i have the same issue here is the new code:
RC4_KEY rc4_key;
QString key= "612c207468652066";
QByteArray keyData=key.QString::toUtf8();
unsigned char * mykey=reinterpret_cast<unsigned char *>(keyData.data());
QString input = "7447806cc997966f6b8b59a57c01bfe6fe8381d4fed5628b531fbeb1c4629151722ee712fa906dfea1c68b7015243bfcdd42d3e990c1bd6daa56df620a9d1e441f4ba308da5584e032f06fe925ee9df328fadab0ad2a5869fdea366189a397f72b320a4f11b0cc41cbfaa3b6923f20cc8c4c80d9c9b69eb5e2a6cc2fb9ee2e72ca79f08617b3b5812eddb3f58b68";
qDebug()<< "input"<<input << input.size();
QByteArray inputData=input.QString::toUtf8();
unsigned char * ciphertext1= (unsigned char*)malloc(input.size());
memset(ciphertext1, 0, input.size());
ciphertext1=reinterpret_cast<unsigned char *>(QByteArray::fromHex(inputData).data());
printf("ciphertext1: %s\n", ciphertext1);
RC4_set_key(&rc4_key,keyData.size() , mykey);
unsigned char * firsthashJson= (unsigned char*)malloc(input.size()+1);
memset(firsthashJson, 0, input.size()+1);
firsthashJson[input.size()]=0;
RC4(&rc4_key,input.size(),ciphertext1, firsthashJson);
printf("Decrypted: %s\n", firsthashJson);
firsthashJson[input.size()]=0;
QString str1 = QString::fromUtf8((char*)firsthashJson);
qDebug()<<"final string "<<str1<< str1.size();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的核心问题是您将
input.size()
作为len
参数传递给rc4
,但是input.size()
实际上是双重ciphertext1
,因为十六进制编码。这是一个正确的版本,而没有留下QT的舒适性和安全性:
Your core problem is that you are passing
input.size()
as thelen
parameter toRC4
, butinput.size()
is actually double the size of yourciphertext1
because of hex encoding.Here is a correct version without leaving the comfort and safety of Qt: