c#和java通用DES加解密

        /// 
        /// Des加密
        /// 
        /// 源字符串
        /// des密钥,长度必须8位
        /// 密钥向量
        /// 加密后的字符串
        internal static string EncryptDes(string source, byte[] key, byte[] iv)
        {
            using (DESCryptoServiceProvider desProvider = new DESCryptoServiceProvider())
            {
                byte[] inputByteArray = Encoding.UTF8.GetBytes(source);
                using (MemoryStream memoryStream = new MemoryStream())
                {
                    using (CryptoStream cryptoStream = new CryptoStream(memoryStream, desProvider.CreateEncryptor(key, iv), CryptoStreamMode.Write))
                    {
                        cryptoStream.Write(inputByteArray, 0, inputByteArray.Length);
                        cryptoStream.FlushFinalBlock();
                        return Convert.ToBase64String(memoryStream.ToArray());
                    }
                }
            }
        }

        /// 
        /// Des解密
        /// 
        /// 源字符串
        /// des密钥,长度必须8位
        /// 密钥向量
        /// 解密后的字符串
        internal static string DecryptDes(string source, byte[] key, byte[] iv)
        {
            using (DESCryptoServiceProvider desProvider = new DESCryptoServiceProvider())
            {
                byte[] inputByteArray = Convert.FromBase64String(source);
                using (MemoryStream memoryStream = new MemoryStream())
                {
                    using (CryptoStream cryptoStream = new CryptoStream(memoryStream, desProvider.CreateDecryptor(key, iv), CryptoStreamMode.Write))
                    {
                        cryptoStream.Write(inputByteArray, 0, inputByteArray.Length);
                        cryptoStream.FlushFinalBlock();
                        return Encoding.UTF8.GetString(memoryStream.ToArray());
                    }
                }
            }
        }

上面是C#的des加解密,下面是对应的Java代码

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

	//加密数据
	public static String encrypt(String message)throws Exception {
		Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
		DESKeySpec desKeySpec = new DESKeySpec(mDesKeykey);
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
		SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
		IvParameterSpec iv = new IvParameterSpec(mDesKeykey);
		cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
		return Base64.encode(cipher.doFinal(message.getBytes("UTF-8")));
	}

主要是上面的代码,总的来说也需要注意以下几点

1.加解密字符串对应的编码方式,包括C#和Java的对应

2.上面mDesKeykey表示你的key,外部自己传递修改,也保证C#和Java的对应

3.上面的加解密不是全十六进制的,全十六进制需要对字符串中的逐字符进行处理

4.本来Java的hutool插件,断点进去后,加解密后的十六进制字节数组是带符号的,而C#应该是不带的,导致调不通

还不清楚的话,可参考另一个链接

你可能感兴趣的:(JAVA与C#,java,c#)