我正在使用Xamarin Forms上的Bouncy Castle进行非对称加密。但我正在研究客户< - > Web Api结构。如何将公钥发送给另一方?因为类型是RsaKeyParameters。
RsaKeyPairGenerator rsaKeyPairGnr = new RsaKeyPairGenerator();
rsaKeyPairGnr.Init(new KeyGenerationParameters(new SecureRandom(), 512));
AsymmetricCipherKeyPair keyPair = rsaKeyPairGnr.GenerateKeyPair();
RsaKeyParameters publicKey = (RsaKeyParameters)keyPair.Public;
答案 0 :(得分:1)
如果我有RsaKeyParameters publicKey
并且我需要将其发送到服务器,我可以使用以下方式进行转换:
//convert from key to string
SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(publicKey);
byte[] serializedPublicBytes = publicKeyInfo.ToAsn1Object().GetDerEncoded();
string serializedPublic = Convert.ToBase64String(serializedPublicBytes);
And then, i can convert the serializedPublic to RsaKeyParameters publicKey
//convert from string to key
RsaKeyParameters publicKey2 = (RsaKeyParameters)PublicKeyFactory.CreateKey(Convert.FromBase64String(serializedPublic));
但有人可以告诉我,如果通过Http库通过查询字符串传递公钥是个好主意吗?