Encountered a project, need to use AES encryption password, Android has been written, Java source code:
Private Static FinalString aes_options = "Aes/ecb/pkcs5padding"; Private Final StaticString CHARSET = "Utf-8"; Public Staticstring Encryptaes (String plain, string key) {Try{Cipher Cipher=cipher.getinstance (aes_options); Cipher.init (Cipher.encrypt_mode,NewSecretkeyspec (Key.getbytes (CHARSET), "AES")); returnbase64.encodetostring (cipher.dofinal (Plain.getbytes (CHARSET)), Base64.default); } Catch(Exception e) {e.printstacktrace (); return NULL; } }
Then, I also need to do AES encryption, encryption key I and Android are the same, but the same encryption "123456" is not the same,
Correct: xpuzm4ze0uirp4kn4y6ipg==
Paste the iOS AES encryption code:
// turn string into data+ (nsstring*) encryptaesdata with password: (nsstring*)string app_key: (nsstring*) key;
#pragmaMark-aes encryption//turns a string into a password-coded data+ (nsstring*) Encryptaesdata: (nsstring*)stringApp_key: (nsstring*) key{//Convert nsstring to NSDataNSData *data = [stringdatausingencoding:nsutf8stringencoding]; //encrypt the nsdata with a passwordNSData *encrypteddata =[Data Aes128encryptwithkey:key]; NSLog (@"encrypted string:%@", [EncryptedData base64encodedstring]); return[EncryptedData base64encodedstring];}
#import "Nsdata+aes.h"
#import <CommonCrypto/CommonCryptor.h>
#define GIV @ "AES"//Can be modified by itself
-(NSData *) Aes128encryptwithkey: (NSString *) Key {//Encrypt Charkeyptr[kcckeysizeaes128+1]; Bzero (Keyptr,sizeof(keyptr)); [Key getcstring:keyptr MaxLength:sizeof(KEYPTR) encoding:nsutf8stringencoding]; Charivptr[kcckeysizeaes128+1]; memset (Ivptr,0,sizeof(ivptr)); [GIv getcstring:ivptr maxLength:sizeof(IVPTR) 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, Kccblocksi zeAES128, ivptr, [self bytes], datalength, Buffer, BufferSize,&numbytesencrypted); if(Cryptstatus = =kccsuccess) { return[NSData Datawithbytesnocopy:buffer length:numbytesencrypted]; } free (buffer); returnNil;}
In the bottom-level approach:
-(NSData *) Aes128encryptwithkey: (NSString *) key {//encryption
Have a
Giv parameters, here's the
1, giv must be changed to Java code in the "AES",
2, because iOS does not have the filling algorithm pkcs5padding, so in iOS to write this: Kccoptionpkcs7padding|kccoptionecbmode,
This is the basic change.
Issues with AES Encryption