一个java的DES加解密类转换成C# (二)

2014-11-24 09:56:24 · 作者: · 浏览: 1
u4T4oHMeqetFZqU5oh2XY1sbBPPdGEgMf
/OguRVaTblzl/ylkFc6C9BNNSD0IwL0Ks7Mi73+V76P+aFdPgXQc7u4Vkq8Cd6+HgHErbHbJI729
JPJKM5L2YAAW4Q06oi4yMoEASDjYf7Aa1X/FWqclsZImSDB0okGOiuj857l94BM1zYl2RtWdXa9o
0beiL4CbEvKSC3U3PydAI0+mZbtE0sVkyP0sXTke7ifrwiMG
加密后的字符串长度:360
解密后的字符串:< xml version="1.0" encoding="UTF-8" >
00417850COM011109141222222660161
0000000123客户信息不完整,请补充资料。

解密后的字符串长度:262

,修改里面的des向量iv,字符编码UTF8为Default等,最终
改写为C#代码如下:


using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Security.Cryptography; 
 
public class DES 
{ 
    private const string defaultKey = "BOC_PLFP"; //默认密钥  
    private static byte[] iv = { 0x12, 0x34, 0x56, 0x78, (byte)0x90, (byte)0xab, (byte)0xcd, (byte)0xef };//des 向量  
                
    ///   
    ///  des加密  
    ///   
    /// 要加密的字符串。  
    /// 密钥,且必须为8位。默认公钥加密字符串defaultKey  
    /// 以Base64格式返回的加密字符串。  
    public static string Encrypt(string pToEncrypt, string sKey = defaultKey) 
    { 
        using (DESCryptoServiceProvider des = new DESCryptoServiceProvider()) 
        { 
            byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt); 
            des.Key = ASCIIEncoding.ASCII.GetBytes(sKey); 
            des.IV = iv;  //ASCIIEncoding.ASCII.GetBytes(sKey);  
            System.IO.MemoryStream ms = new System.IO.MemoryStream(); 
            using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write)) 
            { 
                cs.Write(inputByteArray, 0, inputByteArray.Length); 
                cs.FlushFinalBlock(); 
                cs.Close(); 
            } 
            string str = Convert.ToBase64String(ms.ToArray()); 
            ms.Close(); 
            return str; 
        } 
    } 
             
    ///   
    ///  des解密  
    ///   
    /// 要解密的以Base64  
    /// 密钥,且必须为8位。默认公钥解密字符串defaultKey  
    /// 已解密的字符串。  
    public static string Decrypt(string pToDecrypt, string sKey = defaultKey) 
    { 
        byte[] inputByteArray = Convert.FromBase64String(pToDecrypt); 
        using (DESCryptoServiceProvider des = new DESCryptoServiceProvider()) 
        { 
            des.Key = ASCIIEncoding.ASCII.GetBytes(sKey); 
            des.IV = iv; // ASCIIEncoding.ASCII.GetBytes(sKey);  
            System.IO.MemoryStream ms = new System.IO.MemoryStream(); 
            using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write)) 
            { 
                cs.Write(inputByteArray, 0, inputByteArray.Length); 
                cs.FlushFinalBlock(); 
                cs.Close(); 
            } 
            string str = Encoding.Default.GetString(ms.ToArray()); 
            ms.Close(); 
            return str; 
        } 
    } 
} 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

public class DES
{
    private const string defaultKey = "BOC_PLFP"; //默认密钥
    private static byte[] iv = { 0x12, 0x34, 0x56, 0x78, (byte)0x90, (byte)0xab, (byte)0xcd, (byte)0xef };//des 向量
              
    /// 
    ///  des加密
    /// 
    /// 要加密的字符串。
    /// 密钥,且必须为8位。默认公钥加密字符串defaultKey
    /// 以Base64格式返回的加密字符串。
    public static string Encrypt(string pToEncrypt, string sKey = defaultKey)
    {
        using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
        {
            byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
            des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
            des.IV = iv;  //ASCIIEncoding.ASCII.GetBytes(sKey);
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
            {
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                cs.Close();
            }
            string str = Convert.ToBas