ios 中aes能对汉字加密吗
1个回答
展开全部
// 开发中用到AES128加密、解密
#import <Foundation/Foundation.h>
#import <CommonCrypto/CommonCryptor.h>
@interface NSData (AES)
- (NSData *)AES128Operation:(CCOperation)operation key:(NSString *)key iv:(NSString *)iv;
- (NSData *)AES128EncryptWithKey:(NSString *)key iv:(NSString *)iv;
- (NSData *)AES128DecryptWithKey:(NSString *)key iv:(NSString *)iv;
@end
@implementation NSData (AES)
- (NSData *)AES128Operation:(CCOperation)operation key:(NSString *)key iv:(NSString *)iv
{
char keyPtr[kCCKeySizeAES128 + 1];
memset(keyPtr, 0, sizeof(keyPtr));
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
char ivPtr[kCCBlockSizeAES128 + 1];
memset(ivPtr, 0, sizeof(ivPtr));
[iv getCString:ivPtr maxLength:sizeof(ivPtr) encoding:NSUTF8StringEncoding];
NSUInteger dataLength = [self length];
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
size_t numBytesCrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(operation,
kCCAlgorithmAES128,
kCCOptionPKCS7Padding,
keyPtr,
kCCBlockSizeAES128,
ivPtr,
[self bytes],
dataLength,
buffer,
bufferSize,
&numBytesCrypted);
if (cryptStatus == kCCSuccess) {
return [NSData dataWithBytesNoCopy:buffer length:numBytesCrypted];
}
free(buffer);
return nil;
}
- (NSData *)AES128EncryptWithKey:(NSString *)key iv:(NSString *)iv
{
return [self AES128Operation:kCCEncrypt key:key iv:iv];
}
- (NSData *)AES128DecryptWithKey:(NSString *)key iv:(NSString *)iv
{
return [self AES128Operation:kCCDecrypt key:key iv:iv];
}
@end
int main(int argc, char const* argv[])
{
NSAutoreleasePool* pool;
pool = [[NSAutoreleasePool alloc] init];
NSString *key = [NSString stringWithCString:argv[1] encoding:NSUTF8StringEncoding];
NSString *iv = [NSString stringWithCString:argv[2] encoding:NSUTF8StringEncoding];
NSString *data_str = [NSString stringWithCString:argv[3] encoding:NSUTF8StringEncoding];
NSData *data = [data_str dataUsingEncoding:NSUTF8StringEncoding];
NSData *en_data = [data AES128EncryptWithKey:key iv:iv];
NSData *de_data = [en_data AES128DecryptWithKey:key iv:iv];
NSString *de_str = [[[NSString alloc] initWithData:de_data
encoding:NSUTF8StringEncoding] autorelease];
NSLog(@"%@", en_data);
NSLog(@"%@", de_str);
[pool drain];
return 0;
}
#import <Foundation/Foundation.h>
#import <CommonCrypto/CommonCryptor.h>
@interface NSData (AES)
- (NSData *)AES128Operation:(CCOperation)operation key:(NSString *)key iv:(NSString *)iv;
- (NSData *)AES128EncryptWithKey:(NSString *)key iv:(NSString *)iv;
- (NSData *)AES128DecryptWithKey:(NSString *)key iv:(NSString *)iv;
@end
@implementation NSData (AES)
- (NSData *)AES128Operation:(CCOperation)operation key:(NSString *)key iv:(NSString *)iv
{
char keyPtr[kCCKeySizeAES128 + 1];
memset(keyPtr, 0, sizeof(keyPtr));
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
char ivPtr[kCCBlockSizeAES128 + 1];
memset(ivPtr, 0, sizeof(ivPtr));
[iv getCString:ivPtr maxLength:sizeof(ivPtr) encoding:NSUTF8StringEncoding];
NSUInteger dataLength = [self length];
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
size_t numBytesCrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(operation,
kCCAlgorithmAES128,
kCCOptionPKCS7Padding,
keyPtr,
kCCBlockSizeAES128,
ivPtr,
[self bytes],
dataLength,
buffer,
bufferSize,
&numBytesCrypted);
if (cryptStatus == kCCSuccess) {
return [NSData dataWithBytesNoCopy:buffer length:numBytesCrypted];
}
free(buffer);
return nil;
}
- (NSData *)AES128EncryptWithKey:(NSString *)key iv:(NSString *)iv
{
return [self AES128Operation:kCCEncrypt key:key iv:iv];
}
- (NSData *)AES128DecryptWithKey:(NSString *)key iv:(NSString *)iv
{
return [self AES128Operation:kCCDecrypt key:key iv:iv];
}
@end
int main(int argc, char const* argv[])
{
NSAutoreleasePool* pool;
pool = [[NSAutoreleasePool alloc] init];
NSString *key = [NSString stringWithCString:argv[1] encoding:NSUTF8StringEncoding];
NSString *iv = [NSString stringWithCString:argv[2] encoding:NSUTF8StringEncoding];
NSString *data_str = [NSString stringWithCString:argv[3] encoding:NSUTF8StringEncoding];
NSData *data = [data_str dataUsingEncoding:NSUTF8StringEncoding];
NSData *en_data = [data AES128EncryptWithKey:key iv:iv];
NSData *de_data = [en_data AES128DecryptWithKey:key iv:iv];
NSString *de_str = [[[NSString alloc] initWithData:de_data
encoding:NSUTF8StringEncoding] autorelease];
NSLog(@"%@", en_data);
NSLog(@"%@", de_str);
[pool drain];
return 0;
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询