0

We need to take Modulus and exponent from RSA keys. I have created my pubilc key using following methodology. Please let me know how can we take modulus and exponent part from it. I have already read this post.

 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);

// Now I want modulus and exponent form this publicKey

EDITED :- I have also send base64 string to server but there we are facing quite a issue to find public key ref from base64 string. if someone has done it in c#, you can also help us with this

c# code snippet

const string pKey = "-----key-----" 
byte[] publicKeyBytes = Convert.FromBase64String(pKey);            
var stream = new MemoryStream(publicKeyBytes);
Asn1Object asn1Object = Asn1Object.FromStream(stream);

Now we need public key component which we are unable to parse. Any help would be great

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Anurag Bhakuni
  • 2,379
  • 26
  • 32
  • and I have also send base64 string to server but there we are facing quite a issue to find public key ref from base64 string. if someone has done it in c#, you can also help us with this – Anurag Bhakuni Dec 06 '18 at 09:20

1 Answers1

0

On C# you can achieve the encryption using this way,

        const string publicKey = "MIGJAoGBAMIt95f4xaP7vYV/+Hdyb4DK0oKvw495PrRYG3nsYgVP7zlBE/rTN6Nmt69W9d0nGefuRlJFIr9TA8vlJmqTus6uXEasBuEjzH7vM7HQeAK6i8qEbVy0T+Uuq+16yy059NL7i/VWljVE6rqTntDUELmbIwNBwj6oBuL1z3SnFoMjAgMBAAE="; //generated on iOS
        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);