我正在尝试使用公钥/私钥系统加密字符串.我需要将一个字符串传递给服务器,PHP 脚本可以在其中解密它.我有一个可运行的 JavaScript 到 PHP 脚本系统,但是,我想让 PHP 脚本也可以接受来自 iOS 应用程序的数据.任何人都可以指出我正确的方向,或者甚至可以向我介绍一个可以实现类似结果的系统.我正在寻找可用于用 PHP 解密的任何类型的加密.先感谢您.
PHP 和 iOS 都支持 AES 加密.
在 iOS 上,Common Crypto 支持非对称 (RSA) 和对称 (AES) 加密.如果没有对 PKI(公钥基础设施)的强制性需求,请使用 AES.
唯一的问题是 PHP 使用非标准填充 (null) 而不是 PKCS#7,因此您必须在 iOS 大小上进行自己的填充以匹配 PHP 使用的 Bozo 方案或在 PHP 端进行匹配标准的 PKCS#7.请注意,如果数据的最后一个字节为 0,则 PHP 填充失败.
这是一个封装在带有 iv 的函数中的实现示例:
斯威夫特 2.0
<块引用>将 Security.framework
添加到项目中.
#import NSData?{让 keyBytes = UnsafePointer(keyData.bytes)print("keyLength = (keyData.length), keyData = (keyData)")让 ivBytes = UnsafePointer(ivData.bytes)打印(ivLength = (ivData.length),ivData = (ivData)")让 dataLength = Int(data.length)让 dataBytes = UnsafePointer(data.bytes)print("dataLength = (dataLength), data = (data)")让 cryptData:NSMutableData!= NSMutableData(length: Int(dataLength) + kCCBlockSizeAES128)让 cryptPointer = UnsafeMutablePointer(cryptData.mutableBytes)让 cryptLength = size_t(cryptData.length)让 keyLength = size_t(kCCKeySizeAES128)让算法:CCAlgorithm = UInt32(kCCAlgorithmAES128)让选项:CCOptions = UInt32(kCCOptionPKCS7Padding)var numBytesEncrypted:size_t = 0让 cryptStatus = CCCrypt(操作,算法,选项,密钥字节,密钥长度,ivBytes,数据字节,数据长度,cryptPointer, cryptLength,&numBytesEncrypted)如果 UInt32(cryptStatus) == UInt32(kCCSuccess) {cryptData.length = Int(numBytesEncrypted)打印(cryptLength = (numBytesEncrypted),cryptData = (cryptData)")} 别的 {打印(错误:(cryptStatus)")}返回 cryptData;}
//测试代码:
let keyString = "!使用数据键!"让 keyData = (keyString as NSString).dataUsingEncoding(NSUTF8StringEncoding) 作为 NSData!let ivString = "使用随机 iv!"让 ivData = (keyString as NSString).dataUsingEncoding(NSUTF8StringEncoding) 作为 NSData!let message = "不要试图阅读这篇文章.绝密的东西"让 data = (message as NSString).dataUsingEncoding(NSUTF8StringEncoding) as NSData!打印(数据:(数据)")if let encryptedData = testCrypt(data, keyData:keyData, ivData:ivData, operation:UInt32(kCCEncrypt)) {打印(加密数据:(加密数据)")如果让解密数据 = testCrypt(encryptedData, keyData:keyData, ivData:ivData, operation:UInt32(kCCDecrypt)) {打印(解密数据:(解密数据)")}}
输出:
<前>数据:446f6ec2 b4742074 72792074 6f207265 61642074 68697320 74657874 2e20546f 70205365 63726574 2056664 7keyLength = 16,keyData = 21557365 20612064 61746120 6b657921ivLength = 16, ivData = 21557365 20612064 61746120 6b657921数据长度 = 46,数据 = 446f6ec2 b4742074 72792074 6f207265 61642074 68697320 74657874 2e20546f 70205365 637205637646cryptLength = 48, cryptData = c184cb8b 3d24b56f 1e2896c4 a933a824 d5f8820b 9e7549c6 4188594c 8c1e5941 67adbc80 4206df8586加密数据:c184cb8b 3d24b56f 1e2896c4 a933a824 d5f8820b 9e7549c6 4188594c 8c1e5941 67adbc80 420bc362 0d8856d4keyLength = 16,keyData = 21557365 20612064 61746120 6b657921ivLength = 16, ivData = 21557365 20612064 61746120 6b657921数据长度 = 48,数据 = c184cb8b 3d24b56f 1e2896c4 a933a824 d5f8820b 9e7549c6 4188594c 8c1e5941 67adbc80 420bc858d6f60cryptLength = 46, cryptData = 446f6ec2 b4742074 72792074 6f207265 61642074 68697320 74657874 2e20546f 70205365 646726563解密数据:446f6ec2 b4742074 72792074 6f207265 61642074 68697320 74657874 2e20546f 70205365 63726574 2566574SO 上还有其他 Swift AES 加密示例.
ECB 示例 SO 答案 包裹在函数中,但您应该真正使用 CBC 模式.
CBC 示例 SO 答案代码.
I am trying to encrypt a string, using Public/Private Key system. I need to pass a string to the server, where a PHP script can decrypt it. I have a working JavaScript to PHP script system, however, I'd like to make it possible for the PHP script to also accept data from the iOS app. Can anyone point me in the right direction, or perhaps even introduce me to a system which could achieve a similar result. I am looking for any type of encryption that I could use to decrypt with PHP. Thank you in advance.
PHP and iOS both support AES encryption.
On iOS Common Crypto supports both asymmetric (RSA) and symmetric (AES) encryption. If there is not a compelling need for PKI (Public key infrastructure) use AES.
The only issue is that PHP uses non-standard padding (null) instead of PKCS#7 so you will have to do your own padding either on the iOS size to match the Bozo scheme PHP uses or on the PHP side to match the standard PKCS#7. Note the PHP padding fails if the last byte of the data is 0.
Here is an implementation example wrapped in a function with an iv:
Swift 2.0
Add the
Security.framework
to the project.
#import <CommonCrypto/CommonCrypto.h
func testCrypt(data:NSData, keyData:NSData, ivData:NSData, operation:CCOperation) -> NSData? {
let keyBytes = UnsafePointer<UInt8>(keyData.bytes)
print("keyLength = (keyData.length), keyData = (keyData)")
let ivBytes = UnsafePointer<UInt8>(ivData.bytes)
print("ivLength = (ivData.length), ivData = (ivData)")
let dataLength = Int(data.length)
let dataBytes = UnsafePointer<UInt8>(data.bytes)
print("dataLength = (dataLength), data = (data)")
let cryptData: NSMutableData! = NSMutableData(length: Int(dataLength) + kCCBlockSizeAES128)
let cryptPointer = UnsafeMutablePointer<UInt8>(cryptData.mutableBytes)
let cryptLength = size_t(cryptData.length)
let keyLength = size_t(kCCKeySizeAES128)
let algoritm: CCAlgorithm = UInt32(kCCAlgorithmAES128)
let options: CCOptions = UInt32(kCCOptionPKCS7Padding)
var numBytesEncrypted :size_t = 0
let cryptStatus = CCCrypt(operation,
algoritm,
options,
keyBytes, keyLength,
ivBytes,
dataBytes, dataLength,
cryptPointer, cryptLength,
&numBytesEncrypted)
if UInt32(cryptStatus) == UInt32(kCCSuccess) {
cryptData.length = Int(numBytesEncrypted)
print("cryptLength = (numBytesEncrypted), cryptData = (cryptData)")
} else {
print("Error: (cryptStatus)")
}
return cryptData;
}
// Test code:
let keyString = "!Use a data key!"
let keyData = (keyString as NSString).dataUsingEncoding(NSUTF8StringEncoding) as NSData!
let ivString = "Use a random iv!"
let ivData = (keyString as NSString).dataUsingEncoding(NSUTF8StringEncoding) as NSData!
let message = "Don´t try to read this text. Top Secret Stuff"
let data = (message as NSString).dataUsingEncoding(NSUTF8StringEncoding) as NSData!
print("data: (data)")
if let encryptedData = testCrypt(data, keyData:keyData, ivData:ivData, operation:UInt32(kCCEncrypt)) {
print("encryptedData: (encryptedData)")
if let decryptedData = testCrypt(encryptedData, keyData:keyData, ivData:ivData, operation:UInt32(kCCDecrypt)) {
print("decryptedData: (decryptedData)")
}
}
Output:
data: 446f6ec2 b4742074 72792074 6f207265 61642074 68697320 74657874 2e20546f 70205365 63726574 20537475 6666 keyLength = 16, keyData = 21557365 20612064 61746120 6b657921 ivLength = 16, ivData = 21557365 20612064 61746120 6b657921 dataLength = 46, data = 446f6ec2 b4742074 72792074 6f207265 61642074 68697320 74657874 2e20546f 70205365 63726574 20537475 6666 cryptLength = 48, cryptData = c184cb8b 3d24b56f 1e2896c4 a933a824 d5f8820b 9e7549c6 4188594c 8c1e5941 67adbc80 420bc362 0d851d4f f88d6675 encryptedData: c184cb8b 3d24b56f 1e2896c4 a933a824 d5f8820b 9e7549c6 4188594c 8c1e5941 67adbc80 420bc362 0d851d4f f88d6675 keyLength = 16, keyData = 21557365 20612064 61746120 6b657921 ivLength = 16, ivData = 21557365 20612064 61746120 6b657921 dataLength = 48, data = c184cb8b 3d24b56f 1e2896c4 a933a824 d5f8820b 9e7549c6 4188594c 8c1e5941 67adbc80 420bc362 0d851d4f f88d6675 cryptLength = 46, cryptData = 446f6ec2 b4742074 72792074 6f207265 61642074 68697320 74657874 2e20546f 70205365 63726574 20537475 6666 decryptedData: 446f6ec2 b4742074 72792074 6f207265 61642074 68697320 74657874 2e20546f 70205365 63726574 20537475 6666
There are other examples of Swift AES encryption here on SO.
ECB example SO answer wrapped in a function, but you should really use CBC mode.
CBC example SO answer code.
这篇关于Swift 中的异步加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!