2010-04-05 4 views
9

Je tente de crypter/décrypter un fichier texte brut dans mon éditeur de texte. cryptage semble bien fonctionner, mais le décryptage ne fonctionne pas, le texte apparaît crypté. Je suis certain que j'ai décrypté le texte en utilisant le mot que j'ai crypté avec - quelqu'un pourrait regarder à travers l'extrait ci-dessous et m'aider?Cryptage/décryptage de classe NSData-AES dans Cocoa

Merci :)

Encrypting:

NSAlert *alert = [NSAlert alertWithMessageText:@"Encryption" 
            defaultButton:@"Set" 
            alternateButton:@"Cancel" 
             otherButton:nil 
         informativeTextWithFormat:@"Please enter a password to encrypt your file with:"]; 
    [alert setIcon:[NSImage imageNamed:@"License.png"]]; 
    NSSecureTextField *input = [[NSSecureTextField alloc] initWithFrame:NSMakeRect(0, 0, 300, 24)]; 
    [alert setAccessoryView:input]; 
    NSInteger button = [alert runModal]; 
    if (button == NSAlertDefaultReturn) { 
    [[NSUserDefaults standardUserDefaults] setObject:[input stringValue] forKey:@"password"]; 
    NSData *data; 
    [self setString:[textView textStorage]]; 
    NSMutableDictionary *dict = [NSDictionary dictionaryWithObject:NSPlainTextDocumentType 
                  forKey:NSDocumentTypeDocumentAttribute]; 
    [textView breakUndoCoalescing]; 
    data = [[self string] dataFromRange:NSMakeRange(0, [[self string] length]) 
        documentAttributes:dict error:outError]; 
    NSData*encrypt = [data AESEncryptWithPassphrase:[input stringValue]]; 
    [encrypt writeToFile:[absoluteURL path] atomically:YES]; 

Décryptage:

NSAlert *alert = [NSAlert alertWithMessageText:@"Decryption" 
            defaultButton:@"Open" 
            alternateButton:@"Cancel" 
             otherButton:nil 
         informativeTextWithFormat:@"This file has been protected with a password.To view its contents,enter the password below:"]; 
    [alert setIcon:[NSImage imageNamed:@"License.png"]]; 
    NSSecureTextField *input = [[NSSecureTextField alloc] initWithFrame:NSMakeRect(0, 0, 300, 24)]; 
    [alert setAccessoryView:input]; 
    NSInteger button = [alert runModal]; 
    if (button == NSAlertDefaultReturn) { 
    NSLog(@"Entered Password - attempting to decrypt.");  
    NSMutableDictionary *dict = [NSDictionary dictionaryWithObject:NSPlainTextDocumentType 
                   forKey:NSDocumentTypeDocumentOption]; 
    NSData*decrypted = [[NSData dataWithContentsOfFile:[self fileName]] AESDecryptWithPassphrase:[input stringValue]]; 
    mString = [[NSAttributedString alloc] 
       initWithData:decrypted options:dict documentAttributes:NULL 
       error:outError]; 
+0

D'où viennent les méthodes '--AESEncryptWithPassphrase:' et '-AESDecryptWithPassphrase:'? –

+0

Salut Rob, j'ai obtenu la classe NSData + AES (qui inclut ces méthodes) d'ici: http: //iphonedevelopment.blogspot.com/2009/02/strong-encryption-for-cocoa-cocoa-touch.html – Pripyat

+0

le problème semble s'être fixé après avoir changé la valeur des keybits à 128. – Pripyat

Répondre

20

Pourquoi ne pas utiliser les algorithmes de chiffrement intégré? Voici un NSData+AES que j'ai écrit qui utilise CCCrypt avec une clé de 256it pour le cryptage AES256.

Vous pouvez l'utiliser comme:

NSData *data = [[NSData dataWithContentsOfFile:@"/etc/passwd"] 
          encryptWithString:@"mykey"]; 

et décrypter avec:

NSData *file = [data decryptWithString:@"mykey"]; 

AVERTISSEMENT: Il ne garantit mon NSData+AES est sans bug :) Il est assez nouveau. J'apprécie les critiques de code.

+0

merci. J'ai codé quelque chose comme cela il y a un certain temps, aussi codé celui qui fonctionne avec NSMutableDictionary - génial pour les fichiers de propriétés de licence;) – Pripyat

+0

Y a-t-il des sels dans le cryptage de votre @nicerobot fourni? –

+0

@TwoDumpling Il existe un support pour un vecteur initial fourni par CCCrypt mais le sel est facilement ajouté au texte à crypter. – nicerobot