在Java中生成RSA签名,但在C+中验证签名失败+; 最近我需要使用RSA在java中签名字符串,并在C++中验证签名。 在java中,现在我认为每件事都可以,我创建了Puff.KyStand和PrimeUn.KysRoR,可以成功地对数据进行签名和验证。但是当我尝试用C++验证它时,它显示了签名失败。

在Java中生成RSA签名,但在C+中验证签名失败+; 最近我需要使用RSA在java中签名字符串,并在C++中验证签名。 在java中,现在我认为每件事都可以,我创建了Puff.KyStand和PrimeUn.KysRoR,可以成功地对数据进行签名和验证。但是当我尝试用C++验证它时,它显示了签名失败。,java,c++,openssl,cryptography,crypto++,Java,C++,Openssl,Cryptography,Crypto++,这是我的Java代码,在Java中,我将数据签名为Base64字符串,并将其保存在本地计算机上为“sig.dat”,我签名的数据保存为“signed.dat”: 生成sig.dat和signed.dat文件: String signStr = RSAUtils.sign("123".getBytes(), privateKey2); boolean result = RSAUtils.verify("123".getBytes(), publicKey2, signStr); System.ou

这是我的Java代码,在Java中,我将数据签名为Base64字符串,并将其保存在本地计算机上为“sig.dat”,我签名的数据保存为“signed.dat”:

生成sig.dat和signed.dat文件:

String signStr = RSAUtils.sign("123".getBytes(), privateKey2);
boolean result = RSAUtils.verify("123".getBytes(), publicKey2, signStr);
System.out.println("result:" + result);


FileOutputStream fos = new FileOutputStream("signed.dat");
DataOutputStream dos = new DataOutputStream(fos);
dos.write("123".getBytes());
dos.close();

FileOutputStream fos2 = new FileOutputStream("sig.dat");
DataOutputStream dos2 = new DataOutputStream(fos2);
dos2.write(Base64.decodeBase64(signStr));

这里是我的C++代码,我加载文件:“签名.dAT”和“sig .dAT”,使用API来验证它:

void Verify()
{   
    //Read public key
    CryptoPP::ByteQueue bytes;
    FileSource file("pubkey.txt", true, new Base64Decoder);
    file.TransferTo(bytes);
    bytes.MessageEnd();
    RSA::PublicKey pubKey;
    pubKey.Load(bytes);

    RSASSA_PKCS1v15_SHA_Verifier verifier(pubKey);

    //Read signed message
    string signedTxt;
    FileSource("signed.dat", true, new StringSink(signedTxt));
    string sig;
    FileSource("sig.dat", true, new StringSink(sig));

    string combined(signedTxt);
    combined.append(sig);

    cout << "signedTxt------------:" << signedTxt<< endl;
    cout << "sig------------:" << sig << endl;
    //Verify signature
    try
    {
        StringSource(combined, true,
            new SignatureVerificationFilter(
                verifier, NULL,
                SignatureVerificationFilter::THROW_EXCEPTION
            )
        );
        cout << "Signature OK" << endl;
    }
    catch (SignatureVerificationFilter::SignatureVerificationFailed &err)
    {
        cout << err.what() << endl;
    }

}
void Verify()
{   
//读取公钥
CryptoPP::字节队列字节;
FileSource文件(“pubkey.txt”,true,新base64解码器);
文件传输到(字节);
MessageEnd();
RSA::公钥;
加载(字节);
RSASSA_PKCS1v15_SHA_验证器验证器(pubKey);
//读取签名消息
字符串signedText;
FileSource(“signed.dat”,true,新StringSink(signedText));
串信号;
FileSource(“sig.dat”,true,新的StringSink(sig));
组合字符串(signedText);
合并。追加(sig);
库特

将数字签名附加到要检查签名的数据是没有意义的。创建数字签名时,数字签名没有包含自身:因此,为什么要将其包含在要验证的数据中?

我是根据doc wiki,signature Scheme(PKCS v1.5)这样做的,为什么标签中有openssl?你不是在使用crypto++?你是否尝试使用
openssl
来验证签名?@Slava,没有……但是现在我想尝试openssl,因为到目前为止我还没有找到解决方案。我的意思是使用openssl可执行文件,而不是重写你的程序。顺便说一句,至少你可以找到罪魁祸首。
void Verify()
{   
    //Read public key
    CryptoPP::ByteQueue bytes;
    FileSource file("pubkey.txt", true, new Base64Decoder);
    file.TransferTo(bytes);
    bytes.MessageEnd();
    RSA::PublicKey pubKey;
    pubKey.Load(bytes);

    RSASSA_PKCS1v15_SHA_Verifier verifier(pubKey);

    //Read signed message
    string signedTxt;
    FileSource("signed.dat", true, new StringSink(signedTxt));
    string sig;
    FileSource("sig.dat", true, new StringSink(sig));

    string combined(signedTxt);
    combined.append(sig);

    cout << "signedTxt------------:" << signedTxt<< endl;
    cout << "sig------------:" << sig << endl;
    //Verify signature
    try
    {
        StringSource(combined, true,
            new SignatureVerificationFilter(
                verifier, NULL,
                SignatureVerificationFilter::THROW_EXCEPTION
            )
        );
        cout << "Signature OK" << endl;
    }
    catch (SignatureVerificationFilter::SignatureVerificationFailed &err)
    {
        cout << err.what() << endl;
    }

}
string combined(signedTxt);
combined.append(sig);