天天看點

iO裡加密字元串、圖檔、視訊方法

1、使用gtmbase64編碼解碼字元串

http://code.google.com/p/google-toolbox-for-mac/source/browse/trunk/foundation/?r=87

2、編解碼函數(可以編解碼字元串、圖檔、視訊:filepath換成相應的即可):

從模拟器和真機的documents路徑下讀取檔案,編碼後寫入檔案;讀出來解碼

// 加密函數

-(void)func_encodefile

{

    //nsstring *path = [nshomedirectory() stringbyappendingformat:@"/documents/test.png"];

    nsstring *filepath = [nshomedirectory() stringbyappendingformat:@"/documents/iphone4.mov"];

    //檔案路徑轉換為nsdata

    nsdata *imagedataorigin = [nsdata datawithcontentsoffile:filepath];

    // 對前1000位進行異或處理

    unsigned char * cbyte = (unsigned char*)[imagedataorigin bytes];

    for (int index = 0; (index < [imagedataorigin length]) && (index < 1000);

index++, cbyte++)

    {

         *cbyte = (*cbyte) ^ arrayforencode[index];

    }

    //對nsdata進行base64編碼

    nsdata *imagedataencode = [gtmbase64 encodedata:imagedataorigin];

    [imagedataencode writetofile:filepath atomically:yes];

}

// 解密函數

-(void)func_decodefile

    //nsstring *filepath = [nshomedirectory() stringbyappendingformat:@"/documents/test.png"];

    // 讀取被加密檔案對應的資料

    nsdata *dataencoded = [nsdata datawithcontentsoffile:filepath];

    // 對nsdata進行base64解碼 

    nsdata *datadecode = [gtmbase64 decodedata:dataencoded];

    unsigned char * cbyte = (unsigned char*)[datadecode bytes];

    for (int index = 0; (index < [datadecode length]) && (index < 10);

        *cbyte = (*cbyte) ^ arrayforencode[index];

    [datadecode writetofile:filepath atomically:yes];

繼續閱讀