#region3DES/// <summary> ///3DES Encryption/// </summary> /// <param name= "strstring" >strings to be encrypted</param> /// <param name= "Strkey" >secret key</param> /// <returns></returns> Public Static stringDes3encrypt (stringStrstring,stringstrkey) {strstring= strstring +"". PadLeft (8); byte[] bmsg = encoding.getencoding ("GBK"). GetBytes (strstring); intL = (Bmsg.length/ -+1) * -; byte[] btmsg =New byte[L]; Array.copy (Bmsg, btmsg, bmsg.length); byte[] Digestofpassword =Encoding.Default.GetBytes (strkey); byte[] Keybytes =New byte[ -]; Array.copy (Digestofpassword, Keybytes, digestofpassword.length); for(intj =0, k = -; J <8; ) {Keybytes[k+ +] = keybytes[j++]; } TripleDESCryptoServiceProvider DES=NewTripleDESCryptoServiceProvider (); Des. Mode=CIPHERMODE.ECB; ICryptoTransform desencrypt=DES. CreateEncryptor (Keybytes, keybytes); varDD = Desencrypt.transformfinalblock (btmsg,0, btmsg.length); returnByte2hex (DD). ToString (). Substring (0, (Bmsg.length/8+1) * -); } Public StaticString Getadd (intlength, String strkey) { byte[] btmsg =New byte[Length/2]; byte[] Digestofpassword =Encoding.UTF8.GetBytes (strkey); //byte[] Digestofpassword = Encoding.Default.GetBytes (strkey); byte[] Keybytes =New byte[ -]; Array.copy (Digestofpassword, Keybytes, digestofpassword.length); for(intj =0, k = -; J <8; ) {Keybytes[k+ +] = keybytes[j++]; } TripleDESCryptoServiceProvider DES=NewTripleDESCryptoServiceProvider (); Des. Mode=CIPHERMODE.ECB; ICryptoTransform desencrypt=DES. CreateEncryptor (Keybytes, keybytes); varDD = Desencrypt.transformfinalblock (btmsg,0, btmsg.length); String RTN=Byte2hex (DD); returnRtn. Substring (length); } /// <summary> ///byte array to 16 binary string/// </summary> /// <param name= "bytes" ></param> /// <returns></returns> Private Static stringByte2hex (byte[] dd) {StringBuilder ret=NewStringBuilder (); foreach(varIteminchdd) {ret. AppendFormat ("{0:x2}", item); } returnret. ToString (); } /// <summary> ///string to 16 binary byte array/// </summary> /// <param name= "hexstring" ></param> /// <returns></returns> Private Static byte[] Strtotohexbyte (stringhexstring) {hexstring= Hexstring.replace (" ",""); if((hexstring.length%2) !=0) hexstring+=" "; byte[] Returnbytes =New byte[Hexstring.length/2]; for(inti =0; i < returnbytes.length; i++) Returnbytes[i]= Convert.tobyte (hexstring.substring (i *2,2), -); returnreturnbytes; } /// <summary> ///3DES Decryption/// </summary> /// <param name= "strstring" >the string to be decrypted</param> /// <param name= "Strkey" >secret key</param> /// <returns></returns> Public Static stringDes3decrypt (stringStrstring,stringstrkey) {strstring+=Getadd (Strstring.length, strkey); byte[] Digestofpassword =Encoding.Default.GetBytes (strkey); byte[] Keybytes =New byte[ -]; Array.copy (Digestofpassword, Keybytes, digestofpassword.length); for(intj =0, k = -; J <8; ) {Keybytes[k+ +] = keybytes[j++]; } TripleDESCryptoServiceProvider DES=NewTripleDESCryptoServiceProvider (); Des. Mode=CIPHERMODE.ECB; ICryptoTransform Desdecrypt=DES. CreateDecryptor (Keybytes, keybytes); stringresult =""; Try { byte[] Buffer =Strtotohexbyte (strstring); byte[] bb = Desdecrypt.transformfinalblock (Buffer,0, buffer.length); Result= Encoding.GetEncoding ("GBK"). GetString (BB); } Catch(Exception e) {Throwe; } returnresult; } #endregion
3DES plus decryption, no limit on length