首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于C#生成公钥的iOS RSA加密

基于C#生成公钥的iOS RSA加密
EN

Stack Overflow用户
提问于 2018-12-06 11:17:10
回答 1查看 236关注 0票数 0

我试图在iOS和C#之间建立RSA加密通道。

在iOS中,使用安全框架生成公钥。

代码片段,

代码语言:javascript
复制
NSData* tag = [@"com.x.x.x" dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary* attributes =
    @{ (id)kSecAttrKeyType:               (id)kSecAttrKeyTypeRSA,
       (id)kSecAttrKeySizeInBits:         @1024,
       (id)kSecPrivateKeyAttrs:
           @{ (id)kSecAttrIsPermanent:    @YES,
              (id)kSecAttrApplicationTag: tag,
              },
       };

    CFErrorRef error = NULL;
    SecKeyRef privateKey = SecKeyCreateRandomKey((__bridge CFDictionaryRef)attributes,
                                                 &error);
    if (!privateKey) {
        NSError *err = CFBridgingRelease(error); 
        // Handle the error. . .
    }

    SecKeyRef publicKey = SecKeyCopyPublicKey(privateKey);

结果:"MIGJAoGBAMIt95f4xaP7vYV/+Hdyb4DK0oKvw495PrRYG3nsYgVP7zlBE/rTN6Nmt69W9d0nGefuRlJFIr9TA8vlJmqTus6uXEasBuEjzH7vM7HQeAK6i8qEbVy0T+Uuq+16yy059NL7i/VWljVE6rqTntDUELmbIwNBwj6oBuL1z3SnFoMjAgMBAAE="

在C#中,我试图使用上面生成的公钥来加密数据。

C#代码片段:

代码语言:javascript
复制
const string pKey = "MIGJAoGBAMIt95f4xaP7vYV/+Hdyb4DK0oKvw495PrRYG3nsYgVP7zlBE/rTN6Nmt69W9d0nGefuRlJFIr9TA8vlJmqTus6uXEasBuEjzH7vM7HQeAK6i8qEbVy0T+Uuq+16yy059NL7i/VWljVE6rqTntDUELmbIwNBwj6oBuL1z3SnFoMjAgMBAAE=";
            byte[] publicKeyBytes = Convert.FromBase64String(pKey);              
            var stream = new MemoryStream(publicKeyBytes);
            Asn1Object asn1Object = Asn1Object.FromStream(stream);

我正在评估asn1Object并手动复制ModulusExponent,在本例中,

代码语言:javascript
复制
BigInteger modulus = new BigInteger("136357523654993073600337541886612209461653649830596179928022807129764638978058775447065286245685299741061442447812373893408612622888251899821604061796495147997132112844250946475016809624300789149318149170549447812758865778797994482272574020014042899233361299524505365746896235948770695552862804390518105473827");
            BigInteger exponent = new BigInteger("65537");

然后我就能用,

代码语言:javascript
复制
AsymmetricKeyParameter param = new RsaKeyParameters(false, modulus, exponent);
 RsaKeyParameters rsaKeyParameters = (RsaKeyParameters)param;
            RSAParameters rsaParameters = new RSAParameters();
 rsaParameters.Modulus = rsaKeyParameters.Modulus.ToByteArrayUnsigned();
            rsaParameters.Exponent = rsaKeyParameters.Exponent.ToByteArrayUnsigned();  
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            rsa.ImportParameters(rsaParameters);

Q1。有任何方法来迭代ans1Object并获得相应的数据吗?

Q2。有任何方法直接使用iOS生成的Base64(公钥)在C#中吗?

Q3。我需要将相同格式的公钥发送回客户端。

如能提供任何指导和帮助,将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-06 12:16:15

所以如此,

经过这么多的研究和100年代的堆栈溢出标签。

我能够使用iOS在C#中生成的公钥进行加密。

代码语言:javascript
复制
pkey : Public Key given from client

下面的代码片段:

代码语言:javascript
复制
            const string pKey = "MIGJAoGBAMIt95f4xaP7vYV/+Hdyb4DK0oKvw495PrRYG3nsYgVP7zlBE/rTN6Nmt69W9d0nGefuRlJFIr9TA8vlJmqTus6uXEasBuEjzH7vM7HQeAK6i8qEbVy0T+Uuq+16yy059NL7i/VWljVE6rqTntDUELmbIwNBwj6oBuL1z3SnFoMjAgMBAAE=";
            byte[] publicKeyBytes = Convert.FromBase64String(pKey);

            var stream = new MemoryStream(publicKeyBytes);
            Asn1Object asn1Object = Asn1Object.FromStream(stream);
            Asn1Encodable asn1Sequence = asn1Object;   

            AlgorithmIdentifier algorithmIdentifier = new 
            AlgorithmIdentifier(PkcsObjectIdentifiers.IdRsaesOaep);  

            SubjectPublicKeyInfo subjectPublicKeyInfo = new 
            SubjectPublicKeyInfo(algorithmIdentifier, asn1Sequence);   

            AsymmetricKeyParameter asymmetricKeyParameter2 = 
            PublicKeyFactory.CreateKey(subjectPublicKeyInfo);    

            RsaKeyParameters rsaKeyParameters = 
            (RsaKeyParameters)asymmetricKeyParameter2;
            RSAParameters rsaParameters = new RSAParameters();
            rsaParameters.Modulus = rsaKeyParameters.Modulus.ToByteArrayUnsigned();
            rsaParameters.Exponent = rsaKeyParameters.Exponent.ToByteArrayUnsigned();

            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            rsa.ImportParameters(rsaParameters);
            string test = "John snow is the true king";
            byte[] encbyte = rsa.Encrypt(Encoding.UTF8.GetBytes(test), RSAEncryptionPadding.Pkcs1);
            string encrt = Convert.ToBase64String(encbyte);

从现在起,代码还没有很好的优化,所以请忽略它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53650240

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档