久久久精品一区ed2k-女人被男人叉到高潮的视频-中文字幕乱码一区久久麻豆樱花-俄罗斯熟妇真实视频

ios開發(fā)des加密,ios數(shù)據(jù)加密的幾種方式

ios怎么對網(wǎng)頁請求下來的des加密的data進行解密

之前在項目上用到AES256加密解密算法,剛開始在java端加密解密都沒有問題,在iOS端加密解密也沒有問題。但是奇怪的是在java端加密后的文件在iOS端無法正確解密打開,然后簡單測試了一下,發(fā)現(xiàn)在java端和iOS端采用相同明文,相同密鑰加密后的密文不一樣!上網(wǎng)查了資料后發(fā)現(xiàn)iOS中AES加密算法采用的填充是PKCS7Padding,而java不支持PKCS7Padding,只支持PKCS5Padding。我們知道加密算法由算法+模式+填充組成,所以這兩者不同的填充算法導(dǎo)致相同明文相同密鑰加密后出現(xiàn)密文不一致的情況。那么我們需要在java中用PKCS7Padding來填充,這樣就可以和iOS端填充算法一致了。

在臨湘等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都做網(wǎng)站、網(wǎng)站建設(shè) 網(wǎng)站設(shè)計制作按需定制開發(fā),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),全網(wǎng)營銷推廣,外貿(mào)網(wǎng)站建設(shè),臨湘網(wǎng)站建設(shè)費用合理。

要實現(xiàn)在java端用PKCS7Padding填充,需要用到bouncycastle組件來實現(xiàn),下面我會提供該包的下載。啰嗦了一大堆,下面是一個簡單的測試,上代碼!

001 package com.encrypt.file;

002

003

004 import java.io.UnsupportedEncodingException;

005 importjava.security.Key;

006 import java.security.Security;

007

008 importjavax.crypto.Cipher;

009 importjavax.crypto.SecretKey;

010 importjavax.crypto.spec.SecretKeySpec;

011

012 public classAES256Encryption{

013

014 /**

015 * 密鑰算法

016 * java6支持56位密鑰,bouncycastle支持64位

017 * */

018 public static finalString KEY_ALGORITHM="AES";

019

020 /**

021 * 加密/解密算法/工作模式/填充方式

022 *

023 * JAVA6 支持PKCS5PADDING填充方式

024 * Bouncy castle支持PKCS7Padding填充方式

025 * */

026 public static finalString CIPHER_ALGORITHM="AES/ECB/PKCS7Padding";

027

028 /**

029 *

030 * 生成密鑰,java6只支持56位密鑰,bouncycastle支持64位密鑰

031 * @return byte[] 二進制密鑰

032 * */

033 public static byte[] initkey() throwsException{

034

035 // //實例化密鑰生成器

036 // Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

037 // KeyGenerator kg=KeyGenerator.getInstance(KEY_ALGORITHM, "BC");

038 // //初始化密鑰生成器,AES要求密鑰長度為128位、192位、256位

039 //// kg.init(256);

040 // kg.init(128);

041 // //生成密鑰

042 // SecretKey secretKey=kg.generateKey();

043 // //獲取二進制密鑰編碼形式

044 // return secretKey.getEncoded();

045 //為了便于測試,這里我把key寫死了,如果大家需要自動生成,可用上面注釋掉的代碼

046 return new byte[] { 0x08, 0x08, 0x04, 0x0b, 0x02, 0x0f, 0x0b, 0x0c,

047 0x01, 0x03, 0x09, 0x07, 0x0c, 0x03, 0x07, 0x0a, 0x04, 0x0f,

048 0x06, 0x0f, 0x0e, 0x09, 0x05, 0x01, 0x0a, 0x0a, 0x01, 0x09,

049 0x06, 0x07, 0x09, 0x0d };

050 }

051

052 /**

053 * 轉(zhuǎn)換密鑰

054 * @param key 二進制密鑰

055 * @return Key 密鑰

056 * */

057 public static Key toKey(byte[] key) throwsException{

058 //實例化DES密鑰

059 //生成密鑰

060 SecretKey secretKey=newSecretKeySpec(key,KEY_ALGORITHM);

061 returnsecretKey;

062 }

063

064 /**

065 * 加密數(shù)據(jù)

066 * @param data 待加密數(shù)據(jù)

067 * @param key 密鑰

068 * @return byte[] 加密后的數(shù)據(jù)

069 * */

070 public static byte[] encrypt(byte[] data,byte[] key) throwsException{

071 //還原密鑰

072 Key k=toKey(key);

073 /**

074 * 實例化

075 * 使用 PKCS7PADDING 填充方式,按如下方式實現(xiàn),就是調(diào)用bouncycastle組件實現(xiàn)

076 * Cipher.getInstance(CIPHER_ALGORITHM,"BC")

077 */

078 Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

079 Cipher cipher=Cipher.getInstance(CIPHER_ALGORITHM, "BC");

080 //初始化,設(shè)置為加密模式

081 cipher.init(Cipher.ENCRYPT_MODE, k);

082 //執(zhí)行操作

083 returncipher.doFinal(data);

084 }

085 /**

086 * 解密數(shù)據(jù)

087 * @param data 待解密數(shù)據(jù)

088 * @param key 密鑰

089 * @return byte[] 解密后的數(shù)據(jù)

090 * */

091 public static byte[] decrypt(byte[] data,byte[] key) throwsException{

092 //歡迎密鑰

093 Key k =toKey(key);

094 /**

095 * 實例化

096 * 使用 PKCS7PADDING 填充方式,按如下方式實現(xiàn),就是調(diào)用bouncycastle組件實現(xiàn)

097 * Cipher.getInstance(CIPHER_ALGORITHM,"BC")

098 */

099 Cipher cipher=Cipher.getInstance(CIPHER_ALGORITHM);

100 //初始化,設(shè)置為解密模式

101 cipher.init(Cipher.DECRYPT_MODE, k);

102 //執(zhí)行操作

103 returncipher.doFinal(data);

104 }

105 /**

106 * @param args

107 * @throws UnsupportedEncodingException

108 * @throws Exception

109 */

110 public static void main(String[] args) throwsUnsupportedEncodingException{

111

112 String str="AES";

113 System.out.println("原文:"+str);

114

115 //初始化密鑰

116 byte[] key;

117 try {

118 key = AES256Encryption.initkey();

119 System.out.print("密鑰:");

120 for(int i = 0;ikey.length;i++){

121 System.out.printf("%x", key[i]);

122 }

123 System.out.print("\n");

124 //加密數(shù)據(jù)

125 byte[] data=AES256Encryption.encrypt(str.getBytes(), key);

126 System.out.print("加密后:");

127 for(int i = 0;idata.length;i++){

128 System.out.printf("%x", data[i]);

129 }

130 System.out.print("\n");

131

132 //解密數(shù)據(jù)

133 data=AES256Encryption.decrypt(data, key);

134 System.out.println("解密后:"+newString(data));

135 } catch (Exception e) {

136 // TODO Auto-generated catch block

137 e.printStackTrace();

138 }

139

140 }

141 }

運行程序后的結(jié)果截圖:

ViewController.m文件

01 //

02 // ViewController.m

03 // AES256EncryptionDemo

04 //

05 // Created by 孫 裔 on 12-12-13.

06 // Copyright (c) 2012年 rich sun. All rights reserved.

07 //

08

09 #import "ViewController.h"

10 #import "EncryptAndDecrypt.h"

11

12 @interface ViewController ()

13

14 @end

15

16 @implementation ViewController

17 @synthesize plainTextField;

18 - (void)viewDidLoad

19 {

20 [super viewDidLoad];

21 // Do any additional setup after loading the view, typically from a nib.

22 }

23

24 - (void)didReceiveMemoryWarning

25 {

26 [super didReceiveMemoryWarning];

27 // Dispose of any resources that can be recreated.

28 }

29 //這個函數(shù)實現(xiàn)了用戶輸入完后點擊視圖背景,關(guān)閉鍵盤

30 - (IBAction)backgroundTap:(id)sender{

31 [plainTextField resignFirstResponder];

32 }

33

34 - (IBAction)encrypt:(id)sender {

35

36 NSString *plainText = plainTextField.text;//明文

37 NSData *plainTextData = [plainText dataUsingEncoding:NSUTF8StringEncoding];

38

39 //為了測試,這里先把密鑰寫死

40 Byte keyByte[] = {0x08,0x08,0x04,0x0b,0x02,0x0f,0x0b,0x0c,0x01,0x03,0x09,0x07,0x0c,0x03,

41 0x07,0x0a,0x04,0x0f,0x06,0x0f,0x0e,0x09,0x05,0x01,0x0a,0x0a,0x01,0x09,

42 0x06,0x07,0x09,0x0d};

43 //byte轉(zhuǎn)換為NSData類型,以便下邊加密方法的調(diào)用

44 NSData *keyData = [[NSData alloc] initWithBytes:keyByte length:32];

45 //

46 NSData *cipherTextData = [plainTextData AES256EncryptWithKey:keyData];

47 Byte *plainTextByte = (Byte *)[cipherTextData bytes];

48 for(int i=0;i[cipherTextData length];i++){

49 printf("%x",plainTextByte[i]);

50 }

51

52 }

53 @end

附上出處鏈接:

iOS DES加密 解密

頭文件

#import CommonCrypto/CommonCryptor.h

NSString *const kInitVector = @"ffGGtsdfzxCv5568";

NSString *const DESKey = @"gg356tt8g5h6j9jh";

+ (NSString *)encodeDesWithString:(NSString *)str{

NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];

size_t plainTextBufferSize = [data length];

const void *vplainText = (const void *)[data bytes];

CCCryptorStatus ccStatus;

uint8_t *bufferPtr = NULL;

size_t bufferPtrSize = 0;

size_t movedBytes = 0;

bufferPtrSize = (plainTextBufferSize + kCCBlockSizeDES) ~(kCCBlockSizeDES - 1);

bufferPtr = malloc( bufferPtrSize * sizeof(uint8_t));

memset((void *)bufferPtr, 0x0, bufferPtrSize);

const void *vkey = (const void *) [DESKey UTF8String];

const void *vinitVec = (const void *) [kInitVector UTF8String];

ccStatus = CCCrypt(kCCEncrypt,

? ? ? ? ? ? ? ? ? kCCAlgorithmDES,

? ? ? ? ? ? ? ? ? kCCOptionPKCS7Padding,

? ? ? ? ? ? ? ? ? vkey,

? ? ? ? ? ? ? ? ? kCCKeySizeDES,

? ? ? ? ? ? ? ? ? vinitVec,

? ? ? ? ? ? ? ? ? vplainText,

? ? ? ? ? ? ? ? ? plainTextBufferSize,

? ? ? ? ? ? ? ? ? (void *)bufferPtr,

? ? ? ? ? ? ? ? ? bufferPtrSize,

? ? ? ? ? ? ? ? ? movedBytes);

NSData *myData = [NSData dataWithBytes:(const void *)bufferPtr length:(NSUInteger)movedBytes];

NSString *result = [myData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];

return result;

}

+ (NSString *)decodeDesWithString:(NSString *)str{

? ? NSData *encryptData = [[NSData alloc] initWithBase64EncodedString:str options:NSDataBase64DecodingIgnoreUnknownCharacters];

? ? size_t plainTextBufferSize = [encryptData length];

? ? const void *vplainText = [encryptData bytes];

? CCCryptorStatus ccStatus;

? ? uint8_t *bufferPtr = NULL;

? ? size_t bufferPtrSize = 0;

? ? size_t movedBytes = 0;

? bufferPtrSize = (plainTextBufferSize + kCCBlockSizeDES) ~(kCCBlockSizeDES - 1);

? ? bufferPtr = malloc( bufferPtrSize * sizeof(uint8_t));

? ? memset((void *)bufferPtr, 0x0, bufferPtrSize);

? const void *vkey = (const void *) [DESKey UTF8String];

? ? const void *vinitVec = (const void *) [kInitVector UTF8String];

? ccStatus = CCCrypt(kCCDecrypt,

? ? ? ? ? ? ? ? ? ? ? kCCAlgorithmDES,

? ? ? ? ? ? ? ? ? ? ? kCCOptionPKCS7Padding,

? ? ? ? ? ? ? ? ? ? ? vkey,

? ? ? ? ? ? ? ? ? ? ? kCCKeySizeDES,

? ? ? ? ? ? ? ? ? ? ? vinitVec,

? ? ? ? ? ? ? ? ? ? ? vplainText,

? ? ? ? ? ? ? ? ? ? ? plainTextBufferSize,

? ? ? ? ? ? ? ? ? ? ? (void *)bufferPtr,

? ? ? ? ? ? ? ? ? ? ? bufferPtrSize,

? ? ? ? ? ? ? ? ? ? ? movedBytes);

? ? NSString *result = [[NSString alloc] initWithData:[NSData dataWithBytes:(const void *)bufferPtr

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? length:(NSUInteger)movedBytes] encoding:NSUTF8StringEncoding];

? ? return result;

}

iOS開發(fā)--AES加密中的那些坑

在開發(fā)中經(jīng)常會遇到數(shù)據(jù)的加密,常見的有base64、DES、AES、RSA等,由于AES的用法相對簡單一些,在公司的項目中,我們使用的是AES加密。但是遇到一個大坑就是后臺使用了AES的128/CBC/NoPadding加密模式,很可悲的是iOS中只有PKCS7Padding和PKCS5Padding這兩種模式,沒有NoPadding模式。經(jīng)過各種百度、谷歌后,終于發(fā)現(xiàn)了一篇文章解決了這個問題。

下面是參考文章的鏈接 :

問題就處在No Padding. No Pading的情況下,一定要對加密數(shù)據(jù)不是kCCKeySizeAES128倍數(shù)部分進行0x0000的填充,不然加密長度不正確,一般情況下選擇使用kCCOptionPKCS7Padding(也就是0x0001)進行填充,但是我們是No Padding所以要用 0x0000 填充。

跪求 DES跨(C# Android IOS)三個平臺通用的加解密方法

#region???跨平臺加解密(c#?安卓?IOS)

//??public?static?string?sKey?=?"12345678";

//??///?summary

//??///?解密

//??///?/summary

//??///?param?name="pToDecrypt"要解密的以Base64/param

//??///?param?name="sKey"密鑰,且必須為8位/param

//??///?returns已解密的字符串/returns

//??public?static?string?DesDecrypt(string?pToDecrypt)

//??{

//??????//轉(zhuǎn)義特殊字符

//??????pToDecrypt?=?pToDecrypt.Replace("-",?"+");

//??????pToDecrypt?=?pToDecrypt.Replace("_",?"/");

//??????pToDecrypt?=?pToDecrypt.Replace("~",?"=");

//??????byte[]?inputByteArray?=?Convert.FromBase64String(pToDecrypt);

//??????using?(DESCryptoServiceProvider?des?=?new?DESCryptoServiceProvider())

//??????{

//??????????des.Key?=?ASCIIEncoding.ASCII.GetBytes(sKey);

//??????????des.IV?=?ASCIIEncoding.ASCII.GetBytes(sKey);

//??????????System.IO.MemoryStream?ms?=?new?System.IO.MemoryStream();

//??????????using?(CryptoStream?cs?=?new?CryptoStream(ms,?des.CreateDecryptor(),?CryptoStreamMode.Write))

//??????????{

//??????????????cs.Write(inputByteArray,?0,?inputByteArray.Length);

//??????????????cs.FlushFinalBlock();

//??????????????cs.Close();

//??????????}

//??????????string?str?=?Encoding.UTF8.GetString(ms.ToArray());

//??????????ms.Close();

//??????????return?str;

//??????}

//??}

//??///?summary

//??///?對字符串進行DES加密

//??///?/summary

//??///?param?name="sourceString"待加密的字符串/param

//??///?returns加密后的BASE64編碼的字符串/returns

//??public?string?Encrypt(string?sourceString)

//{

//???byte[]?btKey?=?Encoding.UTF8.GetBytes(sKey);

//???byte[]?btIV?=?Encoding.UTF8.GetBytes(sKey);

//????DESCryptoServiceProvider?des?=?new?DESCryptoServiceProvider();

//????using?(MemoryStream?ms?=?new?MemoryStream())

//????{

//????????byte[]?inData?=?Encoding.UTF8.GetBytes(sourceString);

//????????try

//????????{

//????????????using?(CryptoStream?cs?=?new?CryptoStream(ms,?des.CreateEncryptor(btKey,?btIV),?CryptoStreamMode.Write))

//????????????{

//????????????????cs.Write(inData,?0,?inData.Length);

//????????????????cs.FlushFinalBlock();

//????????????}

//????????????return?Convert.ToBase64String(ms.ToArray());

//????????}

//????????catch

//????????{

//????????????throw;

//????????}

//????}

//}

#endregion

安卓---------------------------------------------------------------------------

//????//?加密

//public?static?String?DecryptDoNet(String?message,?String?key)

//????????throws?Exception?{

//????byte[]?bytesrc?=?Base64.decode(message.getBytes(),?Base64.DEFAULT);

//????Cipher?cipher?=?Cipher.getInstance("DES/CBC/PKCS5Padding");

//????DESKeySpec?desKeySpec?=?new?DESKeySpec(key.getBytes("UTF-8"));

//????SecretKeyFactory?keyFactory?=?SecretKeyFactory.getInstance("DES");

//????SecretKey?secretKey?=?keyFactory.generateSecret(desKeySpec);

//????IvParameterSpec?iv?=?new?IvParameterSpec(key.getBytes("UTF-8"));

//????cipher.init(Cipher.DECRYPT_MODE,?secretKey,?iv);

//????byte[]?retByte?=?cipher.doFinal(bytesrc);

//????return?new?String(retByte);

//}

////?解密

//public?static?String?EncryptAsDoNet(String?message,?String?key)

//????????throws?Exception?{

//????Cipher?cipher?=?Cipher.getInstance("DES/CBC/PKCS5Padding");

//????DESKeySpec?desKeySpec?=?new?DESKeySpec(key.getBytes("UTF-8"));

//????SecretKeyFactory?keyFactory?=?SecretKeyFactory.getInstance("DES");

//????SecretKey?secretKey?=?keyFactory.generateSecret(desKeySpec);

//????IvParameterSpec?iv?=?new?IvParameterSpec(key.getBytes("UTF-8"));

//????cipher.init(Cipher.ENCRYPT_MODE,?secretKey,?iv);

//????byte[]?encryptbyte?=?cipher.doFinal(message.getBytes());

//????return?new?String(Base64.encode(encryptbyte,?Base64.DEFAULT));

//}

Ios?--------------------------------------------------------------------------------------------------------------------\

static?const?char*?encryptWithKeyAndType(const?char?*text,CCOperation?encryptOperation,char?*key)

{

NSString?*textString=[[NSString?alloc]initWithCString:text?encoding:NSUTF8StringEncoding];

//??????NSLog(@"[[item.url?description]?UTF8String=%@",textString);

const?void?*dataIn;

size_t?dataInLength;

if?(encryptOperation?==?kCCDecrypt)//傳遞過來的是decrypt?解碼

{

//解碼?base64

NSData?*decryptData?=?[GTMBase64?decodeData:[textString?dataUsingEncoding:NSUTF8StringEncoding]];//轉(zhuǎn)成utf-8并decode

dataInLength?=?[decryptData?length];

dataIn?=?[decryptData?bytes];

}

else??//encrypt

{

NSData*?encryptData?=?[textString?dataUsingEncoding:NSUTF8StringEncoding];

dataInLength?=?[encryptData?length];

dataIn?=?(const?void?*)[encryptData?bytes];

}

CCCryptorStatus?ccStatus;

uint8_t?*dataOut?=?NULL;?//可以理解位type/typedef?的縮寫(有效的維護了代碼,比如:一個人用int,一個人用long。最好用typedef來定義)

size_t?dataOutAvailable?=?0;?//size_t??是操作符sizeof返回的結(jié)果類型

size_t?dataOutMoved?=?0;

dataOutAvailable?=?(dataInLength?+?kCCBlockSizeDES)??~(kCCBlockSizeDES?-?1);

dataOut?=?malloc(?dataOutAvailable?*?sizeof(uint8_t));

memset((void?*)dataOut,?00,?dataOutAvailable);//將已開辟內(nèi)存空間buffer的首?1?個字節(jié)的值設(shè)為值?0

//NSString?*initIv?=?@"12345678";

const?void?*vkey?=?key;

const?void?*iv?=?(const?void?*)?key;?//[initIv?UTF8String];

//CCCrypt函數(shù)?加密/解密

ccStatus?=?CCCrypt(encryptOperation,//??加密/解密

kCCAlgorithmDES,//??加密根據(jù)哪個標(biāo)準(zhǔn)(des,3des,aes。。。。)

kCCOptionPKCS7Padding,//??選項分組密碼算法(des:對每塊分組加一次密??3DES:對每塊分組加三個不同的密)

vkey,??//密鑰????加密和解密的密鑰必須一致

kCCKeySizeDES,//???DES?密鑰的大?。╧CCKeySizeDES=8)

iv,?//??可選的初始矢量

dataIn,?//?數(shù)據(jù)的存儲單元

dataInLength,//?數(shù)據(jù)的大小

(void?*)dataOut,//?用于返回數(shù)據(jù)

dataOutAvailable,

dataOutMoved);

NSString?*result?=?nil;

if?(encryptOperation?==?kCCDecrypt)//encryptOperation==1??解碼

{

//得到解密出來的data數(shù)據(jù),改變?yōu)閡tf-8的字符串

result?=?[[NSString?alloc]?initWithData:[NSData?dataWithBytes:(const?void?*)dataOut?length:(NSUInteger)dataOutMoved]?encoding:NSUTF8StringEncoding];

}

else?//encryptOperation==0??(加密過程中,把加好密的數(shù)據(jù)轉(zhuǎn)成base64的)

{

//編碼?base64

NSData?*data?=?[NSData?dataWithBytes:(const?void?*)dataOut?length:(NSUInteger)dataOutMoved];

result?=?[GTMBase64?stringByEncodingData:data];

}

return?[result?UTF8String];

}

+(NSString*)encryptWithContent:(NSString*)content?type:(CCOperation)type?key:(NSString*)aKey

{

const?char?*?contentChar?=[content?UTF8String];

char?*?keyChar?=(char*)[aKey?UTF8String];

const?char?*miChar;

miChar?=?encryptWithKeyAndType(contentChar,?type,?keyChar);

return??[NSString?stringWithCString:miChar?encoding:NSUTF8StringEncoding];

}

ios 中開發(fā)中用戶信息中的加密方式有哪些

5.1 通過簡單的URLENCODE + BASE64編碼防止數(shù)據(jù)明文傳輸

5.2 對普通請求、返回數(shù)據(jù),生成MD5校驗(MD5中加入動態(tài)密鑰),進行數(shù)據(jù)完整性(簡單防篡改,安全性較低,優(yōu)點:快速)校驗。

5.3 對于重要數(shù)據(jù),使用RSA進行數(shù)字簽名,起到防篡改作用。

5.4 對于比較敏感的數(shù)據(jù),如用戶信息(登陸、注冊等),客戶端發(fā)送使用RSA加密,服務(wù)器返回使用DES(AES)加密。

原因:客戶端發(fā)送之所以使用RSA加密,是因為RSA解密需要知道服務(wù)器私鑰,而服務(wù)器私鑰一般盜取難度較大;如果使用DES的話,可以通過破解客戶端獲取密鑰,安全性較低。而服務(wù)器返回之所以使用DES,是因為不管使用DES還是RSA,密鑰(或私鑰)都存儲在客戶端,都存在被破解的風(fēng)險,因此,需要采用動態(tài)密鑰,而RSA的密鑰生成比較復(fù)雜,不太適合動態(tài)密鑰,并且RSA速度相對較慢,所以選用DES)

把相關(guān)算法的代碼也貼一下吧 (其實使用一些成熟的第三方庫或許會來得更加簡單,不過自己寫,自由點)。注,這里的大部分加密算法都是參考一些現(xiàn)有成熟的算法,或者直接拿來用的。

1、MD5

//因為是使用category,所以木有參數(shù)傳入啦

-(NSString *) stringFromMD5 {

if(self == nil || [self length] == 0) {

return nil;

}

const char *value = [self UTF8String];

unsigned char outputBuffer[CC_MD5_DIGEST_LENGTH];

CC_MD5(value, strlen(value), outputBuffer);

NSMutableString *outputString = [[NSMutableString alloc] initWithCapacity:CC_MD5_DIGEST_LENGTH * 2];

for(NSInteger count = 0; count CC_MD5_DIGEST_LENGTH; count++){

[outputString appendFormat:@"%02x",outputBuffer[count]];

}

return [outputString autorelease];

}

2、Base64

+ (NSString *) base64EncodeData: (NSData *) objData {

const unsigned char * objRawData = [objData bytes];

char * objPointer;

char * strResult;

// Get the Raw Data length and ensure we actually have data

int intLength = [objData length];

if (intLength == 0) return nil;

// Setup the String-based Result placeholder and pointer within that placeholder

strResult = (char *)calloc(((intLength + 2) / 3) * 4, sizeof(char));

objPointer = strResult;

// Iterate through everything

while (intLength 2) { // keep going until we have less than 24 bits

*objPointer++ = _base64EncodingTable[objRawData[0] 2];

*objPointer++ = _base64EncodingTable[((objRawData[0] 0x03) 4) + (objRawData[1] 4)];

*objPointer++ = _base64EncodingTable[((objRawData[1] 0x0f) 2) + (objRawData[2] 6)];

*objPointer++ = _base64EncodingTable[objRawData[2] 0x3f];

// we just handled 3 octets (24 bits) of data

objRawData += 3;

intLength -= 3;

}

// now deal with the tail end of things

if (intLength != 0) {

*objPointer++ = _base64EncodingTable[objRawData[0] 2];

if (intLength 1) {

*objPointer++ = _base64EncodingTable[((objRawData[0] 0x03) 4) + (objRawData[1] 4)];

*objPointer++ = _base64EncodingTable[(objRawData[1] 0x0f) 2];

*objPointer++ = '=';

} else {

*objPointer++ = _base64EncodingTable[(objRawData[0] 0x03) 4];

*objPointer++ = '=';

*objPointer++ = '=';

}

}

// Terminate the string-based result

*objPointer = '\0';

NSString *rstStr = [NSString stringWithCString:strResult encoding:NSASCIIStringEncoding];

free(objPointer);

return rstStr;

}

3、AES

-(NSData*) EncryptAES: (NSString *) key {

char keyPtr[kCCKeySizeAES256+1];

bzero(keyPtr, sizeof(keyPtr));

[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];

NSUInteger dataLength = [self length];

size_t bufferSize = dataLength + kCCBlockSizeAES128;

void *buffer = malloc(bufferSize);

size_t numBytesEncrypted = 0;

CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128,

kCCOptionPKCS7Padding | kCCOptionECBMode,

keyPtr, kCCBlockSizeAES128,

NULL,

[self bytes], dataLength,

buffer, bufferSize,

numBytesEncrypted);

if (cryptStatus == kCCSuccess) {

return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];

}

free(buffer);

return nil;

}

4、RSA

- (NSData *) encryptWithData:(NSData *)content {

size_t plainLen = [content length];

if (plainLen maxPlainLen) {

NSLog(@"content(%ld) is too long, must %ld", plainLen, maxPlainLen);

return nil;

}

void *plain = malloc(plainLen);

[content getBytes:plain

length:plainLen];

size_t cipherLen = 128; // currently RSA key length is set to 128 bytes

void *cipher = malloc(cipherLen);

OSStatus returnCode = SecKeyEncrypt(publicKey, kSecPaddingPKCS1, plain,

plainLen, cipher, cipherLen);

NSData *result = nil;

if (returnCode != 0) {

NSLog(@"SecKeyEncrypt fail. Error Code: %ld", returnCode);

}

else {

result = [NSData dataWithBytes:cipher

length:cipherLen];

}

free(plain);

free(cipher);

return result;

}

本文標(biāo)題:ios開發(fā)des加密,ios數(shù)據(jù)加密的幾種方式
網(wǎng)站鏈接:http://sd-ha.com/article24/dsihcce.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作服務(wù)器托管、品牌網(wǎng)站設(shè)計、做網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)