我使用此程序对消息(程序参数:“ Sign Message”)进行加密,并使用libsodium对其进行解密,我尝试了bith变体crypto sign和crypto_sign_detached,但始终无法验证消息。
#include <iostream>
#include <string.h>
#include <iomanip>
#include <sodium.h>
#include <sstream>
using namespace std;
void HexStringToArray ( const char * const text , unsigned char a[] , const size_t array_size )
{
for ( size_t i = 0; i < array_size ; i ++)
{
const string text_part ( text + 2 * i , 2) ; // Process 2 chars at a time
stringstream s ( text_part ) ;
s >> hex ;
int value ;
s >> value ;
a[i] = char(value) ;
}
}
int main(const int argc, const char * const argv[])
{
if (sodium_init() == -1)
return 1;
if(strcmp(argv[1],"Sign")==0) //Das Programm signiert eine Nachricht
{
const char * const message = argv[2];//Nachricht
const size_t message_length = sizeof(message)+1;//Nachrichtenlänge in bytes: warum muss man +1 addieren damit auch die letzte Buchstage gelesen wird?
unsigned char* m;
m = (unsigned char*) message;//Konvertierung der Nachricht im benötigten Format
unsigned char pk[crypto_sign_PUBLICKEYBYTES];
unsigned char sk[crypto_sign_SECRETKEYBYTES];
crypto_sign_keypair(pk, sk);//generate keys
//convert signed message to hex
stringstream s;
s<<hex ;
for (size_t i = 0; i < sizeof pk; i++)
s << setw (2) << setfill ('0') << (int)pk[i];
unsigned char signed_message[crypto_sign_BYTES + message_length];
unsigned long long signed_message_len;
crypto_sign(signed_message, &signed_message_len,m,message_length, sk);
//convert key to hex
stringstream t;
t<<hex ;
for (size_t i = 0; i < sizeof signed_message; i++)
t << setw (2) << setfill ('0') << ( int )signed_message[i];
cout<<endl<<"Signed Message: "<<t.str();//hex
cout<<endl<<"Public key: "<<s.str();
}
else if(strcmp(argv[1],"Verify")==0)
{
unsigned char unsigned_message[strlen(argv[2])];//Länge Nachricht plaintext
unsigned long long unsigned_message_len;
const char * const signed_message_hexa=argv[3];
const char * const pk_hexa=argv[4];
//Hexa in char/ libsodium umwandeln
//signed_message_hexa
unsigned char signed_message[crypto_sign_BYTES + strlen(argv[2])];
for ( size_t i = 0; i < strlen(signed_message_hexa)/2 ; i ++)
{
const string text_part ( signed_message_hexa + 2 * i , 2) ;
stringstream s ( text_part ) ;
s >> hex ;
int value ;
s >> value ;
signed_message[i]=value;
}
//pk
unsigned char pk[crypto_sign_PUBLICKEYBYTES];
for ( size_t i = 0; i < strlen(pk_hexa)/2 ; i ++)
{
const string text_part ( pk_hexa + 2 * i , 2) ;
stringstream s ( text_part ) ;
s >> hex ;
int value ;
s >> value ;
pk[i]=value;
}
unsigned long long signed_message_len=crypto_sign_BYTES + strlen(argv[2]);
if (crypto_sign_open(unsigned_message, &unsigned_message_len,signed_message, signed_message_len, pk) != 0)
cout<<"Signature invalid";
else cout<<"Signature valid";
(void) argc;
return 0;
}}
我使用参数
启动程序签名消息
返回
签名消息:d93c44fd742e01c64a388862aa11e06dbb7081a2b4e226e78e9574b342dfb99bb2c4d188722faea49df2d2ed426131181d381923649144053dcd07ec46e2460d4d657373616766005555
公钥:9e649d74d1852ce6396b0f0a680e567f8663c2f6c3edacd0ba41db29f562066a
然后我以
开始确认消息d93c44fd742e01c64a388862aa11e06dbb7081a2b4e226e78e9574b342dfb99bb2c4d188722faea49df2d2ed426131181d381923649144053dcd07ec46e2460d4d6573736167650055e
它返回
签名无效
是否存在转换错误,或者我在错误的模式下使用了参数?
非常感谢!