key= "1234578" used in encryption and decryption; Simultaneous value in the method of tuning
C # code
#regionCross-platform plus decryption (C #)/// <summary> ///des encryption of strings/// </summary> /// <param name= "sourcestring" >string to encrypt</param> /// <returns>BASE64 encoded string after encryption</returns> Public stringEncrypt (stringSourcestring,stringSKey) { byte[] BtKey =Encoding.UTF8.GetBytes (SKey); byte[] Btiv =Encoding.UTF8.GetBytes (SKey); DESCryptoServiceProvider des=NewDESCryptoServiceProvider (); using(MemoryStream ms =NewMemoryStream ()) { byte[] InData =Encoding.UTF8.GetBytes (sourcestring); Try { using(CryptoStream cs =NewCryptoStream (MS, Des. CreateEncryptor (BtKey, Btiv), CryptoStreamMode.Write)) {cs. Write (InData,0, indata.length); Cs. FlushFinalBlock (); } returnconvert.tobase64string (Ms. ToArray ()); } Catch { Throw; } } } /// <summary> ///decryption/// </summary> /// <param name= "Ptodecrypt" >To decrypt the Base64</param> /// <param name= "SKey" >Key , and must be 8-bit</param> /// <returns>decrypted String</returns> Public stringDecrypt (stringPtodecrypt,stringSKey) { //Escape Special CharactersPtodecrypt = Ptodecrypt.replace ("-","+"); Ptodecrypt= Ptodecrypt.replace ("_","/"); Ptodecrypt= Ptodecrypt.replace ("~","="); byte[] Inputbytearray =convert.frombase64string (Ptodecrypt); using(DESCryptoServiceProvider des =NewDESCryptoServiceProvider ()) {des. Key=ASCIIEncoding.ASCII.GetBytes (SKey); Des.iv=ASCIIEncoding.ASCII.GetBytes (SKey); System.IO.MemoryStream Ms=NewSystem.IO.MemoryStream (); using(CryptoStream cs =NewCryptoStream (MS, Des. CreateDecryptor (), cryptostreammode.write)) {cs. Write (Inputbytearray,0, inputbytearray.length); Cs. FlushFinalBlock (); Cs. Close (); } stringstr =Encoding.UTF8.GetString (Ms. ToArray ()); Ms. Close (); returnstr; } } #endregion
iOS code
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)//Pass Decrypt decoding { //decoding base64NSData *decryptdata = [GTMBase64 decodedata:[textstring datausingencoding:nsutf8stringencoding];//turn utf-8 and decodeDatainlength =[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;//Understanding bit type/typedef abbreviation (Effective maintenance code ratio: defined with a typedef with a long with int)size_t dataoutavailable =0;//size_t operator sizeof return knot typesize_t dataoutmoved =0; Dataoutavailable= (Datainlength + kccblocksizedes) & ~ (Kccblocksizedes-1); Dataout= malloc (dataoutavailable *sizeof(uint8_t)); memset ((void*) Dataout,xx, dataoutavailable);//memory space buffer first 1 byte value set value 0//nsstring *initiv = @ "12345678"; Const void*vkey =key; Const void*iv = (Const void*) key;//[Initiv utf8string]; //cccrypt function encryption/decryptionCcstatus = Cccrypt (Encryptoperation,//Encryption/DecryptionKccalgorithmdes,//encryption based on which criteria (DES3DESAES)Kccoptionpkcs7padding,//option group cipher (des: 3DES per block: Each group plus three)Vkey,//Key encryption The decryption key must beKcckeysizedes,//DES Key (kcckeysizedes=8)iv//Select the initial vectorDataIn,//Data storage UnitDatainlength,//Data(void*) Dataout,//for returning datadataoutavailable,&dataoutmoved); NSString*result =Nil; if(encryptoperation = = Kccdecrypt)//encryptoperation==1 decoding { //Decrypt Data Change Utf-8 stringresult = [[NSString alloc] initwithdata:[nsdata datawithbytes: (Const void*) dataout Length: (Nsuinteger) dataoutmoved] encoding:nsutf8stringencoding]; } Else //encryptoperation==0 (Encrypted data transfer to base64) { //Code base64NSData *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];}
Android Code
//Encrypt Public Staticstring decryptdonet (String message, string key)throwsException {byte[] Bytesrc =Base64.decode (Message.getbytes (), base64.default); Cipher Cipher= Cipher.getinstance ("des/cbc/pkcs5padding"); Deskeyspec Deskeyspec=NewDeskeyspec (Key.getbytes ("UTF-8")); Secretkeyfactory keyfactory= Secretkeyfactory.getinstance ("DES"); Secretkey Secretkey=Keyfactory.generatesecret (DESKEYSPEC); Ivparameterspec IV=NewIvparameterspec (Key.getbytes ("UTF-8")); Cipher.init (Cipher.decrypt_mode, Secretkey, iv); byte[] Retbyte =cipher.dofinal (BYTESRC); return NewString (Retbyte); } //decryption Public Staticstring encryptasdonet (String message, string key)throwsException {Cipher Cipher= Cipher.getinstance ("des/cbc/pkcs5padding"); Deskeyspec Deskeyspec=NewDeskeyspec (Key.getbytes ("UTF-8")); Secretkeyfactory keyfactory= Secretkeyfactory.getinstance ("DES"); Secretkey Secretkey=Keyfactory.generatesecret (DESKEYSPEC); Ivparameterspec IV=NewIvparameterspec (Key.getbytes ("UTF-8")); Cipher.init (Cipher.encrypt_mode, Secretkey, iv); byte[] Encryptbyte =cipher.dofinal (Message.getbytes ()); return NewString (Base64.encode (Encryptbyte, Base64.default)); }
C#/ios/android Universal encryption and decryption method