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EKbx7aT3Ji8TZCDAfDMbhxvxOf
dPMBDjxP5X3zr7cWgwIDAQAB
-----END PUBLIC KEY-----
如果解密出的结果像是下面这样,应该是对的:
{"2658844581501957149":{"file_name":"wwdc2023-10117_hd.mp4","file_size":"100597894","pick_code":"b12x4iiejvkkq83a","url":{"url":"http:\/\/xxx.xxx.com\/xxx\/wwdc2023-10117_hd.mp4?t=1690607362&u=101248331&s=1048576&d=658578066-b12x4iiejvkkq83a-0&c=2&f=1&k=ddb3d920081771b2f40ee75164c5bb1b&us=52428800&uc=10&v=1","client":2,"desc":null,"isp":null,"oss_id":"fhnfile\/xxx","ooid":""}}}
注意由于加密内容数据比较大,需要分段解密。
不管用什么方法,python/go/java 还是网上的在线解密工具,只要能解密出来即可。
我用 swift 写了分段解密,结果解密失败,用网上在线的解密工具也是失败,不知道网上的工具是不是由于不支持分段解密才失败的。
下面是我用 Swift 写的方法:
import Security
func convertPublicKey(pemString: String) -> SecKey? {
// 1. Remove headers, newlines and spaces
let keyString = pemString
.replacingOccurrences(of: "-----BEGIN PUBLIC KEY-----", with: "")
.replacingOccurrences(of: "-----END PUBLIC KEY-----", with: "")
.replacingOccurrences(of: "\n", with: "")
.replacingOccurrences(of: " ", with: "")
// 2. Convert base64 string to Data
guard let keyData = Data(base64Encoded: keyString) else {
return nil
}
// 3. Create key dictionary
let keyDict: [CFString: Any] = [
kSecAttrKeyType: kSecAttrKeyTypeRSA,
kSecAttrKeyClass: kSecAttrKeyClassPublic,
kSecAttrKeySizeInBits: NSNumber(value: 2048),
kSecReturnPersistentRef: true
]
// 4. Create SecKey from Data
var error: Unmanaged<CFError>?
guard let key = SecKeyCreateWithData(keyData as CFData, keyDict as CFDictionary, &error) else {
// Handle the error here
return nil
}
return key
}
let rsaKeyInSwift = convertPublicKey(pemString: rsaPublicKey)
func rsaDecrypt(data: Data) -> Data? {
let blockSize = SecKeyGetBlockSize(rsaKeyInSwift!)
var decryptedData = Data()
for i in stride(from: 0, to: data.count, by: blockSize) {
let endIndex = min(i + blockSize, data.count)
let chunk = data[i..<endIndex]
print(chunk)
var error: Unmanaged<CFError>?
guard let decryptedChunk = SecKeyCreateDecryptedData(
rsaKeyInSwift!,
.rsaEncryptionPKCS1,
chunk as CFData,
&error
) else {
print("Decryption error:", error.debugDescription)
return nil
}
decryptedData.append(decryptedChunk as Data)
}
return decryptedData
}
报错:
Decryption error: Optional(Swift.Unmanaged<__C.CFErrorRef>(_value: Error Domain=NSOSStatusErrorDomain Code=-50 "RSAdecrypt wrong input (err -27)" UserInfo={numberOfErrorsDeep=0, NSDescription=RSAdecrypt wrong input (err -27)}))
在网上找到了一个开源的项目,它用 go 语言自己实现了 rsa 解密算法,不清楚为什么它不用标准库来解密。我用标准库实现的( Swift 版本)无法解密。所以不清楚是不是我的代码问题还是其他问题。
下面是 go 语言开源版本:
func rsaDecrypt(input []byte) []byte {
output := make([]byte, 0)
cipherSize, blockSize := len(input), rsaServerKey.Size()
for offset := 0; offset < cipherSize; offset += blockSize {
sliceSize := blockSize
if offset+sliceSize > cipherSize {
sliceSize = cipherSize - offset
}
n := big.NewInt(0).SetBytes(input[offset : offset+sliceSize])
m := big.NewInt(0).Exp(n, big.NewInt(int64(rsaServerKey.E)), rsaServerKey.N)
b := m.Bytes()
index := bytes.IndexByte(b, '\x00')
if index < 0 {
return nil
}
output = append(output, b[index+1:]...)
}
return output
}
由于不熟悉 go 语言,大概看了下算法,也是计算 (n^e) mod N
。
所以请大家用这个公钥解密上面的内容,能否解密出来?
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.