我正在编码一个可以生成rsa公共密钥的类,我具有模数和指数,因此我必须创建一个rsa公共密钥。在我编码下一行时,此密钥需要加密一些文本。
我正在开发Swift 4.2
下一行来自这里Create SecKey from modulus and exponent in swift
import Foundation
import Security
class rsaPublicKeyS {
private let MODULUS = "CA458DD21E5B61B7D0F286BD4804AE9471FDD9C81E89AA3020BC9EB8ECA81D646F6540AF155D644BC3D4DD0E2A0798CD899B4737425450F8FFC189D4664A127508BDDA8F3811F38443B05A9DDA8D8CB42A2A0000E019247A7E547743D8D22A4F6D9F4A37934BFB559EDC2107EE9FD67D8FBE6EB8F506FBF13C3C9F0829AEC176DA8C8DDFF5E9392D6E36ACE3289B453268E4551A2F3DE1737CEEB24CFC2375A655F14B77C02D3FB1E1C789849FB16D8D8E0B22D7B5BE3A1140F9BEAA586D00C903B536BB1C3E08CEE8F4B3C9BE1505E5A151006C15204A79B9398C77D3A3B5DDD27615BF7A34CBCEB1345B182D37A2DCF40EB3F51CBD75223569D62D6508C5B7"
private let EXPONENT = "10001"
var modulusArray = [UInt8]()
var exponentArray = [UInt8]()
var sequenceEncoded = [UInt8]()
func convertArray()
{
modulusArray = [
136, 0, 243, 196, 194, 126, 151, 243, 72, 84, 246, 234, 207, 215, 168, 5, 233, 212, 8, 37, 34, 52, 215, 217, 223, 183, 58, 129, 66, 112, 88, 71, 201, 71, 33, 156, 132, 7, 189, 234, 110, 6, 46, 189, 233, 206, 61, 128, 220, 138, 56, 49, 34, 159, 245, 208, 214, 49, 169, 58, 170, 68, 127, 93, 137, 99, 74, 54, 65, 109, 112, 33, 65, 169, 246, 176, 128, 121, 171, 35, 214, 236, 210, 123, 94, 146, 86, 30, 134, 135, 116, 124, 4, 55, 208, 163, 219, 220, 203, 249, 107, 69, 147, 169, 66, 214, 179, 195, 152, 211, 209, 78, 100, 114, 209, 203, 120, 16, 254, 24, 39, 143, 79, 49, 202, 10, 37, 2, 155, 162, 14, 253, 194, 205, 74, 116, 60, 205, 25, 53, 85, 144, 72, 11, 7, 133, 78, 149, 111, 0, 215, 174, 36, 104, 175, 62, 196, 197, 49, 78, 172, 146, 82, 216, 160, 45, 48, 212, 50, 168, 208, 255, 205, 82, 22, 11, 13, 156, 197, 42, 159, 26, 124, 237, 178, 131, 239, 186, 37, 96, 24, 154, 243, 202, 252, 87, 102, 23, 19, 29, 73, 130, 95, 45, 219, 104, 13, 54, 30, 165, 144, 223, 1, 14, 169, 100, 111, 246, 54, 185, 47, 156, 238, 249, 88, 33, 244, 135, 233, 102, 36, 86, 196, 143, 178, 176, 62, 24, 178, 209, 163, 244, 116, 236, 81, 177, 190, 205, 140, 230, 6, 113, 158, 105, 111, 123
]
exponentArray = [
1, 0, 0, 0, 1
]
modulusArray.insert(0x00, at: 0)
}
func encodedME()
{
var modulusEncoded: [UInt8] = []
modulusEncoded.append(0x02)
modulusEncoded.append(contentsOf: lengthField(of: modulusArray))
modulusEncoded.append(contentsOf: modulusArray)
var exponentEncoded: [UInt8] = []
exponentEncoded.append(0x02)
exponentEncoded.append(contentsOf: lengthField(of: exponentArray))
exponentEncoded.append(contentsOf: exponentArray)
sequenceEncoded.append(0x30)
sequenceEncoded.append(contentsOf: lengthField(of: (modulusEncoded + exponentEncoded)))
sequenceEncoded.append(contentsOf: (modulusEncoded + exponentEncoded))
}
func generateKey() -> SecKey
{
convertArray()
encodedME()
let keyData = Data(bytes: sequenceEncoded)
let keySize = (modulusArray.count * 8)
let attributes: [String: Any] = [
kSecAttrKeyType as String: kSecAttrKeyTypeRSA,
kSecAttrKeyClass as String: kSecAttrKeyClassPublic,
kSecAttrKeySizeInBits as String: keySize
]
let publicKey = SecKeyCreateWithData(keyData as CFData, attributes as CFDictionary, nil)
secKeyB64(key: publicKey!)
return publicKey!
}
func lengthField(of valueField: [UInt8]) -> [UInt8] {
var count = valueField.count
if count < 128 {
return [ UInt8(count) ]
}
// The number of bytes needed to encode count.
let lengthBytesCount = Int((log2(Double(count)) / 8) + 1)
// The first byte in the length field encoding the number of remaining bytes.
let firstLengthFieldByte = UInt8(128 + lengthBytesCount)
var lengthField: [UInt8] = []
for _ in 0..<lengthBytesCount {
// Take the last 8 bits of count.
let lengthByte = UInt8(count & 0xff)
// Add them to the length field.
lengthField.insert(lengthByte, at: 0)
// Delete the last 8 bits of count.
count = count >> 8
}
// Include the first byte.
lengthField.insert(firstLengthFieldByte, at: 0)
return lengthField
}
func secKeyB64(key: SecKey)
{
var error:Unmanaged<CFError>?
if let cfdata = SecKeyCopyExternalRepresentation(key, &error) {
let data:Data = cfdata as Data
let b64Key = data.base64EncodedString()
print(b64Key)
}
}
}
在代码中,我有一个常量MODULUS和EXPONENT,用这个常量我必须创建一个rsa公共密钥,但是当我尝试将字符串转换为[Uint8]并执行函数genereteKey时,我在publicKey中得到了零
但是,如果我尝试使用modulationArray和exponentArray,则函数genereteKey会毫无问题地执行,并且我会得到一个secKey
已经存在获取rsa公钥的其他方法吗?还是为什么当我尝试强制转换功能不起作用?