问题描述
我想使用RSA加密数据,我试图在我的代码中生成密钥并且它正在工作,但我真正需要的是将公钥作为字符串从服务器获取,然后将其用作Seckey,以便我可以使用它使用RSA加密数据,
我尝试了这段代码:
I want to encrypt data using RSA , I tried to generate the key in my code and it's working , But what I actually need is to get the public key as a string from server and then use it as Seckey so I can use it to encrypt data using RSA,I tried this code:
//KeyString is the string of the key from server
let KeyData = (keyString as NSString).dataUsingEncoding(NSUTF8StringEncoding) as NSData!
var cert : Unmanaged<SecCertificateRef>!;
var policy : Unmanaged<SecPolicy>!;
cert = SecCertificateCreateWithData(kCFAllocatorDefault, KeyData);
policy = SecPolicyCreateBasicX509();
var status : OSStatus = noErr
var trust: SecTrust?
var certArray : [Unmanaged<SecCertificateRef>!] = [cert];
var certArrayPointer = UnsafeMutablePointer<UnsafePointer<Void>>(certArray)
status = SecTrustCreateWithCertificates(cert, policy, trust);
let publicKey: SecKeyRef = SecTrustCopyPublicKey(trust!).takeUnretainedValue()
我无法'运行此代码,因为SecTrustCreateWithCertificates方法期望证书为anyObject! ,我不知道如何解决这个问题,如果解决这个问题会让我获得SecKey。
I couldn't run this code because SecTrustCreateWithCertificates Method is expecting certificate as anyObject! , I don't Know how to fix this,And if solving this will let me get the SecKey.
我从
所以如果任何人都可以帮我找到合适的代码来解决这个问题,我将非常感谢:)
So if any one can help me getting the right code to solve this , I will be very thankful :)
推荐答案
对于mac:
For mac:
let pubKey = "-----BEGIN PUBLIC KEY-----MIICIjANBgAgK.......InbFk1FkucQqruMyUCAwEAAQ==-----END PUBLIC KEY-----"
let pubKeyData = pubKey.dataUsingEncoding(NSASCIIStringEncoding)
var error: Unmanaged<CFErrorRef>?
let secKey = SecKeyCreateFromData(NSDictionary(), pubKeyData!, &error)
pubKey是公钥的字符串表示形式。
如果您不知道公钥,可以使用以下命令从私钥推断:
Where pubKey is a string representation of your public key.If you don't know your public key, you can infer it from your private key with the following command:
openssl rsa -in server.key -pubout > mykey.pub
其中server.key是包含的文件---- -BEGIN RSA私钥-----
作为第一行。
Where server.key is the file containing -----BEGIN RSA PRIVATE KEY-----
as the first line.
这有点复杂。
你需要一个 der
文件。它是您的证书的二进制表示。
如果您需要转换现有证书,可以使用以下命令进行转换:
It's a bit more complicate.You need a der
file. It's a binary representation of your certificate.If you need to convert an existing certificate, you can do so with the following command:
openssl x509 -outform der -in file.crt|pem -out mycert.der
。 crt
或 .pem
文件包含 ----- BEGIN CERTIFICATE -----
作为第一行。
The .crt
or .pem
file contains -----BEGIN CERTIFICATE-----
as the first line.
将 der
文件放入捆绑包中并执行:
Put the der
file in your bundle and do:
let certificateData = NSData(contentsOfURL:NSBundle.mainBundle().URLForResource("mycert", withExtension: "der")!)
let certificate = SecCertificateCreateWithData(nil, certificateData!)
var trust: SecTrustRef?
let policy = SecPolicyCreateBasicX509()
let status = SecTrustCreateWithCertificates(certificate!, policy, &trust)
if status == errSecSuccess {
let key = SecTrustCopyPublicKey(trust!)!;
}
Yatta!密钥现在包含公钥的 SecKey
表示。快乐固定。
Yatta ! Key now contains a SecKey
representation of your public key. Happy Pinning.
这篇关于来自Swift中服务器的公钥字符串的Seckey的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!