简体   繁体   中英

Encryption Decryption AES/ECB/NoPadding

is there any method de encrypt/decrypt a string in iOS following this algorithm ? all i know that in java this class : javax.crypto.Cipher is used for this purpose

Apple Common Crypto does support AES, ECB mode (use CBC mode) and no padding. If you do not use padding the input data length must be an exact multiple of the AES block size (16-bytes), the standard padding is PKCS#7 (née PKCS#5).

You would be better off using a solution that supports secure encryption such as RNCryptor which supports multiple languages and platforms.

Just using encryption does not provide security, it must be used correctly in conjunction with other cryptographic primitives.

Apple's Common Crpto does support it. Import CommonCryptor.h and CCCryptorStatus "CCCrypt" is what you need.

Here's an example:

+ (NSData *)encryptDataWithAESECB:(NSData *)data
                              key:(NSData *) key
                            error:(NSError **)error {
    size_t outLength;

    int cipherLen = (int)(data.length/kAlgorithmBlockSize + 1)*kAlgorithmBlockSize;
    NSMutableData *cipherData = [NSMutableData dataWithLength:cipherLen];
    NSData *newData = [self addPaddingBeforeEncryptWithAESECB:data];

    CCCryptorStatus result = CCCrypt(kCCEncrypt, // operation
                                     kAlgorithm, // Algorithm
                                     kCCOptionECBMode, // Mode
                                     key.bytes, // key
                                     key.length, // keylength
                                     0,// iv
                                     newData.bytes, // dataIn
                                     newData.length, // dataInLength,
                                     cipherData.mutableBytes, // dataOut
                                     cipherData.length, // dataOutAvailable
                                     &outLength); // dataOutMoved
    if (result == kCCSuccess) {
        cipherData.length = outLength;
    }else {
        if (error) {
            *error = [NSError errorWithDomain:kRNCryptManagerErrorDomain code:result userInfo:nil];
        }
        return nil;
    }
    return cipherData;
}

Please refer to my post here for more information: AES ECB iOS Encrypt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM