美文网首页
加密解密

加密解密

作者: wpf_register | 来源:发表于2017-11-22 17:27 被阅读24次

    参考文档1

    MD5

    1.简单说明:
    MD5:全称是Message Digest Algorithm 5,译为“消息摘要算法第5版”
    效果:对输入信息生成唯一的128位散列值(32个字符)
    MD5生成的是固定的 128bit ,即128个0和1的二进制位,而在实际应用开发中,通常是以16进制输出的,所以正好就是32位的16进制,说白了也就是32个16进制的数字。

    2. MD5的特点:
    (1)输入两个不同的明文不会得到相同的输出值
    (2)根据输出值,不能得到原始的明文,即其过程不可逆(只能加密, 不能解密)
    (3)加密区分大小写

    3.MD5的应用:
    由于MD5加密算法具有较好的安全性,而且免费,因此该加密算法被广泛使用
    大多数的登录功能向后台提交密码时都会使用到这种算法


    AES加密

    AES测试网站

    简单说明

    1. 一般用到AES加密+Base64编码相结合
    2. AES 加密模式: ECB CBC CFB OFB ,前后台选择相同模式
    3. 加密过程要提供一个 privateKey
    4. 加密过程要提供一个偏移量(IV),非必须的。
    5. 补码方式:PKCS7Padding PKCS5Padding。

    CCCryptorStatus 构造

    苹果文档
    /*
        @function   CCCrypt
        @abstract   Stateless, one-shot encrypt or decrypt operation.
                    This basically performs a sequence of CCCrytorCreate(),
                    CCCryptorUpdate(), CCCryptorFinal(), and CCCryptorRelease().
        
        @param      alg             Defines the encryption algorithm.
        
        
        @param      op              Defines the basic operation: kCCEncrypt or kCCDecrypt.
        
        @param      options         A word of flags defining options. See discussion
                                    for the CCOptions type.
        
        @param      key             Raw key material, length keyLength bytes. 
                                    加解密的密钥
        
        @param      keyLength       Length of key material. Must be appropriate 
                                    for the select algorithm. Some algorithms may 
                                    provide for varying key lengths.
                                    参数解释详见苹果文档中的一个Key Size的枚举
                                    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,
                                                };
        
        @param      iv              Initialization vector, optional. Used for 
                                    Cipher Block Chaining (CBC) mode. If present, 
                                    must be the same length as the selected 
                                    algorithm's block size. If CBC mode is
                                    selected (by the absence of any mode bits in 
                                    the options flags) and no IV is present, a 
                                    NULL (all zeroes) IV will be used. This is 
                                    ignored if ECB mode is used or if a stream 
                                    cipher algorithm is selected. 
                                    向量:大概意思说,此属性可选,但只能用于CBC模式。
                                    如果出现那么他的长度必须和算法的block size保持一致。
                                    如果是因为默认选择的CBC模式而且向量没有定义,那么向量会被定义为NULL。
                                    如果选择了ECB模式或是其他的流密码算法,之前所说的逻辑都不成立。
        
        @param      dataIn          Data to encrypt or decrypt, length dataInLength 
                                    bytes. 
        
        @param      dataInLength    Length of data to encrypt or decrypt.
        
        @param      dataOut         Result is written here. Allocated by caller. 
                                    Encryption and decryption can be performed
                                    "in-place", with the same buffer used for 
                                    input and output. 
        
        @param      dataOutAvailable The size of the dataOut buffer in bytes.  
        
        @param      dataOutMoved    On successful return, the number of bytes
                        written to dataOut. If kCCBufferTooSmall is
                    returned as a result of insufficient buffer
                    space being provided, the required buffer space
                    is returned here. 
            
        @result     kCCBufferTooSmall indicates insufficent space in the dataOut
                                    buffer. In this case, the *dataOutMoved 
                                    parameter will indicate the size of the buffer
                                    needed to complete the operation. The 
                                    operation can be retried with minimal runtime 
                                    penalty. 
                    kCCAlignmentError indicates that dataInLength was not properly 
                                    aligned. This can only be returned for block 
                                    ciphers, and then only when decrypting or when 
                                    encrypting with block with padding disabled. 
                    kCCDecodeError  Indicates improperly formatted ciphertext or
                                    a "wrong key" error; occurs only during decrypt
                                    operations. 
     */
        
    CCCryptorStatus CCCrypt(
        CCOperation op,         /* kCCEncrypt, etc. */编码 or 解码
        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)
    
    

    相关文章

      网友评论

          本文标题:加密解密

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