gpt4 book ai didi

java - c# RSA 使用私钥加密

转载 作者:太空宇宙 更新时间:2023-11-03 15:33:30 26 4
gpt4 key购买 nike

公钥加密私钥解密加解密成功:

C#公钥加密(成功)

   public string EncryptData(string data) {

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(xml); //public key

var cipher = rsa.Encrypt(Encoding.UTF8.GetBytes(data), false);

return Convert.ToBase64String(cipher );
}

Java私钥解密(成功)

public static void decrypt() throws Exception{
byte[] modulusBytes = Base64.getDecoder().decode(mod);
byte[] dByte = Base64.getDecoder().decode(d);

BigInteger modulus = new BigInteger(1, (modulusBytes));
BigInteger exponent = new BigInteger(1, (dByte));

RSAPrivateKeySpec rsaPrivKey = new RSAPrivateKeySpec(modulus, exponent);
KeyFactory fact = KeyFactory.getInstance("RSA");
PrivateKey privKey = fact.generatePrivate(rsaPrivKey);

Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, privKey);

byte[] cipherData = Base64.getDecoder().decode(cipherByte);
byte[] plainBytes = cipher.doFinal(cipherData);


System.out.println(new String(plainBytes));
}

问题在这里

当c#用私钥加密,java用公钥解密时出现bad padding错误:

使用私钥进行C#加密(失败)

public stringEncryptData(string data) {

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(xml); //private key

var cypher = rsa.Encrypt(Encoding.UTF8.GetBytes(data), false);

return Convert.ToBase64String(cypher);
}

用公钥java解密(失败)

public static void decryptPublic() throws Exception{

byte[] modulusBytes = Base64.getDecoder().decode(mod);
byte[] expBytes = Base64.getDecoder().decode(exp);

BigInteger modulus = new BigInteger(1, (modulusBytes));
BigInteger exponent = new BigInteger(1, (expBytes));

RSAPublicKeySpec pubKey = new RSAPublicKeySpec(modulus, exponent);
KeyFactory fact = KeyFactory.getInstance("RSA");
PublicKey publicKey = fact.generatePublic(pubKey);
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, publicKey );


byte[] cipherData = Base64.getDecoder().decode(cipherByte);
byte[] plainBytes = cipher.doFinal(cipherData);

System.out.println(new String(plainBytes));
}

我知道公钥应该用于加密,私钥用于解密。但在我的情况下,我需要将公钥发送给多个客户端,以便对由其私钥加密的文本进行解密。除客户外,其他人应该无法阅读文本。任何人都可以看到我的代码有什么问题,或者对我的问题提出更好的解决方案。

最佳答案

RSA 加密只有在使用(安全)填充方案时才是安全的。 RSA 加密方案已由 RSA 实验室(现在是 EMC2 的一部分)在 PKCS#1 标准中指定。这些已被复制到 RFC 中,例如 RFC 3447: Public-Key Cryptography Standards (PKCS) #1: RSA Cryptography Specifications Version 2.1 .

For the purposes of this document, an encryption scheme consists ofan encryption operation and a decryption operation, where theencryption operation produces a ciphertext from a message with arecipient's RSA public key, and the decryption operation recovers themessage from the ciphertext with the recipient's corresponding RSAprivate key.

所以用私钥加密是一个未定义的操作。


那么现在要做什么:

  • 安全地分发私钥而不是公钥
  • 生成 key 对并安全地将公钥传输给发送者
  • 如果您需要身份验证/完整性而不是 secret 性,请使用签名生成而不是加密

而且,无论您做什么,都要阅读公钥基础设施 (PKI)。这是一个涉及面很广的主题,您需要先了解它,然后才能应用它。

关于java - c# RSA 使用私钥加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32816389/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com