未解决的加密解密编码问题?

                                 未解决的加密解密编码问题? 

 

using System;

using System.Security.Cryptography;

using System.Text;

using System.IO;

 

class TrippleDESCSPSample

{

    static void Main ()

    {

        try

        {

            // Create a new TripleDESCryptoServiceProvider object

            // to generate a key and initialization vector (IV).

            //TripleDESCryptoServiceProvider tDESalg = new TripleDESCryptoServiceProvider();

 

            string s1 = "!@#$1234asdf";

            string s2 = "!@#$";

            byte[] b1 = Encoding.Unicode.GetBytes(s1);

            byte[] b2 = Encoding.Unicode.GetBytes(s2);

 

            // Create a string to encrypt.

            // string sData = "Here is some data to encrypt.";

            string sData = "Here is some data to encrypt.";

 

            // Encrypt the string to an in-memory buffer.

            //byte[] Data = EncryptTextToMemory(sData, tDESalg.Key, tDESalg.IV);

            byte[] Data = EncryptTextToMemory(sData, b1, b2);

            Console.WriteLine("byte[] Data = EncryptTextToMemory(sData, b1, b2):");

            Print(Data);

            Print2(Data);

            string s = new UnicodeEncoding().GetString(Data);

            Console.WriteLine(s);

            byte[] Data1 = new UnicodeEncoding().GetBytes(s);

            Console.WriteLine("byte[] Data1 = new UnicodeEncoding().GetBytes(s):");

            Print(Data1);

            Print2(Data1);

 

            // Decrypt the buffer back to a string.

            //string Final = DecryptTextFromMemory(Data, tDESalg.Key, tDESalg.IV);

            string Final = DecryptTextFromMemory(Data, b1, b2);

 

            // Display the decrypted string to the console.

            Console.WriteLine(Final);

            Console.ReadLine();

        }

        catch (Exception e)

        {

            Console.WriteLine(e.Message);

            Console.ReadLine();

 

        }

 

    }

 

    public static byte[] EncryptTextToMemory(string Data, byte[] Key, byte[] IV)

    {

        try

        {

            // Create a MemoryStream.

            MemoryStream mStream = new MemoryStream();

            TripleDESCryptoServiceProvider TDES = new TripleDESCryptoServiceProvider();

 

            // Create a CryptoStream using the MemoryStream

            // and the passed key and initialization vector (IV).

            CryptoStream cStream = new CryptoStream(mStream,

                TDES.CreateEncryptor(Key, IV),

                CryptoStreamMode.Write);

 

            // Convert the passed string to a byte array.

            byte[] toEncrypt = new UnicodeEncoding().GetBytes(Data);

 

            // Write the byte array to the crypto stream and flush it.

            cStream.Write(toEncrypt, 0, toEncrypt.Length);

            cStream.FlushFinalBlock();

            Console.WriteLine("toEncrypt.Length:{0}", toEncrypt.Length);

 

            // Get an array of bytes from the

            // MemoryStream that holds the

            // encrypted data.

            byte[] ret = mStream.ToArray();

            Console.WriteLine("the Length of mStream.ToArray:{0}", ret.Length);

            // Close the streams.

            cStream.Close();

            mStream.Close();

            // Return the encrypted buffer.

            return ret;

        }

        catch (CryptographicException e)

        {

            Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);

            return null;

        }

    }

 

    public static string DecryptTextFromMemory(byte[] Data, byte[] Key, byte[] IV)

    {

        try

        {

            // Create a new MemoryStream using the passed

            // array of encrypted data.

            MemoryStream msDecrypt = new MemoryStream(Data);

            TripleDESCryptoServiceProvider TDES = new TripleDESCryptoServiceProvider();

 

           // Create a CryptoStream using the MemoryStream

            // and the passed key and initialization vector (IV).

            CryptoStream csDecrypt = new CryptoStream(msDecrypt,

                TDES.CreateDecryptor(Key, IV),

                CryptoStreamMode.Read);

            Console.WriteLine("Data.Length:{0}", Data.Length);

 

            // Create buffer to hold the decrypted data.

            byte[] fromEncrypt = new byte[Data.Length];

 

            // Read the decrypted data out of the crypto stream

            // and place it into the temporary buffer.

            csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);

            Console.WriteLine("fromEncrypt.Length:{0}", fromEncrypt.Length);

 

            //Convert the buffer into a string and return it.

            return new UnicodeEncoding().GetString(fromEncrypt);

        }

        catch (CryptographicException e)

        {

            Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);

            return null;

        }

    }

 

    public static void Print(byte[] bytes)

    {

 

 

        if ((bytes == null) || (bytes.Length == 0))

            Console.WriteLine("<none>");

        else

        {

            Console.WriteLine("using for (int i = 0; i < bytes.Length; i++) and  bytes[i] print the encoded bytes:");

            for (int i = 0; i < bytes.Length; i++)

                Console.Write("{0:X2} ", bytes[i]);

            Console.WriteLine();

        }

    }

 

    public static void Print2(byte[] bytes)

    {

 

        Console.WriteLine("using foreach (Byte b in bytes) to print the encoded bytes:");

        foreach (Byte b in bytes)

        {

            Console.Write("[{0}]", b);

        }

        Console.WriteLine();

    }

 

}

 

 

测试字符串:  sData = "字是学习五笔字型的一个重要环节。";

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值