我们需要从RSA密钥中获取模数和指数。我已经使用以下方法创建了我的pubilc密钥。请告诉我如何从中提取模和指数部分。我已经读过这篇文章了。
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);
//现在我想要这个公钥的模和指数
编辑:-我还将base64字符串发送到服务器,但在那里我们面临着从base64字符串中查找公钥ref的问题。如果有人在c#中做过,你也可以帮助我们使用这个
c#代码段
const string pKey = "-----key-----"
byte[] publicKeyBytes = Convert.FromBase64String(pKey);
var stream = new MemoryStream(publicKeyBytes);
Asn1Object asn1Object = Asn1Object.FromStream(stream);
现在我们需要无法解析的公钥组件。任何帮助都将是伟大的
在C#上,您可以使用这种方式实现加密,
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);