我需要用php加密和解密由这里解释的ios方法生成的字符串代码https://stackoverflow.com/a/9479657/721253
使用php进行加密我使用该脚本
$key = 'f968f8e82961489a8b14b345';
$data = 'odio quando sto studiando e un velociraptor mi lancia addosso banane';
$encrypted = null;
$m = mcrypt_module_open(MCRYPT_3DES, null, MCRYPT_MODE_ECB, null);
$fake_iv = str_repeat(chr(0), mcrypt_enc_get_iv_size($m));
mcrypt_generic_init($m, $key, $fake_iv);
$encrypted = mcrypt_generic($m, $data);
die(base64_encode($encrypted));
和IOs for decript i使用
NSURL *url = [NSURL URLWithString:@"http://oscurodrago.it/tools/crypto.php"];
NSData *data = [NSData dataWithContentsOfURL:url];
NSData *outData = [self TripleDES:data encryptOrDecrypt:kCCDecrypt key:@"f968f8e82961489a8b14b345"];
ccStatus返回解码错误 - > if(ccStatus == kCCDecodeError)NSLog(@“DECODE ERROR”);
更新 这是viewcontroller.m,我在那里测试那个脚本
#import <CommonCrypto/CommonCryptor.h>
#import "GTMBase64.h"
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (NSData*)TripleDES:(NSData*)plainData encryptOrDecrypt:(CCOperation)encryptOrDecrypt key:(NSString*)key {
const void *vplainText;
size_t plainTextBufferSize;
if (encryptOrDecrypt == kCCDecrypt)
{
NSData *EncryptData = [GTMBase64 decodeData:plainData];
plainTextBufferSize = [EncryptData length];
vplainText = [EncryptData bytes];
}
else
{
plainTextBufferSize = [plainData length];
vplainText = (const void *)[plainData bytes];
}
CCCryptorStatus ccStatus;
uint8_t *bufferPtr = NULL;
size_t bufferPtrSize = 0;
size_t movedBytes = 0;
// uint8_t ivkCCBlockSize3DES;
bufferPtrSize = (plainTextBufferSize + kCCBlockSize3DES) & ~(kCCBlockSize3DES - 1);
bufferPtr = malloc( bufferPtrSize * sizeof(uint8_t));
memset((void *)bufferPtr, 0x0, bufferPtrSize);
// memset((void *) iv, 0x0, (size_t) sizeof(iv));
// NSString *key = @"123456789012345678901234";
NSString *initVec = @"init Vec";
const void *vkey = (const void *) [key UTF8String];
const void *vinitVec = (const void *) [initVec UTF8String];
ccStatus = CCCrypt(encryptOrDecrypt,
kCCAlgorithm3DES,
kCCOptionPKCS7Padding,
vkey, //"123456789012345678901234", //key
kCCKeySize3DES,
vinitVec, //"init Vec", //iv,
vplainText, //"Your Name", //plainText,
plainTextBufferSize,
(void *)bufferPtr,
bufferPtrSize,
&movedBytes);
if (ccStatus == kCCSuccess) NSLog(@"SUCCESS");
else if (ccStatus == kCCParamError) NSLog( @"PARAM ERROR");
else if (ccStatus == kCCBufferTooSmall) NSLog( @"BUFFER TOO SMALL");
else if (ccStatus == kCCMemoryFailure) NSLog( @"MEMORY FAILURE");
else if (ccStatus == kCCAlignmentError) NSLog( @"ALIGNMENT");
else if (ccStatus == kCCDecodeError) NSLog( @"DECODE ERROR");
else if (ccStatus == kCCUnimplemented) NSLog( @"UNIMPLEMENTED");
NSData *result;
if (encryptOrDecrypt == kCCDecrypt)
{
result = [NSData dataWithBytes:(const void *)bufferPtr length:(NSUInteger)movedBytes];
}
else
{
NSData *myData = [NSData dataWithBytes:(const void *)bufferPtr length:(NSUInteger)movedBytes];
result = [GTMBase64 encodeData:myData];
}
return result;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSURL *url = [NSURL URLWithString:@"http://www.oscurodrago.it/tools/crypto.php"];
NSData *data = [NSData dataWithContentsOfURL:url];
NSData *outData = [[NSData alloc] initWithData:[self TripleDES:data encryptOrDecrypt:kCCDecrypt key:@"f968f8e82961489a8b14b345"]];
NSLog(@"%@", data );
// NSData *encriptdata = [self TripleDES:data encryptOrDecrypt:kCCEncrypt key:@"f968f8e82961489a8b14b345"];
NSLog(@"%@", [outData length] );
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
@end
答案 0 :(得分:1)
您必须将'outData'设为base64编码的字符串。然后只有你可以打印NSData。在此之前尝试,通过
打印'outData'长度,数据不为空NSLog(@"%d",[outData length]);
请参阅this链接,将NSData转换为base64字符串。