在 iOS 上使用 MD5 密钥解密 3DES ECB

发布于 2024-10-12 16:48:11 字数 2461 浏览 6 评论 0原文

我正在尝试在 iOS 应用程序中解密来自 .NET 服务器的数据。数据已使用 ECB 模式的 3DES 进行加密。我能够在 Android 上成功解密相同的数据,但在 IOS 上我只是不断收到垃圾输出。

我比较了 iOS 和 Android 之间的加密字节和摘要密钥字节,它们看起来是相同的(尽管我必须将签名的 Java 字节转换为十六进制表示形式才能与 xcode 调试器)。但是,Java 加密对象的级别比 Common Crypto 更高,因此我不确定我是否已正确准备密钥。请看一下下面的代码 - 欢迎任何反馈。

//read the encrypted data from file into NSData
NSString *servicesPath = [NSString stringWithString:[[AppMobiDelegate applicationDocumentsDirectory] stringByAppendingPathComponent:@"services.xml"]];
NSData *servicesData = [NSData dataWithContentsOfFile:servicesPath];

//setup crypto objects
const void *vEncryptedText = [servicesData bytes];
size_t encryptedTextBufferSize = [servicesData length];
CCCryptorStatus ccStatus;
uint8_t *bufferPtr = NULL;
size_t bufferPtrSize = 0;
size_t movedBytes = 0;
bufferPtrSize = (encryptedTextBufferSize + kCCBlockSize3DES) & ~(kCCBlockSize3DES - 1);
bufferPtr = malloc( bufferPtrSize * sizeof(uint8_t));
memset((void *)bufferPtr, 0x0, bufferPtrSize);

//get a cstring with key data
const char *cStr = [@"FAKEKEY" cStringUsingEncoding:UTF8String];

//make a 16 byte array to put the md5 digested data into    
unsigned char md5[CC_MD5_DIGEST_LENGTH];
bzero( md5, sizeof(md5) );
CC_MD5( cStr, strlen(cStr), md5 );
//make a 24 byte array so we have correct size for 3des key and copy digest data in
unsigned char key[kCCKeySize3DES];
bzero( key, sizeof(key) );
memcpy(key, md5, sizeof(md5));  

//decrypt data and return string 
ccStatus = CCCrypt(kCCDecrypt,
    kCCAlgorithm3DES,
    kCCOptionECBMode, //kCCOptionPKCS7Padding kCCOptionECBMode
    key, //vKey md5
    kCCKeySize3DES,
    NULL,
    vEncryptedText,
    encryptedTextBufferSize,
    (void *)bufferPtr,
    bufferPtrSize,
    &movedBytes);

if (ccStatus == kCCParamError) return @"PARAM ERROR";
else if (ccStatus == kCCBufferTooSmall) return @"BUFFER TOO SMALL";
else if (ccStatus == kCCMemoryFailure) return @"MEMORY FAILURE";
else if (ccStatus == kCCAlignmentError) return @"ALIGNMENT";
else if (ccStatus == kCCDecodeError) return @"DECODE ERROR";
else if (ccStatus == kCCUnimplemented) return @"UNIMPLEMENTED";

NSString *result = [[[NSString alloc] initWithData: [NSData dataWithBytes:(const void *)bufferPtr length:(NSUInteger)movedBytes] encoding:NSASCIIStringEncoding] autorelease];
return result;

I am trying to decrypt data from a .NET server in an iOS application. The data has been encrypted with 3DES in ECB mode. I was able to successfully decrypt this same data on Android, but on IOS I just keep getting garbage output.

I've compared the encrypted bytes and digested key bytes between iOS and Android and they appear to be the same (although I had to convert the signed Java bytes into a hex representation to be able to compare to xcode debugger). However, the Java crypto objects` are higher level than Common Crypto, so I'm not confident that I've prepared the key correctly. Please take a look at the following code - any feedback is welcome.

//read the encrypted data from file into NSData
NSString *servicesPath = [NSString stringWithString:[[AppMobiDelegate applicationDocumentsDirectory] stringByAppendingPathComponent:@"services.xml"]];
NSData *servicesData = [NSData dataWithContentsOfFile:servicesPath];

//setup crypto objects
const void *vEncryptedText = [servicesData bytes];
size_t encryptedTextBufferSize = [servicesData length];
CCCryptorStatus ccStatus;
uint8_t *bufferPtr = NULL;
size_t bufferPtrSize = 0;
size_t movedBytes = 0;
bufferPtrSize = (encryptedTextBufferSize + kCCBlockSize3DES) & ~(kCCBlockSize3DES - 1);
bufferPtr = malloc( bufferPtrSize * sizeof(uint8_t));
memset((void *)bufferPtr, 0x0, bufferPtrSize);

//get a cstring with key data
const char *cStr = [@"FAKEKEY" cStringUsingEncoding:UTF8String];

//make a 16 byte array to put the md5 digested data into    
unsigned char md5[CC_MD5_DIGEST_LENGTH];
bzero( md5, sizeof(md5) );
CC_MD5( cStr, strlen(cStr), md5 );
//make a 24 byte array so we have correct size for 3des key and copy digest data in
unsigned char key[kCCKeySize3DES];
bzero( key, sizeof(key) );
memcpy(key, md5, sizeof(md5));  

//decrypt data and return string 
ccStatus = CCCrypt(kCCDecrypt,
    kCCAlgorithm3DES,
    kCCOptionECBMode, //kCCOptionPKCS7Padding kCCOptionECBMode
    key, //vKey md5
    kCCKeySize3DES,
    NULL,
    vEncryptedText,
    encryptedTextBufferSize,
    (void *)bufferPtr,
    bufferPtrSize,
    &movedBytes);

if (ccStatus == kCCParamError) return @"PARAM ERROR";
else if (ccStatus == kCCBufferTooSmall) return @"BUFFER TOO SMALL";
else if (ccStatus == kCCMemoryFailure) return @"MEMORY FAILURE";
else if (ccStatus == kCCAlignmentError) return @"ALIGNMENT";
else if (ccStatus == kCCDecodeError) return @"DECODE ERROR";
else if (ccStatus == kCCUnimplemented) return @"UNIMPLEMENTED";

NSString *result = [[[NSString alloc] initWithData: [NSData dataWithBytes:(const void *)bufferPtr length:(NSUInteger)movedBytes] encoding:NSASCIIStringEncoding] autorelease];
return result;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文