Java/.NET 中的 RSA 加密和 .NET 中的解密

发布于 2024-11-05 04:49:13 字数 1042 浏览 0 评论 0 原文

我的应用程序有 Java 和 .NET 客户端,并且我的应用程序位于 .NET 中。我的客户将向我发送一个使用 RSA 公钥加密的 XML 文件,我需要使用 .NET 对其进行解密

对于字符串“Achinth Anand Gurkhi”,Java RSA 加密方法会生成以下加密字符串:

e8s2Ap3R1AwoaKB7OPCwkf0vhAVGaQisdoq2Yo0BvwcQ7v3oVtMOVc5wsnIyNVOSZV543imwIiBer0HSXRe8PoBD4jj0tTxtLA+bdoR40oQJD2UmZ4OpAH3g92wLXYd4bVvjllcCPPc0tSr/nzEKeZHcnhf6cGpuwfKyFNbXW2vtlEfmRd+LGqlixPRlx1OnsSMNNw+u/5IBs8MauY4Uwq1Lovlgd9f/8WTOvq9ityr84vGLMRGs4wpC7+fFNk8jGuNZgoCDLZw2RqrUd8FBFvN2wCRZXnS7Wg4QjiBdmnq0OsAwK9OFwqnil7DNnDnlytlecR5oYkDhO2fC4FzFiA==

对于相同的字符串,.NET RSA加密方法使用 Java 程序使用的相同公钥生成以下加密字符串:

iJO4hwhXGX27jzK87X9gxzzbKpgf7FKhe6UcY7eoiCpLskOatgCMZTm0aTDuwRZGJGbZCIZt+JI9X8LxwOLmIbv5LGyDq+a8jkrPu+pDRvg2uRuKeQj2yBRcp36X+xFf61ux24NaX2RTCY9YfJcUis9NjEkL0eQ3gC79xO0vuBjaUA2oYOt0Mlr7DmKE+b0lz25J/WJuSW83g2oZOlvJ4RnsrFChu0vHnkHCQo9JVjhMc+Onj7+lbI1CDgGq4XigZrHt+j564y3sc3z0oQYfdZkF3yUZrzd3sJjd9KmryHf52eVb9/qgL2/Za1jUwTzKIOvtG/bQpR2ka7Qu1ZqbxQ==

但我使用匹配私钥的 .NET 解密方法能够将两者解密回相同的字符串“Achinth Anand Gurkhi”。不同的加密字符串怎么可能返回相同的值呢?

My application has Java and .NET clients and my application is in .NET. My clients will send me an XML file encrypted using RSA public key and I need to decrypt it using .NET

For a string say "Achinth Anand Gurkhi" the Java RSA Encryption method generates the following encrypted string:

e8s2Ap3R1AwoaKB7OPCwkf0vhAVGaQisdoq2Yo0BvwcQ7v3oVtMOVc5wsnIyNVOSZV543imwIiBer0HSXRe8PoBD4jj0tTxtLA+bdoR40oQJD2UmZ4OpAH3g92wLXYd4bVvjllcCPPc0tSr/nzEKeZHcnhf6cGpuwfKyFNbXW2vtlEfmRd+LGqlixPRlx1OnsSMNNw+u/5IBs8MauY4Uwq1Lovlgd9f/8WTOvq9ityr84vGLMRGs4wpC7+fFNk8jGuNZgoCDLZw2RqrUd8FBFvN2wCRZXnS7Wg4QjiBdmnq0OsAwK9OFwqnil7DNnDnlytlecR5oYkDhO2fC4FzFiA==

For the same string the .NET RSA Encryption method generates the following encrypted string with the same public key used by Java program:

iJO4hwhXGX27jzK87X9gxzzbKpgf7FKhe6UcY7eoiCpLskOatgCMZTm0aTDuwRZGJGbZCIZt+JI9X8LxwOLmIbv5LGyDq+a8jkrPu+pDRvg2uRuKeQj2yBRcp36X+xFf61ux24NaX2RTCY9YfJcUis9NjEkL0eQ3gC79xO0vuBjaUA2oYOt0Mlr7DmKE+b0lz25J/WJuSW83g2oZOlvJ4RnsrFChu0vHnkHCQo9JVjhMc+Onj7+lbI1CDgGq4XigZrHt+j564y3sc3z0oQYfdZkF3yUZrzd3sJjd9KmryHf52eVb9/qgL2/Za1jUwTzKIOvtG/bQpR2ka7Qu1ZqbxQ==

But my .NET decryption method using the matching private key is able to decrypt both back to the same string "Achinth Anand Gurkhi". How is it possible that different encrypted strings return the same value?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

腹黑女流氓 2024-11-12 04:49:13

它可能是由于随机填充而发生的。

It can happen because of Random Padding.

空城缀染半城烟沙 2024-11-12 04:49:13

这次您可能很幸运,没有被这个问题所困扰(可能是因为您的示例文件只有一个块),但您需要确保加密器和解密器的密码模式相同。

在 .NET 中,密码模式由 transformation 参数的第二个和第三个字段控制/crypto/Cipher.html#getInstance%28java.lang.String%29" rel="nofollow">Cipher.getInstance()

两个平台之间的默认模式也有所不同(.NET 中的 CBC) ,Java 中的 ECB),如果您忽略在任一侧设置模式,这可能会让您陷入困境。

You may have been lucky on this occasion and not been bitten by this (maybe because your sample file is only one block) but you need to ensure that the cipher mode is the same for both the encryptor and decryptor.

In .NET the cipher mode is controlled by the Mode property of the cipher. In Java it is controlled by the second and third fields of the transformation parameter of Cipher.getInstance()

The default mode also differs between the two platforms (CBC in .NET, ECB in Java) which can trip you up if you neglect to set the mode on either side.

如歌彻婉言 2024-11-12 04:49:13

您尚未指定在每种情况下如何加密它们,但标准方法是生成随机对称密钥,使用该密钥加密消息,然后使用公钥加密对称密钥,然后发送两者。因此,即使使用相同的代码,使用相同的密钥加密相同的消息也总是会给出不同的结果。这是预期的,并且这是一个功能,而不是错误:这意味着攻击者也无法判断您是否多次发送相同的消息。

You haven't specified how you encrypted them in each case, but the standard methodology is to generate a random symmetric key, encrypt the message with that key, then encrypt the symmetric key with the public key, and send both. As a result, encrypting the same message with the same key will always give different results, even using the same code. This is expected, and it's a feature, not a bug: It means attackers can't tell if you're sending the same message multiple times either.

他夏了夏天 2024-11-12 04:49:13

据我所知,公钥/私钥对就像:

使用公钥对数据进行加密。使用私钥解密。即公钥和私钥彼此相反。

在通信通道之间,一端(源)与另一端(目标)共享公钥。现在源使用私钥加密其数据并将其发送到目标。 Dest 应用源共享的公钥来解密。

这里可能会发生什么:.NET 和 JAVA 的私钥不同,但相应的公钥必须是各自私钥的逆。

这些密钥可能会考虑主机名/IP/或任何内容(我不确定)来生成公钥/私钥对。
公钥加密需要两个彼此相反的密钥,但加密数据不一定相同

有什么意见/建议吗?

As far as I know Public/Private key pairs are like:

Use public key to a data to encrypt. Use private key to decrypt. i.e. public and private keys are inverse of each other.

Between, a communication channel, one end (source) shares the public key with the other end (dest). Now source encrypts its data using private key and send it to the dest. Dest applies the shared public key by the source to decrypt it.

What here might be going: The private keys of .NET and JAVA are different, but the corresponding public keys must be the inverse of respective private keys.

These keys may take hostname/ip/or anything (I'm not sure) into account to generate a public/private key pair.
The public key encrytion needs two keys which are inverse of each other but not necessarily the encrypted data be same

Any comments/suggestions?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文