将Objective-C RSA公钥导入到c#RSACryptoServiceProvider中 [英] Importing an Objective-C RSA public key into c# RSACryptoServiceProvider

查看:155
本文介绍了将Objective-C RSA公钥导入到c#RSACryptoServiceProvider中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是使用以下lib生成RSA对象的Objective-C: https://github.com/kuapay/iOS-Certificate-Key--and-Trust-Sample-Project

Here is the Objective-C we are using to generate the RSA object using the following lib: https://github.com/kuapay/iOS-Certificate--Key--and-Trust-Sample-Project

 BDRSACryptor *rsa  = [[BDRSACryptor alloc] init];
 BDRSACryptorKeyPair *RSAKeyPair = [rsa generateKeyPairWithKeyIdentifier:nil error:error];

然后我们将 RSAKeyPair.publicKey 传递给我们c#,其中使用BouncyCastles库:

We then pass RSAKeyPair.publicKey to our c#, where using the BouncyCastles library:

using (TextReader sr = new StringReader(pempublic))
{
   var pemReader = new PemReader(sr);
   var temp = (RsaKeyParameters)pemReader.ReadObject();

   var RSAKeyInfo = new RSAParameters
   {
      Modulus =  temp.Modulus.ToByteArray(),
      Exponent = temp.Exponent.ToByteArray()
   };

   var rsaEncryptor = new RSACryptoServiceProvider();
   rsaEncryptor.ImportParameters(RSAKeyInfo);
}

没有错误,但加密是不同的。在c#和obj-c中加密的相同字符串不同,我们无法在一端加密,另一端加密。

There are no errors, but the encryption is different. The same string encrypted in c# and obj-c are different, and we are unable to encrypt on one end and decrypt on the other.

帮助!

编辑:愿意考虑在c#和obj-c之间交换公钥的任何方法。这是我们到目前为止最接近的。

Willing to consider any methodology of exchanging public keys between c# and obj-c. This is just the closest we have come so far.

编辑2: pempublic

-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC/ugxekK+lY0VLeD8qA5nEhIn7IzBkgcrpiEM109chFxHobtvWEZbu8TqTIBtIgtISNp4idcEvahPniEyUawjmRSWB7uYmcHJ3pWaIo5/wBthmGrqS/XjedVXT6RuzaoPf9t0YXyW6YiH1kQZn4gjZF51O6iIk2+VnfkYVqeKBtQIDAQAB-----END PUBLIC KEY-----

Edit3:关于填充:C#和obj-c都是使用OEAP填充。

Regarding padding: C# and obj-c are both using OEAP padding.

Edit4:文本如何被加密:c#

How the text is being encrypted: c#

 byte[] testBytes = Encoding.UTF8.GetBytes("1234567890");
 byte[] encryptedBytes = rsaEncryptor.Encrypt(testBytes, true);
 string base64 = Convert.ToBase64String(encryptedBytes);

obj-c

NSString *encrypted = [rsa encrypt:@"1234567890" key:RSAKeyPair.publicKey error:error];

最终编辑:

.NET服务器上的Chilkat加密库。我们现在可以从公共密钥加载RSA加密器,以从.NET,Java或Objective-C客户端生成的XML和PEM格式。如果任何人可以解释为什么.NET RSACryptoServiceProvider不起作用,我们都很好奇。

Solved by using the Chilkat encryption library on the .NET server. We are now able to load an RSA encryptor from a public key in both XML and PEM format generated from a .NET, Java, or Objective-C Client. If anyone could explain why the .NET RSACryptoServiceProvider wouldn't work, we are all quite curious.

推荐答案

我写了RSA和AES实现使用CommonCrypto,实现是为了与.NET互操作

I wrote RSA and AES implementation using CommonCrypto, implementation is done in order to be interoperable with .NET

查看

https://github.com/ozgurshn/EncryptionForiOS

我使用base64编码

I used base64 encoding

.NET端可以

 public string RsaDecryption(byte[] cipherText, string privateKey)
    {
        var cspDecryption = new RSACryptoServiceProvider();

        cspDecryption.FromXmlString(privateKey);

        var bytesPlainTextData = cspDecryption.Decrypt(cipherText, false);

        return Encoding.UTF8.GetString(bytesPlainTextData);
    }

public byte[] RsaEncryption(string plainText, string publicKey)
    {
        var cspEncryption = new RSACryptoServiceProvider();

        cspEncryption.FromXmlString(publicKey);

        var bytesPlainTextData = Encoding.UTF8.GetBytes(plainText);
        var bytesCypherText = cspEncryption.Encrypt(bytesPlainTextData, false);

        return bytesCypherText;
    }

这篇关于将Objective-C RSA公钥导入到c#RSACryptoServiceProvider中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆