C#des encryption algorithm Specifies that the size of the key is not valid for this algorithm

Source: Internet
Author: User
Tags decrypt pkcs7

API interface calls, you need to encrypt the communication with Java, the communication process with DES encryption, Java Yonder des Key is a 64-bit string, and the previous C # des encryption is a key 8-bit

The key in the DESCryptoServiceProvider is 8 bits;
The key in the RijndaelManaged is 32 bits.

The des/cbc/pkcs5padding in Java corresponds to des/cbc/in C #PKCS7

corresponding Javades encryption function: Because of the network transmission, the + number is all converted to%2B

    Private Static FinalString KEY = "xxxje234d";//64 of Strings     Public StaticString encryptstring (String plaintext)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.substring (0, 8). GetBytes ("UTF-8"));        Cipher.init (Cipher.encrypt_mode, Secretkey, iv); byte[] bt = Cipher.dofinal (Plaintext.getbytes ("UTF-8"))); String STRs=NewBase64encoder (). Encode (BT). ReplaceAll ("[+]", "%2b"); returnSTRs; }

C # corresponding des cryptographic functions:

     Public Static stringToEncrypt2 (stringStrstringMyKey) {            stringEncryptkeyall = convert.tostring (MyKey);//Defining Keys            if(Encryptkeyall.length <9)            {                 for (; ; ) {                    if(Encryptkeyall.length <9) Encryptkeyall+=Encryptkeyall; Else                         Break; }            }            stringEncryptkey = encryptkeyall.substring (0,8); DESCryptoServiceProvider DESCSP=NewDESCryptoServiceProvider ();//instantiating an Add/Decrypt class objectDESCSP. Mode =CIPHERMODE.CBC; DESCSP. Padding=PADDINGMODE.PKCS7; byte[] key = Encoding.UTF8.GetBytes (Encryptkey);//defines a byte array used to store the key            byte[] data = Encoding.UTF8.GetBytes (str);//defines a byte array that is used to store the string to encryptMemoryStream Mstream =NewMemoryStream ();//instantiating a memory stream object//instantiating an encrypted stream object using a memory streamCryptoStream cstream =NewCryptoStream (Mstream, DESCSP.            CreateEncryptor (key, key), cryptostreammode.write); Cstream.write (data,0, data. Length);//writing data to an encrypted streamCstream.flushfinalblock ();//releasing the encrypted stream            returnConvert.tobase64string (Mstream.toarray ()). Replace ("+","%2b");//returns the encrypted string}

In C #, a key is 8-bit:

   Public Static stringEncrypt (stringPtoencrypt,stringSKey) {            using(DESCryptoServiceProvider des =NewDESCryptoServiceProvider ()) {                byte[] Inputbytearray =Encoding.UTF8.GetBytes (Ptoencrypt); 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. CreateEncryptor (), cryptostreammode.write)) {cs. Write (Inputbytearray,0, inputbytearray.length); Cs.                    FlushFinalBlock (); Cs.                Close (); }                stringstr =convert.tobase64string (Ms.                ToArray ()); Ms.                Close (); returnstr; }        }//decryption    /// <summary>        ///for des 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 Static stringDecrypt (stringPtodecrypt,stringSKey) {            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; }        }

C#des Encryption algorithm specifies that the size of the key is not valid for this algorithm

Related Article

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.