Base64 Encrypting and decrypting files

Source: Internet
Author: User


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.