20161121

CommonCrypto加密解密参数说明

CCCryptorStatus CCCrypt(
    CCOperation op,         /* kCCEncrypt, etc. */
    CCAlgorithm alg,        /* kCCAlgorithmAES128, etc. */
    CCOptions options,      /* kCCOptionPKCS7Padding, etc. */
    const void *key,
    size_t keyLength,
    const void *iv,         /* optional initialization vector */
    const void *dataIn,     /* optional per op and alg */
    size_t dataInLength,
    void *dataOut,          /* data RETURNED here */
    size_t dataOutAvailable,
    size_t *dataOutMoved)
__OSX_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);

参数说明

  • CCOperation op加密kCCEncrypt还是解密kCCDecrypt

  • CCAlgorithm alg加密算法,枚举如下[1]

enum {
    kCCAlgorithmAES128 = 0,
    kCCAlgorithmAES = 0,
    kCCAlgorithmDES,
    kCCAlgorithm3DES,       
    kCCAlgorithmCAST,       
    kCCAlgorithmRC4,
    kCCAlgorithmRC2,   
    kCCAlgorithmBlowfish    
};
typedef uint32_t CCAlgorithm;
  • CCOptions options可以使用|符号,枚举如下
enum {
    /* options for block ciphers */
    kCCOptionPKCS7Padding   = 0x0001,
    kCCOptionECBMode        = 0x0002
    /* stream ciphers currently have no options */
};
typedef uint32_t CCOptions;
  • const void *key加密或解密的钥匙[2]
  • size_t keyLength钥匙的内存长度,请根据加密算法从以下枚举中选择
enum {
    kCCKeySizeAES128          = 16,
    kCCKeySizeAES192          = 24,
    kCCKeySizeAES256          = 32,
    kCCKeySizeDES             = 8,
    kCCKeySize3DES            = 24,
    kCCKeySizeMinCAST         = 5,
    kCCKeySizeMaxCAST         = 16,
    kCCKeySizeMinRC4          = 1,
    kCCKeySizeMaxRC4          = 512,
    kCCKeySizeMinRC2          = 1,
    kCCKeySizeMaxRC2          = 128,
    kCCKeySizeMinBlowfish     = 8,
    kCCKeySizeMaxBlowfish     = 56,
};
  • const void *iv初始化矢量,传NULL即可
  • const void *dataIn需要加密的数据[3]
  • size_t dataInLength加密数据的内存长度[4]
  • void *dataOut数据输出的地址
  • size_t dataOutAvailable为数据输出预留的内存大小
  • size_t *dataOutMoved输出的位移

  1. 注意加密和解密时算法要一致 ↩

  2. 请注意类型转化成C类型,笔者使用 UTF8String

  3. 请注意类型转化成C类型,笔者使用 bytes

  4. 内存长度的计算与数据类型有关,请自行百度,这里不做说明 ↩

你可能感兴趣的:(20161121)