[]ios/android/java/node.js Universal AES256 and decryption algorithm for palm-eye

Source: Internet
Author: User

Example.m

@" text "  @ "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";     *data =*encrypteddata = [[Data aes256encryptwithkey:key32] Base64encodedstringwithoptions:0 ]; NSLog (@ "%@", EncryptedData); # = qfplkbn20bzi1nihigoo6g==

Nsdata+aes256.m

-(NSData *) Aes256encryptwithkey: (NSString *) Key {//' key ' should is bytes for AES256, 'll be null-padded otherwise    Charkeyptr[kcckeysizeaes256+1];//Terminator (unused)Bzero (Keyptr,sizeof(keyptr));//fill with zeroes (for padding)//Fetch key Data[Key getcstring:keyptr MaxLength:sizeof(KEYPTR) encoding:nsutf8stringencoding]; Nsuinteger datalength=[self length]; //See the doc:for block ciphers, the output size would always be less than or//equal to the input size plus the size of one block. //That's why we need to add the size of a block heresize_t buffersize = datalength +kCCBlockSizeAES128; void*buffer =malloc (buffersize); size_t numbytesencrypted=0; Cccryptorstatus Cryptstatus=Cccrypt (Kccencrypt, kCCAlgorithmAES128, kccoptionpkcs7padding, Keyptr, Kccke ySizeAES256, NULL/*initialization vector (optional)*/, [self bytes], datalength,/*input*/buffer, buffersize,/*Output*/&numbytesencrypted); if(Cryptstatus = =kccsuccess) {        //The returned NSData takes ownership of the buffer and would free it on deallocation        return[NSData Datawithbytesnocopy:buffer length:numbytesencrypted]; } free (buffer); //Free the buffer;    returnNil;} -(NSData *) Aes256decryptwithkey: (NSString *) Key {//' key ' should is bytes for AES256, 'll be null-padded otherwise    Charkeyptr[kcckeysizeaes256+1];//Terminator (unused)Bzero (Keyptr,sizeof(keyptr));//fill with zeroes (for padding)//Fetch key Data[Key getcstring:keyptr MaxLength:sizeof(KEYPTR) encoding:nsutf8stringencoding]; Nsuinteger datalength=[self length]; //See the doc:for block ciphers, the output size would always be less than or//equal to the input size plus the size of one block. //That's why we need to add the size of a block heresize_t buffersize = datalength +kCCBlockSizeAES128; void*buffer =malloc (buffersize); size_t numbytesdecrypted=0; Cccryptorstatus Cryptstatus=Cccrypt (Kccdecrypt, kCCAlgorithmAES128, kccoptionpkcs7padding, Keyptr, Kccke ySizeAES256, NULL/*initialization vector (optional)*/, [self bytes], datalength,/*input*/buffer, buffersize,/*Output*/&numbytesdecrypted); if(Cryptstatus = =kccsuccess) {        //The returned NSData takes ownership of the buffer and would free it on deallocation        return[NSData Datawithbytesnocopy:buffer length:numbytesdecrypted]; } free (buffer); //Free the buffer;    returnNil;}

Aes256.java

/*** Encodes a String in AES-256 with a given key * *@paramContext *@paramPassword *@paramtext *@returnstring Base64 and AES encoded string*/ Public StaticString encode (String keystring, String stringtoencode)throwsNullPointerException {if(keystring.length () = = 0 | | keystring = =NULL) {        Throw NewNullPointerException ("Please give Password"); }        if(stringtoencode.length () = = 0 | | stringtoencode = =NULL) {        Throw NewNullPointerException ("Please give text"); }        Try{secretkeyspec Skeyspec=GetKey (keystring); byte[] cleartext = Stringtoencode.getbytes ("UTF8"); //IMPORTANT to GET same RESULTS on IOS and ANDROID        Final byte[] IV =New byte[16]; Arrays.fill (iv, (byte) 0x00); Ivparameterspec Ivparameterspec=NewIvparameterspec (iv); //Cipher is not thread safeCipher Cipher = cipher.getinstance ("aes/cbc/pkcs7padding");                Cipher.init (Cipher.encrypt_mode, Skeyspec, Ivparameterspec); String Encrypedvalue=base64.encodetostring (cipher.dofinal (cleartext), base64.default); LOG.D ("Jacek", "Encrypted:" + stringtoencode + "+" +encrypedvalue); returnEncrypedvalue; } Catch(InvalidKeyException e) {e.printstacktrace (); } Catch(unsupportedencodingexception e) {e.printstacktrace (); } Catch(nosuchalgorithmexception e) {e.printstacktrace (); } Catch(badpaddingexception e) {e.printstacktrace (); } Catch(nosuchpaddingexception e) {e.printstacktrace (); } Catch(illegalblocksizeexception e) {e.printstacktrace (); } Catch(invalidalgorithmparameterexception e) {e.printstacktrace (); }    return"";} /*** Decodes a String using AES-256 and Base64 * *@paramContext *@paramPassword *@paramtext *@returndesoded String*/ PublicString decode (string password, string text)throwsNullPointerException {if(password.length () = = 0 | | password = =NULL) {        Throw NewNullPointerException ("Please give Password"); }        if(text.length () = = 0 | | text = =NULL) {        Throw NewNullPointerException ("Please give text"); }        Try{Secretkey key=GetKey (password); //IMPORTANT to GET same RESULTS on IOS and ANDROID        Final byte[] IV =New byte[16]; Arrays.fill (iv, (byte) 0x00); Ivparameterspec Ivparameterspec=NewIvparameterspec (iv); byte[] Encrypedpwdbytes =Base64.decode (text, base64.default); //cipher is not thread safeCipher Cipher = cipher.getinstance ("aes/cbc/pkcs7padding");        Cipher.init (Cipher.decrypt_mode, Key, Ivparameterspec); byte[] Decrypedvaluebytes =(Cipher.dofinal (encrypedpwdbytes)); String Decrypedvalue=NewString (decrypedvaluebytes); LOG.D (Log_tag,"Decrypted:" + text + "+" +decrypedvalue); returnDecrypedvalue; } Catch(InvalidKeyException e) {e.printstacktrace (); } Catch(unsupportedencodingexception e) {e.printstacktrace (); } Catch(nosuchalgorithmexception e) {e.printstacktrace (); } Catch(badpaddingexception e) {e.printstacktrace (); } Catch(nosuchpaddingexception e) {e.printstacktrace (); } Catch(illegalblocksizeexception e) {e.printstacktrace (); } Catch(invalidalgorithmparameterexception e) {e.printstacktrace (); }    return"";} /*** Generates a SECRETKEYSPEC for given password * *@paramPassword *@returnSecretkeyspec *@throwsunsupportedencodingexception*/Private StaticSecretkeyspec GetKey (String password)throwsunsupportedencodingexception {//can change it to wish    intKeylength = 256; byte[] Keybytes =New byte[KEYLENGTH/8]; //explicitly fill with zerosArrays.fill (Keybytes, (byte) 0x0); //If password is shorter then key length, it 'll be zero-padded//To key length    byte[] passwordbytes = Password.getbytes ("UTF-8"); intLength = Passwordbytes.length < keybytes.length?passwordBytes.length:keyBytes.length; System.arraycopy (Passwordbytes,0, Keybytes, 0, length); Secretkeyspec Key=NewSecretkeyspec (Keybytes, "AES"); returnkey;}

node. js

Crypto = require (' crypto '= ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 'new Buffer () Iv.fill ( 0= ' text '= Crypto.createcipheriv (' AES-256-CBC '= cipher.update (' text ', ' UTF8 ', ' Base64 '+ = cipher.final (' base64' = qfplkbn20bzi1nihigoo6g==

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.