ios开发中的aes-128-ecb加密总是不正确。

云松 发布于 2015/09/15 10:57
阅读 2K+
收藏 0

用python , c# , 还有在 aes在线加密计算都是一致的, 但是用objc 生成的 却总是不一样。 

求各位大侠帮忙看看, 我应该怎么配置参数。    已经换了很多库了(cocoasecurity, aescrypt等等)等,试了各种参数,花了不少时间, 实在没有办法了。 

这个是用python计算的


python 代码差不多是这样

# coding=utf-8
__author__ = 'matrix'

from Crypto.Cipher import AES
import base64


BS = AES.block_size
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
unpad = lambda s: s[0:-ord(s[-1])]


class Singleton(object):
    __instance = None

    def __init__(self):
        pass

    def __new__(cls, *args, **kwd):
        if Singleton.__instance is None:
            Singleton.__instance = object.__new__(cls, *args, **kwd)
        return Singleton.__instance


class Aes(Singleton):
    key = None
    encryptor = None

    def __init__(self, key=''):
        self.key = key
        self.mode = AES.MODE_ECB
        print "self mode", self.mode
        self.encryptor = AES.new(self.key)
        print self.encryptor.__dict__


    def encrypt(self, text):
        temp_text = self.encryptor.encrypt(pad(text))
        print "temp text : %s" % repr(temp_text)
        return base64.b64encode(temp_text)

    def decrypt(self, ciphertext):
        un64_text = base64.b64decode(ciphertext)
        return unpad(self.encryptor.decrypt(un64_text))

这个是在网上的在线工具计算的



结果都是  

OhqbKSQSzEqfCO4DJjaU1A==


然而在objc 上运行 不管怎么调都不能匹配上  OhqbKSQSzEqfCO4DJjaU1A== 

还请高手帮忙看看  objc 代码该怎么改 ,从也是从网上找来的。 

头文件 

#import <Foundation/Foundation.h>

@interface ZizAes : NSObject

+ (NSString*) AES128Encrypt:(NSString *)plainText;

+ (NSString*) AES128Decrypt:(NSString *)encryptText;

@end
m文件
#import "aes.h"
#import <CommonCrypto/CommonCryptor.h>
#import "GTMBase64.h"

#define gkey			@"d13f24279a6543db" //自行修改
#define gIv             @"12345678" //自行修改


@implementation ZizAes

+(NSString *)AES128Encrypt:(NSString *)plainText
{
    char keyPtr[kCCKeySizeAES128+1];
    memset(keyPtr, 0, sizeof(keyPtr));
    [gkey getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
    
    char ivPtr[kCCBlockSizeAES128+1];
    memset(ivPtr, 0, sizeof(ivPtr));
    [gIv getCString:ivPtr maxLength:sizeof(ivPtr) encoding:NSUTF8StringEncoding];
    
    NSData* data = [plainText dataUsingEncoding:NSUTF8StringEncoding];
    NSUInteger dataLength = [data length];
    
    int diff = kCCKeySizeAES128 - (dataLength % kCCKeySizeAES128);
    int newSize = 0;
    
    if(diff > 0)
    {
        newSize = (int)dataLength + diff;
    }
    
    char dataPtr[newSize];
    memcpy(dataPtr, [data bytes], [data length]);
    for(int i = 0; i < diff; i++)
    {
        dataPtr[i + dataLength] = 0x00;
    }
    
    size_t bufferSize = newSize + kCCBlockSizeAES128;
    void *buffer = malloc(bufferSize);
    memset(buffer, 0, bufferSize);
    
    size_t numBytesCrypted = 0;
    NSLog(@"keyPtr: %s === dataPtr: %s, size == : %lu", keyPtr, dataPtr, sizeof(dataPtr));
    
    
    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,
                                          kCCAlgorithmAES128,
                                          kCCOptionECBMode,               //
                                          keyPtr,
                                          kCCKeySizeAES128,
                                          NULL,
                                          dataPtr,
                                          sizeof(dataPtr),
                                          buffer,
                                          bufferSize,
                                          &numBytesCrypted);
    
    if (cryptStatus == kCCSuccess) {
        NSData *resultData = [NSData dataWithBytesNoCopy:buffer length:numBytesCrypted];
        return [GTMBase64 stringByEncodingData:resultData];
    }
    free(buffer);
    return nil;
}

+(NSString *)AES128Decrypt:(NSString *)encryptText
{
    char keyPtr[kCCKeySizeAES128 + 1];
    memset(keyPtr, 0, sizeof(keyPtr));
    [gkey getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
    
    char ivPtr[kCCBlockSizeAES128 + 1];
    memset(ivPtr, 0, sizeof(ivPtr));
    [gIv getCString:ivPtr maxLength:sizeof(ivPtr) encoding:NSUTF8StringEncoding];
    
    NSData *data = [GTMBase64 decodeData:[encryptText dataUsingEncoding:NSUTF8StringEncoding]];
    NSUInteger dataLength = [data length];
    size_t bufferSize = dataLength + kCCBlockSizeAES128;
    void *buffer = malloc(bufferSize);
    
    NSString *initIv = @"1234567890123456";
    const void *iv = (const void *) [initIv UTF8String];
    
    size_t numBytesCrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt,
                                          kCCAlgorithmAES128,
                                          kCCOptionECBMode,
                                          keyPtr,
                                          kCCBlockSizeAES128,
                                          iv,
                                          [data bytes],
                                          dataLength,
                                          buffer,
                                          bufferSize,
                                          &numBytesCrypted);
    if (cryptStatus == kCCSuccess) {
        NSData *resultData = [NSData dataWithBytesNoCopy:buffer length:numBytesCrypted];
        return [[NSString alloc] initWithData:resultData encoding:NSUTF8StringEncoding];
        // return [[[NSString alloc] initWithData:resultData encoding:NSUTF8StringEncoding]];
    }
    free(buffer);
    return nil;
}


@end
谢谢了!!!
加载中
返回顶部
顶部