c# java Encrypt 同步 加密 解密

本文介绍并对比了使用C#和Java两种语言实现DES加密和解密的具体方法。通过示例代码展示了如何设置密钥、初始化加密器、进行加密及解密操作,并将最终结果以Base64字符串形式输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

.net


        static void Main(string[] args)
        {

            string encryptKey = ".netkean";
            string plaintexta = EncryptDES("123456", encryptKey);
            string ddes = DecryptDES(plaintexta, encryptKey);
            //Console.WriteLine(plaintext);
            Console.WriteLine(plaintexta);
            Console.WriteLine(ddes);
            Console.ReadKey();     
        }

        public static string EncryptDES(string encryptString, string encryptKey)
        {
            DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
            byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey);
            byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
            MemoryStream mStream = new MemoryStream();
            CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, new byte[8]), CryptoStreamMode.Write);
            cStream.Write(inputByteArray, 0, inputByteArray.Length);
            cStream.FlushFinalBlock();

            return Convert.ToBase64String(mStream.ToArray());
        }

        public static string DecryptDES(string decryptString, string decryptKey)
        {
            byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
            byte[] inputByteArray = Convert.FromBase64String(decryptString);
            DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
            MemoryStream mStream = new MemoryStream();
            CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, new byte[8]), CryptoStreamMode.Write);
            cStream.Write(inputByteArray, 0, inputByteArray.Length);
            cStream.FlushFinalBlock();
            return Encoding.UTF8.GetString(mStream.ToArray());
        }


java

	public static void main(String[] args) {
		String encryptKey =".netkean";
		//加密
		try {
			String dec = encryptDES("123456",encryptKey);
			String ddec = decryptDES(dec,encryptKey);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static String encryptDES(String encryptString, String encryptKey) throws Exception {
		IvParameterSpec zeroIv = new IvParameterSpec(new byte[8]);
		SecretKeySpec key = new SecretKeySpec(encryptKey.getBytes(), "DES");
		Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
		cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
		byte[] encryptedData = cipher.doFinal(encryptString.getBytes());
	 
		return new BASE64Encoder().encode(encryptedData);
	}
	public static String decryptDES(String decryptString, String decryptKey) throws Exception {
		byte[] byteMi = new BASE64Decoder().decodeBuffer(decryptString);
		IvParameterSpec zeroIv = new IvParameterSpec(new byte[8]);
		SecretKeySpec key = new SecretKeySpec(decryptKey.getBytes(), "DES");
		Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
		cipher.init(Cipher.DECRYPT_MODE, key, zeroIv);
		byte decryptedData[] = cipher.doFinal(byteMi);
	 
		return new String(decryptedData);
	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值