Description of the encryption method:
1. according to the Convention secret key, the Des encryption algorithm is used to encrypt the generated byte array;
2. Encode the byte array generated after encryption using the BASE64 algorithm;
Description of the decryption method:
1. using the BASE64 algorithm to decode ciphertext to generate a byte array;
2. According to the agreed secret key, the resulting byte array is used Des decryption;
Java code:
PackageCom.ab.mediation.util;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.IOException;ImportJava.io.InputStreamReader;ImportJava.security.SecureRandom;ImportJavax.crypto.Cipher;ImportJavax.crypto.SecretKey;Importjavax.crypto.SecretKeyFactory;ImportJavax.crypto.spec.DESKeySpec;ImportSun.misc.BASE64Decoder;ImportSun.misc.BASE64Encoder;/*** External interface Data encryption/decryption class *@authorXin **/ Public classDesutil {Private Final StaticString des = "des"; Public Static voidMain (string[] args)throwsException {String Tdoc= "";//Request MessageString Encoding= "GBK";//Assigning a function parameter to a local parameterString Path= "D:\\111111.xml";//String Path = "F:\\111111\\111111.xml";String path1=path;//Initialize file object FFile F=NewFile (path1);//Initialize read data stream object ReaderInputStreamReader Reader=NewInputStreamReader (NewFileInputStream (path1), encoding);//Initialize the string data according to the length of the F file c[]CharC[] =New Char[(int) (F.length ())];//takes the string length and writes the file F contents to the array CintLength =Reader.read (c);//byte array c[], assigned to the variable Tdoc for(inti = 0; i < length; i++) {Tdoc= Tdoc +c[i];}//System.out.println (tdoc);String Key= "111111111"; System.out.println (Encrypt (Tdoc, key)); System.out.println (Decrypt (Encrypt (Tdoc, key), key)); } /*** Description is encrypted based on key value * *@paramData *@paramKey * Encryption key byte array *@return * @throwsException*/ Public StaticString Encrypt (string data, string key)throwsException {byte[] bt =Encrypt (Data.getbytes (), key.getbytes ()); String STRs=NewBase64encoder (). Encode (BT);returnSTRs;} /*** Specify character encoding and encrypt *@paramData *@paramKey *@paramencoding *@return * @throwsException*/ Public StaticString Encrypt (string data, string key, String encoding)throwsException {byte[] bt =Encrypt (data.getbytes (encoding), key.getbytes ()); String STRs=NewBase64encoder (). Encode (BT);returnSTRs;} /*** Description is decrypted based on key value * *@paramData *@paramKey * Encryption key byte array *@return * @throwsIOException *@throwsException*/ Public StaticString decrypt (string data, string key)throwsioexception,exception {if(Data = =NULL)return NULL; Base64decoder Decoder=NewBase64decoder ();byte[] buf =decoder.decodebuffer (data);byte[] bt =Decrypt (buf, key.getbytes ());return NewString (BT);} /*** Decrypt and return the specified encoding string according to the key value *@paramData *@paramKey *@paramencoding *@return * @throwsIOException *@throwsException*/ Public StaticString decrypt (string data, string key, String encoding)throwsioexception,exception {if(Data = =NULL)return NULL; Base64decoder Decoder=NewBase64decoder ();byte[] buf =decoder.decodebuffer (data);byte[] bt =Decrypt (buf, key.getbytes ());return NewString (BT, encoding);} /*** Description is encrypted based on key value * *@paramData *@paramKey * Encryption key byte array *@return * @throwsException*/Private Static byte[] Encrypt (byte[] Data,byte[] key)throwsException {//generate a trustworthy random number sourceSecureRandom SR=Newsecurerandom ();//Create a Deskeyspec object from the original key dataDeskeyspec DKs=NewDeskeyspec (key);//Create a key factory and use it to convert Deskeyspec to Secretkey objectssecretkeyfactory keyfactory=secretkeyfactory.getinstance (DES); Secretkey SecureKey=Keyfactory.generatesecret (DKS);//The cipher object actually completes the cryptographic operationCipher Cipher=cipher.getinstance (DES);//initializing a Cipher object with a keyCipher.init (Cipher.encrypt_mode, SecureKey, SR);returncipher.dofinal (data);} /*** Description is decrypted based on key value * *@paramData *@paramKey * Encryption key byte array *@return * @throwsException*/Private Static byte[] Decrypt (byte[] Data,byte[] key)throwsException {//generate a trustworthy random number sourceSecureRandom SR=Newsecurerandom ();//Create a Deskeyspec object from the original key dataDeskeyspec DKs=NewDeskeyspec (key);//Create a key factory and use it to convert Deskeyspec to Secretkey objectssecretkeyfactory keyfactory=secretkeyfactory.getinstance (DES); Secretkey SecureKey=Keyfactory.generatesecret (DKS);//The cipher object actually completes the decryption operationCipher Cipher=cipher.getinstance (DES);//initializing a Cipher object with a keyCipher.init (Cipher.decrypt_mode, SecureKey, SR);returncipher.dofinal (data);}}
The corresponding. NET code:
Public Static stringDesdecrypt (stringStrstringkey) {DESCryptoServiceProvider DES=NewDESCryptoServiceProvider (); Key= key. Substring (0,8);//intercept top eight bitsDes. Key =ASCIIEncoding.Default.GetBytes (key); Des. Mode=CIPHERMODE.ECB; Des. Padding=System.Security.Cryptography.PaddingMode.PKCS7; ICryptoTransform Desdecrypt=DES. CreateDecryptor (); stringresult =""; Try { byte[] Buffer =convert.frombase64string (str); Result= Encoding.Default.GetString (Desdecrypt.transformfinalblock (Buffer,0, buffer.length)); } Catch(Cryptographicexception e) {Console.WriteLine ("error: {0}", E.message); return NULL; } returnresult; } Public Static stringDesencrypt (stringStrstringkey) { stringres =""; Key= key. Substring (0,8);//intercept top eight bits Try{DESCryptoServiceProvider DES=NewDESCryptoServiceProvider (); Des. Key=Encoding.UTF8.GetBytes (key); Des. Mode=CIPHERMODE.ECB; Des. Padding=PADDINGMODE.PKCS7; ICryptoTransform desencrypt=DES. CreateEncryptor (); byte[] Buffer = encoding.getencoding ("GBK"). GetBytes (str); Res= Convert.tobase64string (Desencrypt.transformfinalblock (Buffer,0, buffer.length)); } Catch(Cryptographicexception e) {Console.WriteLine ("error: {0}", E.message); return NULL; } returnRes; }
Des plus decryption (Java and. net)