简体   繁体   English

在 C# 中验证什么是在 Java (RSA) 中签名的问题

[英]Problem verifying in C# what was signed in Java (RSA)

I was hoping I might get some help here so that I might finally solve this frustrating problem.我希望我能在这里得到一些帮助,这样我就可以最终解决这个令人沮丧的问题。

On the java side of things they sign with the following code:在 Java 方面,他们使用以下代码签名:

public static void main(String[] args) throws Exception {
    if (args.length < 2)
        printInfoAndExit();
    String cmd = args[0];
    Security.addProvider(new BouncyCastleProvider());
    Signature signature = Signature.getInstance("SHA1withRSA", "BC");
    if ("sign".equalsIgnoreCase(cmd)) {
        String pemFileName = args[1];
        String dataFileName = args[2];

        byte[] data = readFile(dataFileName);

        FileReader fr = new FileReader(new File(pemFileName));
        PEMReader pemReader = new PEMReader(fr);
        KeyPair keyPair = (KeyPair) pemReader.readObject();
        fr.close();

        signature.initSign(keyPair.getPrivate());
        signature.update(data);
        byte[] signatureBytes = signature.sign();

        writeFile(signatureBytes, dataFileName + ".signed");
        String encoded = Base64.encode(signatureBytes);
        writeFile(encoded.getBytes(), dataFileName + ".signed.base64");
    } else {
        printInfoAndExit();
    }
}

When I receive the data I have their public key and try to verify with the following C# code:当我收到数据时,我有他们的公钥并尝试使用以下 C# 代码进行验证:

public static bool Verify(String msg, String signature, String publicKey)
{
    RsaKeyParameters remotepubkey = GetRsaPublicKey(publicKey);

    ISigner signer = SignerUtilities.GetSigner("SHA1withRSA");

    signer.Init(false, remotepubkey);
    byte[] sigBytes = Convert.FromBase64String(signature);
    byte[] msgBytes = Encoding.Default.GetBytes(msg);
    signer.BlockUpdate(msgBytes, 0, msgBytes.Length);
    return signer.VerifySignature(sigBytes);
}

This is not working!!这是行不通的!! I can however verify the data with openssl: openssl dgst -sha1 -verify public_key.pem -signature data.txt.signed data.txt但是,我可以使用 openssl 验证数据: openssl dgst -sha1 -verify public_key.pem -signature data.txt.signed data.txt

The question is, what am I missing to make this work?问题是,我缺少什么才能完成这项工作?

NOTE: I don't have a problem with the keys, that is working correctly but somehow there is a difference between how java and .net works with RSA?注意:我的密钥没有问题,它工作正常,但不知何故,java 和 .net 与 RSA 的工作方式有什么不同?

**Edit 1 : **In this particular scenario all I had to do was change the GetSigner to **编辑 1 :**在这个特定场景中,我所要做的就是将 GetSigner 更改为

ISigner signer = SignerUtilities.GetSigner("RSA");

Could someone tell me the difference between SHA1withRSA and RSA?有人能告诉我 SHA1withRSA 和 RSA 之间的区别吗?

The problem was actually solved on the Java side.这个问题实际上是在Java端解决的。 They had some issues with their side of things.他们在事情上有一些问题。

You could have an encoding problem with your message data.您的消息数据可能存在编码问题。 You've converted the original file data into a unicode string, and are trying to convert it back to raw bytes.您已将原始文件数据转换为 unicode 字符串,并尝试将其转换回原始字节。 Depending on the encoding of the file, and if it's even text at all, your msgBytes could be different from the actual file contents.根据文件的编码,如果它甚至是文本,您的 msgBytes 可能与实际文件内容不同。

Read the raw bytes from the file instead of a string.从文件中读取原始字节而不是字符串。 You don't show the code for actually reading the file data, but I assume you're reading it as text.您没有显示实际读取文件数据的代码,但我假设您将其作为文本读取。

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

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