php+;ios加密和解密3des+;base64
我需要用php加密和解密由这里解释的ios方法生成的字符串代码 对于使用php加密,我使用该脚本php+;ios加密和解密3des+;base64,php,ios,base64,encryption,3des,Php,Ios,Base64,Encryption,3des,我需要用php加密和解密由这里解释的ios方法生成的字符串代码 对于使用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);
$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));
#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
在decript的IOs上,我使用这个
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返回解码错误->如果(ccStatus==kCCDecodeError)NSLog(@“解码错误”)
更新
这就是我测试脚本的viewcontroller.m
$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));
#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
#导入
#导入“GTMBase64.h”
#导入“ViewController.h”
@界面视图控制器()
@结束
@实现视图控制器
-(NSData*)三元组:(NSData*)明文数据加密加密加密:(cOperation)加密加密加密密钥:(NSString*)密钥{
const void*vplainText;
大小\u t明文缓冲大小;
if(encryptOrDecrypt==kCCDecrypt)
{
NSData*EncryptData=[GTMBase64解码数据:明文数据];
plainTextBufferSize=[EncryptData长度];
vplainText=[EncryptData字节];
}
其他的
{
plainTextBufferSize=[plainData长度];
vplainText=(const void*)[plainData字节];
}
CCCryptorStatus-ccStatus状态;
uint8_t*bufferPtr=NULL;
大小\u t bufferPtrSize=0;
大小\u 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*键=@“1234567890012345678901234”;
NSString*initVec=@“init Vec”;
常量void*vkey=(常量void*)[key UTF8String];
常量void*vinitVec=(常量void*)[initVec UTF8String];
ccStatus=CCCrypt(encryptOrDecrypt,
kCCAlgorithm3DES,
KCCOPIONPKCS7填充,
vkey,//“1234567890012345678901234”,//key
kCCKeySize3DES,
vinitVec,//“init Vec”,//iv,
vplainText,//“您的姓名”//纯文本,
明文缓冲区大小,
(void*)bufferPtr,
bufferPtrSize,
&移动字节);
如果(ccStatus==kCCSuccess)NSLog(@“成功”);
如果(ccStatus==kCCParamError)NSLog(@“参数错误”);
否则,如果(ccStatus==kCCBufferTooSmall)NSLog(@“缓冲区太小”);
如果(ccStatus==kCCMemoryFailure)NSLog(@“内存故障”);
如果(ccStatus==kCCAlignmentError)NSLog(@“校准”);
如果(ccStatus==kCCDecodeError)NSLog(@“解码错误”);
否则,如果(ccStatus==kCCUnimplemented)NSLog(@“未实现”);
NSData*结果;
if(encryptOrDecrypt==kCCDecrypt)
{
结果=[NSData dataWithBytes:(const void*)bufferPtr length:(nsInteger)movedBytes];
}
其他的
{
NSData*myData=[NSData dataWithBytes:(const void*)bufferPtr length:(NSUInteger)movedBytes];
结果=[GTMBase64 encodeData:myData];
}
返回结果;
}
-(无效)viewDidLoad
{
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置。
NSURL*url=[NSURL URLWithString:@”http://www.oscurodrago.it/tools/crypto.php"];
NSData*data=[NSData dataWithContentsOfURL:url];
NSData*outData=[[NSData alloc]initWithData:[自三元组:数据加密加密加密:kCCDecrypt密钥:@“f968f8e82961489a8b14b345”];
NSLog(@“%@”,数据);
//NSData*encriptdata=[自三元组:数据加密加密加密:kCCEncrypt密钥:@“f968f8e82961489a8b14b345”];
NSLog(@“%@,[outData长度]);
}
-(无效)视图卸载
{
[超级视频下载];
//释放主视图的所有保留子视图。
}
-(布尔)应自动旋转指针面定向:(UIInterfaceOrientation)interfaceOrientation
{
if([[UIDevice currentDevice]userInterfaceIdiom]==UIUserInterfaceIdiomPhone){
返回(interfaceOrientation!=UIInterfaceOrientation肖像向上向下);
}否则{
返回YES;
}
}
@结束
您必须将“outData”设置为base64编码字符串。然后,只有您可以打印NSData。在此之前请尝试,通过按打印“outData”长度,数据不是空的
NSLog(@"%d",[outData length]);
请参阅将NSData转换为base64字符串的链接。我不太擅长ios,但
[outData length]
返回0,我想已经解密/加密base64打印NSLog(@“%d”,[data length])时得到了什么;如果该值也为0,则无法从服务器获取任何数据。这是NSLog的戳记(@“%@”,data);似乎不可怜2012-03-22 18:06:38.973密码数据[1012:f803]