您好,我正在尝试实现一个 iPhone 应用程序,该应用程序记录笔记并对其进行加密,然后在您在 UIAlertView 上输入正确的 PIN 时对其进行解密。当应用程序第一次加载时,我在我的 AppDelegate 中包含了一些代码,用于检查是否已经存在公钥和私钥。如果不是,它会生成一对并将它们添加到内部存储中。当应用程序第一次启动时,它工作得很好。然后我终止应用程序并再次打开它,当我进入现有笔记并尝试解密它时它只是挂起。从控制台我可以看到它在解密方法中加载了密码缓冲区,但这次 textView 从未加载过解密的文本。Button 只是保持在按下状态,并且该过程不会终止。这是代码。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *data2 = [defaults objectForKey:@"publicKey"];
if (data2 == nil) {
encrypt = [[SecKey alloc]init];
[encrypt generateKeyPairRSA];
SecKeyRef publicKey =[encrypt getPublicKeyRef];
SecKeyRef privateKey = [encrypt getPrivateKeyRef];
size_t keySize = SecKeyGetBlockSize(publicKey);
NSData* keyData = [NSData dataWithBytes:publicKey length:keySize];
[[NSUserDefaults standardUserDefaults] setObject:keyData forKey:@"publicKey"];
keySize = SecKeyGetBlockSize(privateKey);
keyData = [NSData dataWithBytes:privateKey length:keySize];
[[NSUserDefaults standardUserDefaults] setObject:keyData forKey:@"privateKey"];
}
当我第一次运行应用程序时,它工作正常。
- (IBAction)save:(id)sender {
//save the context and dismiss the AddNewNotesViewcontroller
uint8_t *cipherBuffer = NULL;
SecKey *encrypt = [[SecKey alloc]init];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *data2 = [defaults objectForKey:@"publicKey"];
encrypt.publicKey =(SecKeyRef)[data2 bytes];
cipherBuffer = [encrypt Encryption:plainTextField.text];
NSMutableData *data = [[NSMutableData alloc]init];
[data appendBytes:cipherBuffer length:strlen((char*)cipherBuffer)+1];
[self.currentNote setNotesTitle:titleField.text];
[self.currentNote setEncryptedText:data];
[self.delegate addNewNoteViewControllerDidSave];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:@"Decrypt"])
{
UITextField *passcode = [alertView textFieldAtIndex:0];
if ([passcode.text isEqual: @"1234"]) {
SecKey *encrypt = [[SecKey alloc]init];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *data2 = [defaults objectForKey:@"publicKey"];
encrypt.publicKey =(SecKeyRef)[data2 bytes];
data2 = [defaults objectForKey:@"privateKey"];
encrypt.privateKey =(SecKeyRef)[data2 bytes];
NSData *etData = [[NSData alloc]init];
etData = [self.currentNote encryptedText];
const void *bytes = [etData bytes];
uint8_t *crypto_data = (uint8_t*)bytes;
NSLog(@"test %s",crypto_data);
NSString *pb =[encrypt Decryption:crypto_data];
self.encryptedText.text =pb;
}
else self.encryptedText.text = @"WRONG PASSWORD";
}
}
这是解密过程
- (void)decryptWithPrivateKey:(uint8_t *)cipherBuffer plainBuffer:(uint8_t *)plainBuffer
{
OSStatus status = noErr;
size_t cipherBufferSize = strlen((char *)cipherBuffer);
NSLog(@"decryptWithPrivateKey: length of buffer: %lu", BUFFER_SIZE);
NSLog(@"decryptWithPrivateKey: length of input: %lu", cipherBufferSize);
size_t plainBufferSize = BUFFER_SIZE;
status = SecKeyDecrypt([self getPrivateKey],
PADDING,
&cipherBuffer[0],
cipherBufferSize,
&plainBuffer[0],
&plainBufferSize
);
NSLog(@"decryption result code: %ld (size: %lu)", status, plainBufferSize);
NSLog(@"FINAL decrypted text: %s", plainBuffer);
}
-(NSString *)Decryption:(uint8_t *)cipherBuffer {
plainBuffer = (uint8_t *)calloc(CIPHER_BUFFER_SIZE, sizeof(uint8_t));
[self decryptWithPrivateKey:cipherBuffer plainBuffer:plainBuffer];
NSLog(@"DECRYPTED DATA : %s",plainBuffer);
NSString *s = [[NSString alloc] initWithBytes:plainBuffer length:BUFFER_SIZE encoding:NSUTF8StringEncoding];
return s;
}
任何想法为什么会发生这种情况?我猜它一定是当它试图从 NSUserDefaults 检索私钥时。因为第一次创建该对时,它存储在 NSUserDefaults 中,但稍后我解密笔记时不会检索它,因为我还没有关闭应用程序。