Java进行3DES加密解密(CBC模式,零字节填充)【与C# CBC 零填充模式 互相加解密】

Java代码如下:
import java.security.Security;
import java.util.Arrays;

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

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Hex;

/***
 * 可以和c#加密结果相同     需要到两个jar包:
 * bcprov-jdk15on-148.jar
 * bcprov-ext-jdk15on-148.jar   在D:/jar/
 * 
 * @ClassName Test3DES_2
 * @author 张月
 * @date 2014年4月16日
 */
public class Test3DES_2 {
	
	public static void main(String[] args) throws Exception {
		String key = "30313233343536373938373635343332";
		String shuju = "0088757365722020202020202020202020202020202020202020202020202013111816000001123456bc445951f5f60762f951146ece3bed0fbde2c4198f7dc43018f1d5f2c807c47f26FFD30AAF8E3823";
		String jiamiResult = encryptStr(shuju,key);
		System.out.println("加密后:"+jiamiResult);//【与C# CBC 零填充模式 互相加解密】
		
		System.out.println("解密后:"+decryptStr(jiamiResult,key));
	}
	
	/**
	 * 3DES加密 (亦称为:DESede加密)  
	 * 
	 * CBC模式
	 * 填充模式:零字节填充  ZeroBytePadding
	 * 
	 * @Method: encrypt3Str 
	 * @param @param shuju
	 * @param @param key
	 * @param @return
	 * @param @throws Exception
	 * @return String
	 * @throws
	 */
	public static String encryptStr(String shuju,String key) throws Exception {
		String result = "";
		try {
			Security.addProvider(new BouncyCastleProvider());
			byte[] bKey = Hex.decode(key);//十六进制转换成字节数据
			byte[] bMsg = Hex.decode(shuju);

			byte[] keyBytes = Arrays.copyOf(bKey, 24);
			int j = 0, k = 16;
			while (j < 8) {
			    keyBytes[k++] = keyBytes[j++];
			}

			SecretKey key3 = new SecretKeySpec(keyBytes, "DESede");
			IvParameterSpec iv3 = new IvParameterSpec(new byte[8]);//初始向量默认 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
			Cipher cipher3 = Cipher.getInstance("DESede/CBC/ZeroBytePadding");
			cipher3.init(Cipher.ENCRYPT_MODE, key3, iv3);

			byte[] bMac = cipher3.doFinal(bMsg);
			result=  new String(Hex.encode(bMac));//encode方法字节数组转换成十六进制
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		return result;
	}
	
	/**
	 * 3DES 解密
	 * @Method: decryptStr 
	 * @param @param shuju
	 * @param @param key
	 * @param @return
	 * @param @throws Exception
	 * @return String
	 * @throws
	 */
	public static String decryptStr(String shuju,String key) throws Exception{
		String result = "";
		try {
			Security.addProvider(new BouncyCastleProvider());
			byte[] bKey = Hex.decode(key);//十六进制转换成字节数据
			byte[] bMsg = Hex.decode(shuju);

			byte[] keyBytes = Arrays.copyOf(bKey, 24);
			int j = 0, k = 16;
			while (j < 8) {
			    keyBytes[k++] = keyBytes[j++];
			}

			SecretKey key3 = new SecretKeySpec(keyBytes, "DESede");
			IvParameterSpec iv3 = new IvParameterSpec(new byte[8]);//初始向量默认 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
			Cipher cipher3 = Cipher.getInstance("DESede/CBC/ZeroBytePadding");
			cipher3.init(Cipher.DECRYPT_MODE, key3, iv3);

			byte[] bMac = cipher3.doFinal(bMsg);
			result=  new String(Hex.encode(bMac));//encode方法字节数组转换成十六进制
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		return result;
	} 
}

C#代码如下:

      
//3DES   CBC模式   PaddingMode.Zeros;  零字节填充

  private string getDes3EncryptedText(string key, string IV, string sourceText)
        {
            var des3 = new TripleDESCryptoServiceProvider();

            des3.IV = HexStringToByteArray(IV);
            des3.Key = HexStringToByteArray(key);
            des3.Mode = CipherMode.CBC;
            des3.Padding = PaddingMode.Zeros;

            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des3.CreateEncryptor(), CryptoStreamMode.Write);

            var data = HexStringToByteArray(sourceText);
            cs.Write(data, 0, data.Length);
            cs.FlushFinalBlock();

            var result = ms.ToArray();

            cs.Close();
            ms.Close();

            return ByteArrayToHexString(result);
        }

        public static string ByteArrayToHexString(byte[] ba)
        {
            string hex = BitConverter.ToString(ba);
            return hex.Replace("-", "");
        }

        private byte[] HexStringToByteArray(string s)
        {
            s = s.Replace(" ", "").Trim();
            byte[] buffer = new byte[s.Length / 2];
            for (int i = 0; i < s.Length; i += 2)
                buffer[i / 2] = (byte)Convert.ToByte(s.Substring(i, 2), 16);
            return buffer;
        }





 

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页