iOS于JAVA后台的AES兼容

@(iOS学习)[ios | 笔记]

iOS于JAVA后台的AES兼容

iOS的AES Padding只支持ccPKCS7Padding, ccNoPadding, 而如果JAVA后端的AES加密解密用到PKCS5Padding的话, 那么iOS端就需要自己去实现了.

代码如下:

- (NSString*) EncodeText:(NSString*)plainText enc:(CCOperation)encryptOrDecrypt{
    
    const void *vplainText;
    size_t plainTextBufferSize;
    
    if (encryptOrDecrypt == kCCDecrypt)
    {
        NSString *base64EncodedString = [[plainText dataUsingEncoding:NSUTF8StringEncoding] base64EncodedStringWithOptions:0];
        NSData *encryptData = [[NSData alloc]initWithBase64EncodedString:base64EncodedString options:0];
        plainTextBufferSize = [encryptData length];
        vplainText = [encryptData bytes];
    }
    else
    {
        plainTextBufferSize = [plainText length];
        vplainText = (const void *) [plainText UTF8String];
    }
    
    CCCryptorStatus ccStatus;
    uint8_t *bufferPtr = NULL;
    size_t bufferPtrSize = 0;
    size_t movedBytes = 0;
    
    bufferPtrSize = (plainTextBufferSize + kCCBlockSize3DES) & ~(kCCBlockSize3DES - 1);
    bufferPtr = malloc( bufferPtrSize * sizeof(uint8_t));
    memset((void *)bufferPtr, 0x0, bufferPtrSize);
    
    // 设置你的AES key
    const unsigned char *keyString = (const unsigned char *)[@"ASAF#Key$M0BiLE!WS@DG_1@"  cStringUsingEncoding: NSUTF8StringEncoding];
    // 这里你的AES IV
    const unsigned char *initializeVactorString = (const unsigned char *)[@"ASAF@DG#" cStringUsingEncoding: NSUTF8StringEncoding];
    
    uint8_t iv[kCCBlockSize3DES];
    memset((void *) iv, 0x0, (size_t) sizeof(iv));
    
    ccStatus = CCCrypt(encryptOrDecrypt,
                       kCCAlgorithm3DES,
                       kCCOptionPKCS7Padding,
                       keyString,
                       kCCKeySize3DES,
                       initializeVactorString,
                       vplainText,
                       plainTextBufferSize,
                       (void *)bufferPtr,
                       bufferPtrSize,
                       &movedBytes);
    
    if (ccStatus == kCCSuccess) NSLog(@"SUCCESS");
    else{
        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;
    if (encryptOrDecrypt == kCCDecrypt)
    {
        result = [ [NSString alloc] initWithData: [NSData dataWithBytes:(const void *)bufferPtr length:(NSUInteger)movedBytes] encoding:NSASCIIStringEncoding];
    }
    else
    {
        NSData *myData = [NSData dataWithBytes:(const void *)bufferPtr length:(NSUInteger)movedBytes];
        result = [myData base64EncodedStringWithOptions:0];
    }
    NSLog(@"%@",result);

    return result;
}

参看:

  • Objective C DESede/CBC/PKCS5Padding

  • What is the difference between PKCS#5 padding and PKCS#7 padding

你可能感兴趣的:(iOS于JAVA后台的AES兼容)