iOS
NSString *str = @"Enter data to be encrypted here!"; NSString *strKey = @"MySecretEncryptionKeyWith32Chars"; NSString *strCrypt = [str AES256EncryptWithKey:strKey];
CBCrypt.h
#import <Foundation/Foundation.h> @interface NSData (AESCrypt) - (NSData *)AES256EncryptWithKey:(NSString *)key; - (NSString *)base64Encoding; @end // ------------------------------------------------------------------------------ @interface NSString (AESCrypt) - (NSString *)AES256EncryptWithKey:(NSString *)key reverse:(BOOL)reverse; @end
CBCrypt.m
#import "CBCrypt.h" #import <CommonCrypto/CommonCryptor.h> static char encodingTable[64] = { 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P', 'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f', 'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v', 'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/' }; @implementation NSData (AESCrypt) - (NSData *)AES256EncryptWithKey:(NSString *)key{ // 'key' should be 16 bytes for AES128 char keyPtr[kCCKeySizeAES256 + 1]; // room for terminator (unused) bzero( keyPtr, sizeof( keyPtr ) ); // fill with zeroes (for padding) // fetch key data [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding]; NSUInteger dataLength = [self length]; size_t bufferSize = dataLength + kCCBlockSizeAES128; void *buffer = malloc( bufferSize ); size_t numBytesEncrypted = 0; CCCryptorStatus cryptStatus = CCCrypt( kCCEncrypt, kCCAlgorithmAES128, kCCOptionECBMode | kCCOptionPKCS7Padding, keyPtr, kCCKeySizeAES256, NULL /* initialization vector (optional) */, [self bytes], dataLength, /* input */ buffer, bufferSize, /* output */ &numBytesEncrypted ); if( cryptStatus == kCCSuccess ){ // the returned NSData takes ownership of the buffer // and will free it on deallocation return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted]; } free( buffer ); //free the buffer return nil; } - (NSString *)base64Encoding{ const unsigned char *bytes = [self bytes]; NSMutableString *result = [NSMutableString stringWithCapacity:self.length]; unsigned long ixtext = 0; unsigned long lentext = self.length; long ctremaining = 0; unsigned char inbuf[3], outbuf[4]; unsigned short i = 0; unsigned short charsonline = 0, ctcopy = 0; unsigned long ix = 0; while( YES ) { ctremaining = lentext - ixtext; if( ctremaining <= 0 ) break; for( i = 0; i < 3; i++ ){ ix = ixtext + i; if( ix < lentext ) { inbuf[i] = bytes[ix]; } else { inbuf [i] = 0; } } outbuf [0] = (inbuf [0] & 0xFC) >> 2; outbuf [1] = ((inbuf [0] & 0x03) << 4) | ((inbuf [1] & 0xF0) >> 4); outbuf [2] = ((inbuf [1] & 0x0F) << 2) | ((inbuf [2] & 0xC0) >> 6); outbuf [3] = inbuf [2] & 0x3F; ctcopy = 4; switch( ctremaining ){ case 1: ctcopy = 2; break; case 2: ctcopy = 3; break; } for( i = 0; i < ctcopy; i++ ){ [result appendFormat:@"%c", encodingTable[outbuf[i]]]; } for( i = ctcopy; i < 4; i++ ){ [result appendString:@"="]; } ixtext += 3; charsonline += 4; } return [NSString stringWithString:result]; @end // ------------------------------------------------------------------------------ @implementation NSString (AESCrypt) - (NSString *)AES256EncryptWithKey:(NSString *)key{ NSData *plainData = [self dataUsingEncoding:NSUTF8StringEncoding]; NSData *encryptedData = [plainData AES256EncryptWithKey:key]; NSString *encryptedString = [encryptedData base64Encoding]; CFStringRef urlString = CFURLCreateStringByAddingPercentEscapes( NULL, (CFStringRef)encryptedString, NULL, (CFStringRef)@"!*'\"();:@=+$,/?%#[]% ", // add & here, the editor won't let me ;) kCFStringEncodingUTF8 ); return (__bridge NSString*)urlString; } @end
php
function decryptAES($_str,$_key){ $padded_key = $_key . str_repeat(chr(0x00), 16); $result = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $padded_key, base64_decode($_str), 'ecb'); $pad_char = ord(substr($result, -1)); $result_without_padding = substr($result, 0, strlen($result) - $pad_char); return $result_without_padding; }
Pingback: Verschlüsseln und Entschlüsseln in PHP