Transfer from http://www.cnblogs.com/zhuiyi/archive/2013/04/01/2993201.html
Recently by Des plus decryption made super depressed, I use C # method to encrypt the ciphertext is always with the client to the Java encryption ciphertext different, find a lot of information has not been solved. I didn't solve the problem until I saw the above blog post. Mark here to avoid forgetting!
Let's start with a C # version:
1 Public classDeshelper2 {3 4 /// <summary>5 ///des encryption Algorithm6 /// </summary>7 /// <param name= "encryptstring" >the string to encrypt</param>8 /// <param name= "SKey" >Add password Key</param>9 /// <returns>correctly returns the encrypted result, the error returns the source string</returns>Ten Public Static stringTodesencrypt (stringEncryptString,stringSKey) One { A Try - { - the byte[] Keybytes =Encoding.UTF8.GetBytes (SKey); - byte[] Keyiv =keybytes; - byte[] Inputbytearray =Encoding.UTF8.GetBytes (encryptstring); - +DESCryptoServiceProvider Desprovider =NewDESCryptoServiceProvider (); - + //Java default is the ECB mode, pkcs5padding;c# the default CBC mode, pkcs7padding so here we use the ECB way by default ADesprovider.mode =CIPHERMODE.ECB; atMemoryStream Memstream =NewMemoryStream (); -CryptoStream crypstream =NewCryptoStream (Memstream, Desprovider.createencryptor (Keybytes, Keyiv), cryptostreammode.write); - -Crypstream.write (Inputbytearray,0, inputbytearray.length); - Crypstream.flushfinalblock (); - returnconvert.tobase64string (Memstream.toarray ()); in - } to Catch + { - returnencryptstring; the } * } $ Panax Notoginseng - /// <summary> the ///des decryption Algorithm + /// </summary> A /// <param name= "decryptstring" >the string to decrypt</param> the /// <param name= "SKey" >Encrypt key</param> + /// <returns>correctly returns the encrypted result, the error returns the source string</returns> - Public Static stringTodesdecrypt (stringDecryptstring,stringSKey) $ { $ byte[] Keybytes =Encoding.UTF8.GetBytes (SKey); - byte[] Keyiv =keybytes; - byte[] Inputbytearray =convert.frombase64string (decryptstring); the -DESCryptoServiceProvider Desprovider =NewDESCryptoServiceProvider ();Wuyi the //Java default is the ECB mode, pkcs5padding;c# the default CBC mode, pkcs7padding so here we use the ECB way by default -Desprovider.mode =CIPHERMODE.ECB; WuMemoryStream Memstream =NewMemoryStream (); -CryptoStream crypstream =NewCryptoStream (Memstream, Desprovider.createdecryptor (Keybytes, Keyiv), cryptostreammode.write); About $Crypstream.write (Inputbytearray,0, inputbytearray.length); - Crypstream.flushfinalblock (); - returnEncoding.Default.GetString (Memstream.toarray ()); - A } +}
And a Java version.
1 Public classDeshelper {2 3 Private byte[] deskey;4 5 PublicDES (String deskey) {6 This. Deskey =deskey.getbytes ();7 }8 9 Public byte[] Desencrypt (byte[] plaintext)throwsException {TenSecureRandom sr =Newsecurerandom (); One byteRawkeydata[] =Deskey; ADeskeyspec DKs =NewDeskeyspec (rawkeydata); -Secretkeyfactory keyfactory = secretkeyfactory.getinstance ("DES"); -Secretkey key =Keyfactory.generatesecret (DKS); theCipher Cipher = cipher.getinstance ("DES"); - Cipher.init (Cipher.encrypt_mode, Key, SR); - byteData[] =plaintext; - byteEncrypteddata[] =cipher.dofinal (data); + returnEncryptedData; - } + A Public byte[] Desdecrypt (byte[] encrypttext)throwsException { atSecureRandom sr =Newsecurerandom (); - byteRawkeydata[] =Deskey; -Deskeyspec DKs =NewDeskeyspec (rawkeydata); -Secretkeyfactory keyfactory = secretkeyfactory.getinstance ("DES"); -Secretkey key =Keyfactory.generatesecret (DKS); -Cipher Cipher = cipher.getinstance ("DES"); in Cipher.init (Cipher.decrypt_mode, Key, SR); - byteEncrypteddata[] =Encrypttext; to byteDecrypteddata[] =cipher.dofinal (EncryptedData); + returnDecrypteddata; - } the * PublicString Encrypt (String input)throwsException { $ returnBase64Encode (Desencrypt (Input.getbytes ()));Panax Notoginseng } - the PublicString decrypt (String input)throwsException { + byte[] result =Base64decode (input); A return NewString (Desdecrypt (Result)); the } + - Public StaticString Base64Encode (byte[] s) { $ if(s = =NULL) $ return NULL; -Base64encoder B =NewSun.misc.BASE64Encoder (); - returnB.encode (s); the } - Wuyi Public Static byte[] Base64decode (String s)throwsIOException { the if(s = =NULL) - return NULL; WuBase64decoder decoder =NewBase64decoder (); - byte[] B =Decoder.decodebuffer (s); About returnb; $ } - -}
C # Java des encryption decryption