美文网首页
AES加密ECB模式PKCS5Padding模式的实现

AES加密ECB模式PKCS5Padding模式的实现

作者: LeeYouth | 来源:发表于2016-12-13 17:34 被阅读0次

    AES加密ECB模式PKCS5Padding模式的实现

    具体代码如下:

    //字符串加密(16进制)

    + (NSString*)encyptPKCS5:(NSString*)plainText WithKey:(NSString*)key{

    //把string转NSData

    NSData* data = [plainText dataUsingEncoding:NSUTF8StringEncoding];

    //length

    size_tplainTextBufferSize = [data length];

    constvoid*vplainText = (constvoid*)[databytes];

    uint8_t*bufferPtr =NULL;

    size_tbufferPtrSize =0;

    size_tmovedBytes =0;

    bufferPtrSize = (plainTextBufferSize +kCCBlockSizeAES128) & ~(kCCBlockSizeAES128-1);

    bufferPtr =malloc( bufferPtrSize *sizeof(uint8_t));

    memset((void*)bufferPtr,0x0, bufferPtrSize);

    constvoid*vkey = (constvoid*) [keyUTF8String];

    //配置CCCrypt

    CCCryptorStatusccStatus =CCCrypt(kCCEncrypt,

    kCCAlgorithmAES128,//3DES

    kCCOptionECBMode|kCCOptionPKCS7Padding,//设置模式

    vkey,//key

    kCCKeySizeAES128,

    nil,//偏移量,这里不用,设置为nil;不用的话,必须为nil,不可以为@“”

    vplainText,

    plainTextBufferSize,

    (void*)bufferPtr,

    bufferPtrSize,

    &movedBytes);

    if(ccStatus ==kCCSuccess) {

    NSData*myData = [NSDatadataWithBytes:(constchar*)bufferPtr length:(NSUInteger)movedBytes];

    NSUInteger len = [myData length];

    char*chars = (char*)[myDatabytes];

    NSMutableString*hexString = [[NSMutableString alloc]init];

    for(NSUInteger i =0; i < len; i++ )

    [hexString appendString:[NSString stringWithFormat:@"%0.2hhx", chars[i]]];

    //16进制

    return hexString;

    }

    free(bufferPtr);

    return nil;

    }

    相关文章

      网友评论

          本文标题:AES加密ECB模式PKCS5Padding模式的实现

          本文链接:https://www.haomeiwen.com/subject/vndbmttx.html