DES加密 java与.net可以相互加密解密的方法

我.net/Java程序员。今天让C#和Java数据加密交互。通过这个方法可以实现java和C#相互加密与解密 并能保持解密出来一致。

话不多说,看代码

C#代码如下

package web_test;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;


public class AESUtils {

    
    private byte[] desKey;

    //解密数据
    public static String decrypt(String message, String key) throws Exception {
        byte[] bytesrc = convertHexString(message);
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
        IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));
        cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
        byte[] retByte = cipher.doFinal(bytesrc);
        return new String(retByte);
    }

    public static byte[] encrypt(String message, String key)
            throws Exception {
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
        IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
        return cipher.doFinal(message.getBytes("UTF-8"));
    }

    public static byte[] convertHexString(String ss) {
        byte digest[] = new byte[ss.length() / 2];
        for (int i = 0; i < digest.length; i++) {
            String byteString = ss.substring(2 * i, 2 * i + 2);
            int byteValue = Integer.parseInt(byteString, 16);
            digest[i] = (byte) byteValue;
        }
        return digest;
    }

    public static String toHexString(byte b[]) {
        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < b.length; i++) {
            String plainText = Integer.toHexString(0xff & b[i]);
            if (plainText.length() < 2)
                plainText = "0" + plainText;
            hexString.append(plainText);
        }
        return hexString.toString();
    }

}

 static void Main(string[] args)
        {
            string DES_Key = "12345678";
            Console.WriteLine("----------------------------------------------------------------------");

            localhost.testService ts = new localhost.testService();
            //接收java WebService服务loadname方法返回的值
            int c = ts.loadname(456);//ts.loadname(456)代表一个int类型的值,你可以写成 int c=123456;等等
            //接收java WebService服务loadname2方法返回的值
            string d = ts.loadname2();
           //处理java传过来的密文
            string jiamijava = AESHelper.DESEnCodeJava(d, DES_Key);

           //解密java处理的密文
            string jiemi = AESHelper.DESDeCode(jiamijava, DES_Key);

           //打印解密后的密文
            Console.WriteLine(jiemi);

            //加密C#数据
            string jiami = AESHelper.DESEnCode("我太难了,反倒是减肥广东省", DES_Key);
            //接收java WebService服务loadname4方法返回的值
            string cd = ts.loadname4(jiami);
            //处理java传过来的密文
            jiamijava = AESHelper.DESEnCodeJava(cd, DES_Key);

           //解密java处理的密文
            jiemi = AESHelper.DESDeCode(jiamijava, DES_Key);

            //打印解密后的密文
            Console.WriteLine(jiemi);
            //接收java WebService服务datime方法返回的值
            string c1 = ts.datime(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
            Console.WriteLine(ts.dble(623.23687));
            Console.WriteLine(c);
            //Console.WriteLine(d);
            Console.WriteLine(c1);
            Console.WriteLine(c + "===" + jiemi);

            //加密C#数据
            jiami = AESHelper.DESEnCode("", DES_Key);
            //接收java WebService服务str方法返回的值
            string str = ts.str(jiami);

            //处理java传过来的密文
            jiamijava = AESHelper.DESEnCodeJava(str, DES_Key);

            //解密java处理掉密文
            jiemi = AESHelper.DESDeCode(jiamijava, DES_Key);
            Console.WriteLine("================================================");
            Console.WriteLine(jiemi);
            Console.WriteLine("================================================");
            Console.Read();
        }

 

Java代码如下

package web_test;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;


public class AESUtils {


    private byte[] desKey;

    //解密数据
    public static String decrypt(String message, String key) throws Exception {
        byte[] bytesrc = convertHexString(message);
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
        IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));
        cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
        byte[] retByte = cipher.doFinal(bytesrc);
        return new String(retByte);
    }

    public static byte[] encrypt(String message, String key)
            throws Exception {
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
        IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
        return cipher.doFinal(message.getBytes("UTF-8"));
    }

    public static byte[] convertHexString(String ss) {
        byte digest[] = new byte[ss.length() / 2];
        for (int i = 0; i < digest.length; i++) {
            String byteString = ss.substring(2 * i, 2 * i + 2);
            int byteValue = Integer.parseInt(byteString, 16);
            digest[i] = (byte) byteValue;
        }
        return digest;
    }

    public static String toHexString(byte b[]) {
        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < b.length; i++) {
            String plainText = Integer.toHexString(0xff & b[i]);
            if (plainText.length() < 2)
                plainText = "0" + plainText;
            hexString.append(plainText);
        }
        return hexString.toString();
    }

}

 

package web_test;

import org.apache.axis2.databinding.types.xsd.DateTime;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.xml.ws.BindingType;
import javax.xml.ws.soap.SOAPBinding;
import java.io.*;
import java.math.BigInteger;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.RSAPublicKeySpec;
import java.util.Scanner;

import static web_test.AESUtils.decrypt;
import static web_test.AESUtils.encrypt;
import static web_test.AESUtils.toHexString;


//@BindingType(SOAPBinding.SOAP12HTTP_BINDING)
//@BindingType(SOAPBinding.SOAP11HTTP_BINDING)
//public class test implements java.io.Serializable {

public class test {

    //String key = "kkkkkkk123456789";//必须保持16位//
    String key = "12345678";


    public String loadname2() {
        String strBase64 = "";
        try {
            //加密
            //String value = "A:正月里是新年哪,呀子一子哟,正月里是新年,一嗨哟";
            String value = "{\"code\":0,\"msg\":null,\"count\":50,\"data\":[{\"ClearDate\":\"2019-02-13\",\"ClearInfo\":\"没有需要清除的数据 . 2019-02-13\",\"ClearUser\":\"系统自动\",\"RecordId\":206},{\"ClearDate\":\"2019-02-12\",\"ClearInfo\":\"共清除的数据 103 条,敏感信息全部赋值为 000000000. 2019-02-12\",\"ClearUser\":\"系统自动\",\"RecordId\":205},{\"ClearDate\":\"2019-02-11\",\"ClearInfo\":\"没有需要清除的数据 . 2019-02-11\",\"ClearUser\":\"系统自动\",\"RecordId\":204},{\"ClearDate\":\"2019-02-10\",\"ClearInfo\":\"共清除的数据 146 条,敏感信息全部赋值为 000000000. 2019-02-10\",\"ClearUser\":\"系统自动\",\"RecordId\":203},{\"ClearDate\":\"2019-02-09\",\"ClearInfo\":\"没有需要清除的数据 . 2019-02-09\",\"ClearUser\":\"系统自动\",\"RecordId\":202},{\"ClearDate\":\"2019-02-08\",\"ClearInfo\":\"没有需要清除的数据 . 2019-02-08\",\"ClearUser\":\"系统自动\",\"RecordId\":201},{\"ClearDate\":\"2019-02-07\",\"ClearInfo\":\"共清除的数据 53 条,敏感信息全部赋值为 000000000. 2019-02-07\",\"ClearUser\":\"系统自动\",\"RecordId\":200},{\"ClearDate\":\"2019-02-06\",\"ClearInfo\":\"共清除的数据 54 条,敏感信息全部赋值为 000000000. 2019-02-06\",\"ClearUser\":\"系统自动\",\"RecordId\":199},{\"ClearDate\":\"2019-02-05\",\"ClearInfo\":\"共清除的数据 50 条,敏感信息全部赋值为 000000000. 2019-02-05\",\"ClearUser\":\"系统自动\",\"RecordId\":198},{\"ClearDate\":\"2019-02-04\",\"ClearInfo\":\"共清除的数据 55 条,敏感信息全部赋值为 000000000. 2019-02-04\",\"ClearUser\":\"系统自动\",\"RecordId\":197},{\"ClearDate\":\"2019-02-03\",\"ClearInfo\":\"没有需要清除的数据 . 2019-02-03\",\"ClearUser\":\"系统自动\",\"RecordId\":196},{\"ClearDate\":\"2019-02-02\",\"ClearInfo\":\"共清除的数据 39 条,敏感信息全部赋值为 000000000. 2019-02-02\",\"ClearUser\":\"系统自动\",\"RecordId\":195},{\"ClearDate\":\"2019-02-01\",\"ClearInfo\":\"没有需要清除的数据 . 2019-02-01\",\"ClearUser\":\"系统自动\",\"RecordId\":194},{\"ClearDate\":\"2019-01-31\",\"ClearInfo\":\"没有需要清除的数据 . 2019-01-31\",\"ClearUser\":\"系统自动\",\"RecordId\":193},{\"ClearDate\":\"2019-01-30\",\"ClearInfo\":\"共清除的数据 52 条,敏感信息全部赋值为 000000000. 2019-01-30\",\"ClearUser\":\"系统自动\",\"RecordId\":192},{\"ClearDate\":\"2019-01-29\",\"ClearInfo\":\"共清除的数据 38 条,敏感信息全部赋值为 000000000. 2019-01-29\",\"ClearUser\":\"系统自动\",\"RecordId\":191},{\"ClearDate\":\"2019-01-28\",\"ClearInfo\":\"共清除的数据 67 条,敏感信息全部赋值为 000000000. 2019-01-28\",\"ClearUser\":\"系统自动\",\"RecordId\":190},{\"ClearDate\":\"2019-01-27\",\"ClearInfo\":\"共清除的数据 89 条,敏感信息全部赋值为 000000000. 2019-01-27\",\"ClearUser\":\"系统自动\",\"RecordId\":189},{\"ClearDate\":\"2019-01-26\",\"ClearInfo\":\"共清除的数据 131 条,敏感信息全部赋值为 000000000. 2019-01-26\",\"ClearUser\":\"系统自动\",\"RecordId\":188},{\"ClearDate\":\"2019-01-25\",\"ClearInfo\":\"没有需要清除的数据 . 2019-01-25\",\"ClearUser\":\"系统自动\",\"RecordId\":187},{\"ClearDate\":\"2019-01-24\",\"ClearInfo\":\"没有需要清除的数据 . 2019-01-24\",\"ClearUser\":\"系统自动\",\"RecordId\":186},{\"ClearDate\":\"2019-01-23\",\"ClearInfo\":\"共清除的数据 79 条,敏感信息全部赋值为 000000000. 2019-01-23\",\"ClearUser\":\"系统自动\",\"RecordId\":185},{\"ClearDate\":\"2019-01-22\",\"ClearInfo\":\"共清除的数据 84 条,敏感信息全部赋值为 000000000. 2019-01-22\",\"ClearUser\":\"系统自动\",\"RecordId\":184},{\"ClearDate\":\"2019-01-21\",\"ClearInfo\":\"共清除的数据 153 条,敏感信息全部赋值为 000000000. 2019-01-21\",\"ClearUser\":\"系统自动\",\"RecordId\":183},{\"ClearDate\":\"2019-01-20\",\"ClearInfo\":\"没有需要清除的数据 . 2019-01-20\",\"ClearUser\":\"系统自动\",\"RecordId\":182},{\"ClearDate\":\"2019-01-19\",\"ClearInfo\":\"没有需要清除的数据 . 2019-01-19\",\"ClearUser\":\"系统自动\",\"RecordId\":181},{\"ClearDate\":\"2019-01-18\",\"ClearInfo\":\"没有需要清除的数据 . 2019-01-18\",\"ClearUser\":\"系统自动\",\"RecordId\":180},{\"ClearDate\":\"2019-01-17\",\"ClearInfo\":\"没有需要清除的数据 . 2019-01-17\",\"ClearUser\":\"系统自动\",\"RecordId\":179},{\"ClearDate\":\"2019-01-16\",\"ClearInfo\":\"共清除的数据 84 条,敏感信息全部赋值为 000000000. 2019-01-16\",\"ClearUser\":\"系统自动\",\"RecordId\":178},{\"ClearDate\":\"2019-01-15\",\"ClearInfo\":\"共清除的数据 85 条,敏感信息全部赋值为 000000000. 2019-01-15\",\"ClearUser\":\"系统自动\",\"RecordId\":177},{\"ClearDate\":\"2019-01-14\",\"ClearInfo\":\"共清除的数据 70 条,敏感信息全部赋值为 000000000. 2019-01-14\",\"ClearUser\":\"系统自动\",\"RecordId\":176},{\"ClearDate\":\"2019-01-13\",\"ClearInfo\":\"共清除的数据 72 条,敏感信息全部赋值为 000000000. 2019-01-13\",\"ClearUser\":\"系统自动\",\"RecordId\":175},{\"ClearDate\":\"2019-01-12\",\"ClearInfo\":\"共清除的数据 111 条,敏感信息全部赋值为 000000000. 2019-01-12\",\"ClearUser\":\"系统自动\",\"RecordId\":174},{\"ClearDate\":\"2019-01-11\",\"ClearInfo\":\"没有需要清除的数据 . 2019-01-11\",\"ClearUser\":\"系统自动\",\"RecordId\":173},{\"ClearDate\":\"2019-01-10\",\"ClearInfo\":\"没有需要清除的数据 . 2019-01-10\",\"ClearUser\":\"系统自动\",\"RecordId\":172},{\"ClearDate\":\"2019-01-09\",\"ClearInfo\":\"共清除的数据 76 条,敏感信息全部赋值为 000000000. 2019-01-09\",\"ClearUser\":\"系统自动\",\"RecordId\":171},{\"ClearDate\":\"2019-01-08\",\"ClearInfo\":\"共清除的数据 69 条,敏感信息全部赋值为 000000000. 2019-01-08\",\"ClearUser\":\"系统自动\",\"RecordId\":170},{\"ClearDate\":\"2019-01-07\",\"ClearInfo\":\"共清除的数据 54 条,敏感信息全部赋值为 000000000. 2019-01-07\",\"ClearUser\":\"系统自动\",\"RecordId\":169},{\"ClearDate\":\"2019-01-06\",\"ClearInfo\":\"共清除的数据 45 条,敏感信息全部赋值为 000000000. 2019-01-06\",\"ClearUser\":\"系统自动\",\"RecordId\":168},{\"ClearDate\":\"2019-01-05\",\"ClearInfo\":\"共清除的数据 81 条,敏感信息全部赋值为 000000000. 2019-01-05\",\"ClearUser\":\"系统自动\",\"RecordId\":167},{\"ClearDate\":\"2019-01-04\",\"ClearInfo\":\"没有需要清除的数据 . 2019-01-04\",\"ClearUser\":\"系统自动\",\"RecordId\":166},{\"ClearDate\":\"2019-01-03\",\"ClearInfo\":\"没有需要清除的数据 . 2019-01-03\",\"ClearUser\":\"系统自动\",\"RecordId\":165},{\"ClearDate\":\"2019-01-02\",\"ClearInfo\":\"共清除的数据 29 条,敏感信息全部赋值为 000000000. 2019-01-02\",\"ClearUser\":\"系统自动\",\"RecordId\":164},{\"ClearDate\":\"2019-01-01\",\"ClearInfo\":\"共清除的数据 29 条,敏感信息全部赋值为 000000000. 2019-01-01\",\"ClearUser\":\"系统自动\",\"RecordId\":163},{\"ClearDate\":\"2018-12-31\",\"ClearInfo\":\"共清除的数据 66 条,敏感信息全部赋值为 000000000. 2018-12-31\",\"ClearUser\":\"系统自动\",\"RecordId\":162},{\"ClearDate\":\"2018-12-30\",\"ClearInfo\":\"共清除的数据 59 条,敏感信息全部赋值为 000000000. 2018-12-30\",\"ClearUser\":\"系统自动\",\"RecordId\":161},{\"ClearDate\":\"2018-12-29\",\"ClearInfo\":\"共清除的数据 75 条,敏感信息全部赋值为 000000000. 2018-12-29\",\"ClearUser\":\"系统自动\",\"RecordId\":160},{\"ClearDate\":\"2018-12-28\",\"ClearInfo\":\"没有需要清除的数据 . 2018-12-28\",\"ClearUser\":\"系统自动\",\"RecordId\":159},{\"ClearDate\":\"2018-12-27\",\"ClearInfo\":\"没有需要清除的数据 . 2018-12-27\",\"ClearUser\":\"系统自动\",\"RecordId\":158},{\"ClearDate\":\"2018-12-26\",\"ClearInfo\":\"共清除的数据 68 条,敏感信息全部赋值为 000000000. 2018-12-26\",\"ClearUser\":\"系统自动\",\"RecordId\":157}]}";
            strBase64 = java.net.URLEncoder.encode(value, "utf-8").toLowerCase();
            //打印加密后的字符串
            System.out.println(strBase64);
        } catch (Exception ex) {
            System.out.println(ex.toString());
        }
        //返回加密后的字符串
        return strBase64;
    }

    public int loadname(int str) {
        return str;
    }

    public String loadname4(String str) {
        String strBase64 = "";
        String jiami = "";
        try {
            //解密传过来的字符密文str
            String b = java.net.URLDecoder.decode(decrypt(str, key), "utf-8");

            //拼接解密后的字符串
            strBase64 = "当前键入的字符串:" + b;
            //加密拼接后的字符串
            jiami = java.net.URLEncoder.encode(strBase64, "utf-8").toLowerCase();
            //打印加密后的字符串
            System.out.println(strBase64);

        } catch (Exception ex) {
            System.out.println(ex.toString());
        }
        return jiami;
    }

    public String datime(String dtime) {
        return "当前时间:" + dtime;
    }

    public double dble(double d) {
        return d;
    }

    public String str(String value) throws Exception {
        //解密传过来的字符密文
        String b = java.net.URLDecoder.decode(decrypt(value, key), "utf-8");

        //加密解密后的字符串
        String jiami = java.net.URLEncoder.encode(b, "utf-8").toLowerCase();
        return jiami;
    }
}

 

也可参照另外一个做些写的:https://www.cnblogs.com/sexintercourse/p/6063387.html

你可能感兴趣的:(DES加密 java与.net可以相互加密解密的方法)