Edit Added a screnshot of the request needed.
编辑添加所需请求的screnshot。
I'm trying to make a MacOs app in Swift 4. This app communicates with a api that requires RSA encryption.
我正在尝试用Swift 4做一个MacOs应用。这个应用程序与需要RSA加密的api通信。
Google didn't give results that could explaining how to do this. Apple's documentation on this subject is quite extensive (https://developer.apple.com/library/content/documentation/Security/Conceptual/CertKeyTrustProgGuide/KeyRead.html#//apple_ref/doc/uid/TP40001358-CH222-SW2), but still not what I need.
谷歌并没有给出可以解释如何做到这一点的结果。苹果关于这个主题的文档非常广泛(https://developer.apple.com/library/content/documentation/Security/Conceptual/CertKeyTrustProgGuide/KeyRead.html#/ apple_ref/doc/uid/TP40001358-CH222-SW2),但仍然不是我需要的。
The function SecKeyCopyExternalRepresentation gives a Data object that cannot be transformed to a String. The documentation says that it is a PCKS #1 response, but I can't work it out.
函数SecKeyCopyExternalRepresentation给出一个不能转换为字符串的数据对象。文档说这是PCKS #1的响应,但是我不能算出来。
I've tried a lot of things, including below, but I can't get it to work.
我尝试了很多东西,包括下面的,但是我不能让它工作。
func externalRepresentation(_ key: SecKey) -> String? {
var error: Unmanaged<CFError>?
guard let data = SecKeyCopyExternalRepresentation(key, &error) as Data? else {
return nil
}
return data.base64EncodedString()
}
The request needs to be the following: Request
请求必须是:request
Is this even possible in Swift?
这在Swift中可能吗?
1 个解决方案
#1
1
The normal way to encode a binary blob, like a certificate or RSA key as a string is to use base64 encoding. You can convert a Data
to base64 quite easily with the function base64EncodedString(options:)
. i.e.
编码二进制blob的正常方法,如证书或RSA密钥作为字符串,是使用base64编码。使用base64EncodedString(选项:)函数,可以很容易地将数据转换为base64。即。
let myString = myData.base64EncodedString()
whether that is exactly what you need for this application is hard to tell because your question doesn't give much context.
这是否正是您需要的应用程序是很难判断的,因为您的问题没有给出太多的上下文。
Looking at your screen shot, as well as the base64 encoded string, you need a header and footer. Most of the apparently random letters in the data structure are the base64 string (the JSON conversion has encoded the line feeds with \n
and something else has doubled up the backslashes). Your last step is therefore to prepend the string with -----BEGIN PUBLIC KEY-----
and a new line, ad append a new line and -----END PUBLIC KEY-----
查看您的屏幕快照,以及base64编码的字符串,您需要一个页眉和页脚。数据结构中的大多数显然是随机的字母是base64字符串(JSON转换用\n编码了换行,其他东西将反斜杠加倍)。
One more thing: you can get the original data back from the base64 string quite easily with Data.init?((base64Encoded base64String:,options:). i.e.
还有一件事:使用data .init可以很容易地从base64字符串获取原始数据。((base64Encoded base64String:选择:)。即。
guard let myDataCopy = Data(base64Encoded: myString)
else { fatalError("the string was not really base64") }
#1
1
The normal way to encode a binary blob, like a certificate or RSA key as a string is to use base64 encoding. You can convert a Data
to base64 quite easily with the function base64EncodedString(options:)
. i.e.
编码二进制blob的正常方法,如证书或RSA密钥作为字符串,是使用base64编码。使用base64EncodedString(选项:)函数,可以很容易地将数据转换为base64。即。
let myString = myData.base64EncodedString()
whether that is exactly what you need for this application is hard to tell because your question doesn't give much context.
这是否正是您需要的应用程序是很难判断的,因为您的问题没有给出太多的上下文。
Looking at your screen shot, as well as the base64 encoded string, you need a header and footer. Most of the apparently random letters in the data structure are the base64 string (the JSON conversion has encoded the line feeds with \n
and something else has doubled up the backslashes). Your last step is therefore to prepend the string with -----BEGIN PUBLIC KEY-----
and a new line, ad append a new line and -----END PUBLIC KEY-----
查看您的屏幕快照,以及base64编码的字符串,您需要一个页眉和页脚。数据结构中的大多数显然是随机的字母是base64字符串(JSON转换用\n编码了换行,其他东西将反斜杠加倍)。
One more thing: you can get the original data back from the base64 string quite easily with Data.init?((base64Encoded base64String:,options:). i.e.
还有一件事:使用data .init可以很容易地从base64字符串获取原始数据。((base64Encoded base64String:选择:)。即。
guard let myDataCopy = Data(base64Encoded: myString)
else { fatalError("the string was not really base64") }