如何使用字符串和密钥生成加密字符串?就像是...
NSString *key = @"password";
NSString *stringToBeEncrypted = @"This is important";
NSString *result = [Encryptor encryptedStringForKey:key string:stringToBeEncrypted];
如何使用字符串和密钥生成加密字符串?就像是...
NSString *key = @"password";
NSString *stringToBeEncrypted = @"This is important";
NSString *result = [Encryptor encryptedStringForKey:key string:stringToBeEncrypted];
经常使用的一种是 AES256Encryption,它的实现方式如下:
NSString *stringToBeEncrypted = @"This is important";
NSString *key = @"password";
NSLog( @"Original String: %@", stringToBeEncrypted );
NSString *encryptedString = [stringToBeEncrypted AES256EncryptWithKey:key];
NSLog( @"Encrypted String: %@", encryptedString );
NSLog( @"Decrypted String: %@", [encryptedString AES256DecryptWithKey:key] );
编码有很多种,
这里有一个样本,
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString *key = @"my password";
NSString *secret = @"text to encrypt";
NSData *plain = [secret dataUsingEncoding:NSUTF8StringEncoding];
NSData *cipher = [plain AES256EncryptWithKey:key];
plain = [cipher AES256DecryptWithKey:key];
[pool drain];
- (NSData*) encryptString:(NSString*)plaintext withKey:(NSString*)key {
return [[plaintext dataUsingEncoding:NSUTF8StringEncoding] AES256EncryptWithKey:key];
}
- (NSString*) decryptData:(NSData*)ciphertext withKey:(NSString*)key {
return [[[NSString alloc] initWithData:[ciphertext AES256DecryptWithKey:key]
encoding:NSUTF8StringEncoding] autorelease];
}