views:

51

answers:

2
+1  Q: 

AES test on iphone

my encryption is working but i cant decrypt kindly suggest what i am doing wrong here

NSString *passphrase = @"hello";
    NSStringEncoding myEncoding = NSASCIIStringEncoding;

    NSString *alphaStringPlain = @"cell";
    NSData *alphaDataPlain = [alphaStringPlain dataUsingEncoding:myEncoding];

    NSData *alphaDataCypher = [alphaDataPlain AESEncryptWithPassphrase:passphrase];
    NSString *alphaStringCypher = [[NSString alloc] initWithData:alphaDataCypher encoding:myEncoding];

    NSLog(alphaStringCypher); // perfeclty encypted i guess 

    /////// FOR DECRYPTION///////////////




    NSData *zCypher = [alphaDataPlain AESDecryptWithPassphrase:alphaStringCypher];
    NSString *Cypher = [[NSString alloc] initWithData:zCypher encoding:myEncoding];



    NSLog(@" decode %@",[Cypher dataUsingEncoding:NSUTF8StringEncoding]);


    NSLog(@"  decode %@",Cypher);// not working some garbage value
A: 

I think you are decrypting wrong value try with this :

NSData *zCypher = [alphaStringCypher AESDecryptWithPassphrase:passphrase];
Gyani
no i couldn't find the plain text hello or Cell?? using your code also gives me garbage value :(
prajakta
u may get help from http://blog.objectgraph.com/index.php/2010/04/20/encrypting-decrypting-base64-encode-decode-in-iphone-objective-c/
Gyani
i got it thanks a lot
prajakta
A: 

After struggling i got the ans

NSString *passphrase = @"1234567812345678";
        NSStringEncoding myEncoding = NSASCIIStringEncoding;

        NSString *alphaStringPlain = @"hello";

        NSData *alphaDataPlain = [alphaStringPlain dataUsingEncoding:myEncoding];

        NSData *alphaDataCypher = [alphaDataPlain AESEncryptWithPassphrase:passphrase];
        NSString *alphaStringCypher = [[NSString alloc] initWithData:alphaDataCypher encoding:myEncoding];

        NSLog(alphaStringCypher); 

        ///////
        NSData *zCypher = [alphaDataCypher AESDecryptWithPassphrase:passphrase];
        NSString *Cypher = [[NSString alloc] initWithData:zCypher encoding:myEncoding];


        //NSData *zCypher = [alphaStringCypher AESDecryptWithPassphrase:passphrase];

        NSLog(@" hua kya decode %@",cypher);// working 
prajakta