Issues with AES Encryption

Source: Internet
Author: User

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

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.