2016-06-09 72 views
2

工具:Visual Studio 2012. Windows 7 64位。 關鍵字:C#,註冊表,三重DES加密解密。3DES:從註冊表中接收不同的字節數組編碼字符串

我已經創建了一個演示程序來讀取和寫入(加密字符串)到註冊表。

這個程序背後的概念是:我想使用三重DES加密和解密數據(我已經使用TripleDESCryptoServiceProvider Class。)。加密後,加密字節數組作爲字符串存儲在註冊表中。使用這個,它完美地工作。 但是,當我從註冊表獲得這個字符串,並將其轉換的字節數組解密,數組的大小是不同的和加密期間,它顯示錯誤:

的數據長度解密是無效的。

下面是我的代碼:

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Security.Cryptography; 
using System.Text; 
using System.Threading.Tasks; 
using Microsoft.Win32; 

namespace TripleDES_in_Memory 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      try 
      { 
       using (RegistryKey key = Registry.CurrentUser.OpenSubKey("MyCompanyName\\")) 
       { 
        if (key != null) 
        { 
         object o = key.GetValue("TrialPeriod"); 
         if (o != null) 
         { 
          string result = Convert.ToString(o); 
          byte[] Data = Encoding.UTF8.GetBytes(result); 


          BinaryFormatter bf = new BinaryFormatter(); 
          using (var ms = new MemoryStream()) 
          { 
           bf.Serialize(ms, o); 
           byte[] narray = ms.ToArray(); 
          } 

          TripleDESCryptoServiceProvider tDESalg = new TripleDESCryptoServiceProvider(); 
          string keybyjaimesh = "MyEncryptKey"; 
          tDESalg.Key = Encoding.UTF8.GetBytes(keybyjaimesh.PadRight(24, ' ')); 

          string ipmanual = "ivmanual"; 
          tDESalg.IV = Encoding.UTF8.GetBytes(ipmanual.PadRight(8, ' ')); 
          byte[] iv = tDESalg.IV; 

          tDESalg.IV = iv; 
          string Final = DecryptTextFromMemory(Data, tDESalg.Key, tDESalg.IV); //decrypt 
          Console.WriteLine(Final); 
         } 
        } 
        else 
        { 
         TripleDESCryptoServiceProvider tDESalg = new TripleDESCryptoServiceProvider(); 

         string sData = "aaaaaaaaaaaaaaaaaaaaaaaa"; 
         DateTime today = DateTime.Today; 
         DateTime answer = today.AddDays(1); 

         string keybyjaimesh = "MyEncryptKey"; 

         tDESalg.Key = Encoding.UTF8.GetBytes(keybyjaimesh.PadRight(24, ' ')); 

         string ipmanual = "ivmanual"; 
         tDESalg.IV = Encoding.UTF8.GetBytes(ipmanual.PadRight(8, ' ')); 
         byte[] iv = tDESalg.IV; 

         byte[] Data = EncryptTextToMemory(sData, tDESalg.Key, tDESalg.IV); 
         Console.WriteLine("Encrypted data main function : " + System.Text.Encoding.UTF8.GetString(Data)); 


         Microsoft.Win32.RegistryKey key1; 
         key1 = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("MyCompanyName"); 
         key1.SetValue("TrialPeriod", System.Text.Encoding.UTF8.GetString(Data)); 
         key1.SetValue("IV", System.Text.Encoding.UTF8.GetString(iv)); 
         key1.Close(); 
        } 
       } 
      } 
      catch (Exception e) 
      { 
       Console.WriteLine(e.Message); 
      } 
     } 

     public static byte[] EncryptTextToMemory(string Data, byte[] Key, byte[] IV) 
     { 
      try 
      { 
       MemoryStream mStream = new MemoryStream(); 

       CryptoStream cStream = new CryptoStream(mStream, 
        new TripleDESCryptoServiceProvider().CreateEncryptor(Key, IV), 
        CryptoStreamMode.Write); 

       byte[] toEncrypt = new ASCIIEncoding().GetBytes(Data); 
       string result = System.Text.Encoding.UTF8.GetString(toEncrypt); 
       Console.WriteLine("byte to array : " + result); 

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

       byte[] ret = mStream.ToArray(); 
       Console.WriteLine("Encrypted data : " + System.Text.Encoding.UTF8.GetString(ret)); 

       cStream.Close(); 
       mStream.Close(); 

       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 
      { 
       MemoryStream msDecrypt = new MemoryStream(Data); 

       CryptoStream csDecrypt = new CryptoStream(msDecrypt, 
        new TripleDESCryptoServiceProvider().CreateDecryptor(Key, IV), 
        CryptoStreamMode.Read); 

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

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

       return new ASCIIEncoding().GetString(fromEncrypt); 
      } 
      catch (CryptographicException e) 
      { 
       Console.WriteLine("A Cryptographic error occurred: {0}", e.Message); 
       return null; 
      } 
     } 

    } 
} 

加密的字符串,在註冊表中:enter image description here

請提出您有什麼看法。

我該如何解決這個問題?

回答

1

我解決了它。

我有商店加密的字符串在將其轉換爲TOBase64String後在註冊表中。

string base64 = Convert.ToBase64String(encrypted string); 

對於解密,使用獲取字符串:

然後轉換成64基地字符串:

byte[] encoded = Convert.FromBase64String(encrypteddatafromregistry); 

這陣適用於解密。

相關問題