1 //String Encryption2-(void) Demo1 {3 //normal 8 bit binary data4NSString *str =@"Hello world!";5 //converting a string into binary data6NSData *data =[str datausingencoding:nsutf8stringencoding];7 8 //base64 binary data after encryption9NSData *base64data = [Data base64encodeddatawithoptions:0];Ten //base64 the encrypted string OneNSString *base64string = [Data base64encodedstringwithoptions:0]; A -[Data writetofile:@"/users/zhufeng/desktop/data"Atomically:yes]; -[Base64data WriteToFile:@"/users/zhufeng/desktop/base64data"Atomically:yes]; the //[base64string writetofile:@ "/users/zhufeng/desktop/base64string" atomically:yes]; - //Output -NSLog (@"Data:%@", data); -NSLog (@"base64data:%@", base64data); +NSLog (@"base64string:%@", base64string); - } + A //Encrypting File Information at-(void) Demo2 { - //1. Get binary data for files -NSData *data = [NSData datawithcontentsoffile:@"/users/zhufeng/desktop/wode.jpg"]; - - //2. Base64 encryption of the resulting file binary data -NSData *base64data = [Data base64encodeddatawithoptions:0]; in - //3. Storing data after encryption to[Base64data WriteToFile:@"/users/zhufeng/desktop/wodezhaopian"Atomically:yes]; + } - the //Decrypting file Information *-(void) Demo3 { $ //1. Get Base64 encrypted binary dataPanax NotoginsengNSData *base64data = [NSData datawithcontentsoffile:@"/users/zhufeng/desktop/wodezhaopian"]; - the //2. Convert Base64 encrypted binary data to normal binary data +NSData *data = [[NSData alloc] initwithbase64encodeddata:base64data options:0]; A the //3. Store the decrypted file +[Data writetofile:@"/users/zhufeng/desktop/my decrypted photo. gif"Atomically:yes]; - } $ $ - //Video Encryption --(void) Demo4 { the //1. Get binary data for files -NSData *data = [NSData datawithcontentsoffile:@"/users/zhufeng/desktop/x Travels of the D-Saint return. Monkey.King.Hero.is.Back.mp4"];Wuyi the //2. Base64 encryption of the resulting file binary data -NSData *base64data = [Data base64encodeddatawithoptions:0]; Wu - //3. Storing data after encryption About[Base64data WriteToFile:@"/users/zhufeng/desktop/x Travels of the D-Saint return. MP4"Atomically:yes]; $ } - //Video Decryption --(void) Demo5 { - //1. Get the binary data of the encrypted base64 file ANSData *base64data = [NSData datawithcontentsoffile:@"/users/zhufeng/desktop/x Travels of the D-Saint return. MP4"]; + the //2. Convert a base64 encrypted file to a normal binary file -NSData *data = [[NSData alloc] initwithbase64encodeddata:base64data options:0]; $ the //3. Store the decrypted file the[Data writetofile:@"/users/zhufeng/desktop/x's Travels of the D Holy return declassified. mp4"Atomically:yes]; the the}
Base64 Encrypting and decrypting files