3DES plus decryption, no limit on length

Source: Internet
Author: User

#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

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.