"Plus decrypt" Java encryption code and C # decryption code for DES encryption algorithm

Source: Internet
Author: User
Tags decrypt

Java encryption:

Package Webdomain;import Java.security.Key;      Import Java.security.spec.AlgorithmParameterSpec; Import Javax.crypto.cipher;import javax.crypto.SecretKeyFactory; Import Javax.crypto.spec.deskeyspec;import javax.crypto.spec.ivparameterspec;import Decoder.BASE64Encoder; Public classCrytotools {Private StaticFinalbyte[] Deskey ="here 8-bit key--a". GetBytes ();//    Private StaticFinalbyte[] Desiv ="here is another 8-bit key--b". GetBytes ();//    StaticAlgorithmparameterspec IV =NULL; Private StaticKey key =NULL;  PublicCrytotools () throws Exception {Deskeyspec KeySpec=NewDeskeyspec (Deskey); IV=NewIvparameterspec (DESIV); Secretkeyfactory keyfactory= Secretkeyfactory.getinstance ("DES"); Key=Keyfactory.generatesecret (KEYSPEC); }     Publicstring Encode (string data) throws exception{Cipher Encipher= Cipher.getinstance ("des/cbc/pkcs5padding");        Encipher.init (Cipher.encrypt_mode, KEY,IV); byte[] Pasbyte = Encipher.dofinal (Data.getbytes ("Utf-8")); Base64encoder Base64encoder=NewBase64encoder (); returnBase64encoder.encode (Pasbyte); }}

C # decryption

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingsystem.web;usingSystem.Text;usingSystem.Configuration;usingSystem.Security.Cryptography;usingSystem.IO;namespaceappservice{ Public classDES {//des -in-the-sign         Public Static stringDesdecode (stringEncryptedstring,stringSKey) {            Try            {                byte[] BtKey = Encoding.Default.GetBytes ("here 8-bit key--a");//キー                byte[] Btiv = Encoding.Default.GetBytes ("here another 8-bit key--b");//キーDESCryptoServiceProvider des =NewDESCryptoServiceProvider (); using(MemoryStream ms =NewMemoryStream ()) {                    byte[] InData =convert.frombase64string (encryptedstring); Try                    {                        using(CryptoStream cs =NewCryptoStream (MS, Des. CreateDecryptor (BtKey, Btiv), CryptoStreamMode.Write)) {cs. Write (InData,0, indata.length); Cs.                        FlushFinalBlock (); }                        returnEncoding.UTF8.GetString (Ms.                    ToArray ()); }                    Catch                    {                        returnencryptedstring; }                }            }            Catch {                returnencryptedstring; }        }    }}

"Plus decrypt" Java encryption code and C # decryption code for DES encryption algorithm

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.