ios – 如何在Objective c中使用AES Crypto发送CommonCrypto POST请求?

ios – 如何在Objective c中使用AES Crypto发送CommonCrypto POST请求?,第1张

概述我只是尝试从iOS应用程序向目标c中的php平台Web服务发送安全请求.我尝试了2天,但我没有找到逻辑或任何如何实现这一点: 以下是使用CryptoSwift Framework的Swift代码 func HTTPPostJSON(url: String, jsonData: Dictionary<String, AnyObject>, type: String = "POST", encrypt 我只是尝试从iOS应用程序向目标c中的PHP平台Web服务发送安全请求.我尝试了2天,但我没有找到逻辑或任何如何实现这一点:

以下是使用CryptoSwift Framework的Swift代码

func httpPostJsON(url: String,JsonData: Dictionary<String,AnyObject>,type: String = "POST",encrypt: Bool = false,callback: (String,String?) -> VoID) {        if DeBUG().state {            print("** Start http")        }        crypto_enabled = encrypt        let req = NSMutableURLRequest(URL: NSURL(string: url)!)        req.httpMethod = type        req.addValue(self.dataType,forhttpheaderFIEld: self.headerType)        let Json: JsON = JsON(JsonData)        //        var data: NSData = NSJsONSerialization.dataWithJsONObject(Json.object,options: nil,error: nil)!        var data: NSData = NSData()        do {            data = try NSJsONSerialization.dataWithJsONObject(Json.object,options: NSJsONWritingOptions.init(rawValue: 0))        } catch {            print("JsON to NSData error: \(error)")        }        if DeBUG().state {            print("JsON Object: \(Json)")        }        if crypto_enabled {            if DeBUG().state {                print("Encryption enabled")            }            let iv = Cipher.randomIV(AES.blockSize)            //            println("UInt8 IV: \(iv)")            let iv_size = iv.count //count(iv)            print("IV Size: \(iv_size)")            var key = [UInt8] (self.secret.utf8)            //            println("UInt8 Key: \(key)")            let secret_len = self.secret.characters.count            print("Key length: \(secret_len)")            if self.secret.characters.count != 32 {                if DeBUG().state {                    print("Hashing Secret")                }                let data: NSData = self.secret.dataUsingEnCoding(NSUTF8StringEnCoding,allowLossyConversion: false)!                //                key = (CryptoSwift.Hash.md5(data).calculate()!).arrayOfBytes()                key = (CryptoSwift.Hash.sha256(data).calculate()!).arrayOfBytes()                if DeBUG().state {                    print("New UInt8 Key: \(key)")                    let new_key_len = key.count                    print("New Key Length: \(new_key_len)")                }            }            let aes = AES(key: key,iv: iv,blockMode: .CBC)!            var encrypted: [UInt8] = []            do {                encrypted = try aes.encrypt(data.arrayOfBytes())            } catch {                print("Encrypt data Failed: \(error)")            }            // IV            let ivData: NSData = NSData.withBytes(iv)            //            println("IV in NSData: \(ivData)\n")            let ivBase64 = ivData.base64EncodedDataWithOptions(NSDataBase64EnCodingOptions(rawValue: 0))            //            println("IV in Base64: \(ivBase64)\n")            let ivBase64String = Nsstring(data: ivBase64,enCoding: NSUTF8StringEnCoding) as! String            //            println("IV in Base64 String: \(ivBase64String)\n")            // cdata            let cdata_Data = NSData.withBytes(encrypted)            // 1st cdata Base64 enCoding            let cdata_Base64 = cdata_Data.base64EncodedDataWithOptions(NSDataBase64EnCodingOptions(rawValue: 0))            let cdata_Base64String = Nsstring(data: cdata_Base64,enCoding: NSUTF8StringEnCoding) as! String            // 2nd cdata Base64 enCoding            let cdata_Base64String_Data = cdata_Base64String.dataUsingEnCoding(NSUTF8StringEnCoding,allowLossyConversion: false)!            let cdata_L2_Base64 = cdata_Base64String_Data.base64EncodedDataWithOptions(NSDataBase64EnCodingOptions(rawValue: 0))            let cdata_L2_Base64String = Nsstring(data: cdata_L2_Base64,enCoding: NSUTF8StringEnCoding) as! String            let cipheredDict: Dictionary<String,AnyObject> = [                "iv": ivBase64String,"cdata": cdata_L2_Base64String // cdata_Base64String            ]            var cipheredJsON: JsON = JsON(cipheredDict)            //            let cipheredData: NSData = NSJsONSerialization.dataWithJsONObject(cipheredJsON.object,error: nil)!            var cipheredData: NSData = NSData()            do {                cipheredData = try NSJsONSerialization.dataWithJsONObject(cipheredJsON.object,options: NSJsONWritingOptions.init(rawValue: 0))            } catch {                print("Ciphered JsON to NSData error: \(error)")            }            //            if DeBUG().state {            //                println("Ciphered Data: \(cipheredData)")            //            }            data = cipheredData        }        //let JsonString = Json //self.JsONStringify(Json)        //let data: NSData = JsonString.dataUsingEnCoding(NSUTF8StringEnCoding,allowLossyConversion: false)!        req.httpBody = data        self.httpSendReq(req,callback: callback)    }

我尝试使用MIHCrypto在目标c中实现

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://00.00.00.000/member/slogin"]];    NSMutableDictionary *parameters = [NSMutableDictionary new];    [parameters setobject:@"POS" forKey:@"from"];    [parameters setobject:@"52001" forKey:@"username"];    [parameters setobject:@"111111" forKey:@"password"];    // Specify that it will be a POST request    request.httpMethod = @"POST";    // This is how we set header fIElds    [request setValue:@"application/Json" forhttpheaderFIEld:@"Content-Type"];     NSError *JsonSerializationError = nil;    Nsstring*secrate = @"keythatuser";    // Convert your data and set your request's httpBody property  NSData *JsonData = [NSJsONSerialization dataWithJsONObject:parameters options:NSJsONWritingPrettyPrinted error:&JsonSerializationError];    NSError *error;    NSData *encryptedData = [RNEncryptor encryptData:JsonData                                        withSettings:kRNCryptorAES256Settings                                            password:secrate                                               error:&error];    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];    [conn start];

坦率地说,我不知道如何解析它,请指导我加密PHP服务器请求和解密后的加密响应.

解决方法 我们可以通过在Objective-C项目中创建一个Swift辅助类来实现.

使用安装pod在您的项目中

platform :ios,'8.0'use_frameworks!target 'CryptoTest' do    pod 'CryptoSwift'end

添加带有桥接头的帮助器swift文件

为此,请转到设置>包装>定义Module = True

现在在Helper文件中导入CryptoSwift

例如 :

////  CryptoHelper.swift//  CryptoTestimport UIKitimport CryptoSwiftclass CryptoHelper: NSObject {    func crypttest(){        /* Hash enum usage */        let input:[UInt8] = [49,50,51]        let output = input.md5()        // alternatively: let output = CryptoSwift.Hash.md5(input).calculate()        print(output.toHexString())    }}

现在在任何Objective-C(.m)文件中使用该帮助文件

例如:

#import "VIEwController.h"#import "CryptoTest-Swift.h"@interface VIEwController ()@end@implementation VIEwController- (voID)vIEwDIDLoad {    [super vIEwDIDLoad];    CryptoHelper *testCrypt = [[CryptoHelper alloc]init];    [testCrypt cryptTest];    // Do any additional setup after loading the vIEw,typically from a nib.}- (voID)dIDReceiveMemoryWarning {    [super dIDReceiveMemoryWarning];    // dispose of any resources that can be recreated.}@end

这里#import“CryptoTest-Swift.h”表示#import“YourProjectModulename-Swift.h”.

希望它能帮到你.

总结

以上是内存溢出为你收集整理的ios – 如何在Objective c中使用AES Crypto发送CommonCrypto POST请求?全部内容,希望文章能够帮你解决ios – 如何在Objective c中使用AES Crypto发送CommonCrypto POST请求?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1048204.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存