2016-08-10 3 views
0
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Security.Cryptography; 
using System.IO; 
using System.ComponentModel; 


namespace DecryptionToEncryption 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      //Create byte arrays to hold original, encrypted, and decrypted data. 
      **byte[] encryptedstring = {0x7B,0x35,0x30,0x36,0x30,0x32,0x36,0x30,0x34,0x7C,0x55,0x38,0x30,0x30,0x45,0x44,0x45,0x37,0x33,0x46,0x32,0x34,0x31,0x41,0x43,0x32,0x45,0x35,0x38,0x41,0x37,0x44,0x37,0x34,0x43,0x38,0x37,0x39,0x44,0x31,0x44,0x37,0x37,0x7C,0x34,0x44,0x42,0x36,0x43,0x34,0x7D};** 
      string data = Encoding.UTF7.GetString(encryptedstring); 

      byte[] key = new byte[16]; 
      for (int i = 0; i < 16; ++i) 
      { 
       key[i] = 1; 
      } 

      byte[] iv = new byte[16]; 
      for (int i = 0; i < 16; ++i) 
      { 
       iv[i] = 1; 
      } 


      RijndaelManaged myRijndael = new RijndaelManaged(); 

      myRijndael.Key = key; 
      myRijndael.IV = iv; 
      byte[] encrypted = encryptStringToBytes_AES(data, myRijndael.Key, myRijndael.IV); 
      string str =Encoding.UTF7.GetString(encrypted); 
      char[] charValues = str.ToCharArray(); 
      string hexOutput = ""; 
      foreach (char _eachChar in charValues) 
      { 
       // Get the integral value of the character. 
       int value = Convert.ToInt32(_eachChar); 
       // Convert the decimal value to a hexadecimal value in string form. 
       hexOutput += String.Format("{0,10:X}", value); 
       // to make output as your eg 
       // hexOutput +=" "+ String.Format("{0:X}", value); 

      } 


      Console.WriteLine(hexOutput); 


      Console.ReadLine(); 

      // sends the byte array via active tcp connection 
      // _transport.SendEncryptedData(encrypted); 
     } 



     static byte[] encryptStringToBytes_AES(string plainText, byte[] Key, byte[] IV) 
     { 
      // Check arguments. 
      if (plainText == null || plainText.Length <= 0) 
       throw new ArgumentNullException("plainText"); 
      if (Key == null || Key.Length <= 0) 
       throw new ArgumentNullException("Key"); 
      if (IV == null || IV.Length <= 0) 
       throw new ArgumentNullException("IV"); 

      // Declare the stream used to encrypt to an in memory 
      // array of bytes. 
      MemoryStream msEncrypt = null; 

      // Declare the RijndaelManaged object 
      // used to encrypt the data. 
      RijndaelManaged aesAlg = null; 

      try 
      { 
       // Create a RijndaelManaged object 
       // with the specified key and IV. 
       aesAlg = new RijndaelManaged(); 
       aesAlg.Key = Key; 
       aesAlg.IV = IV; 

       // Create an encrypto to perform the stream transform. 
       ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV); 

       // Create the streams used for encryption. 
       msEncrypt = new MemoryStream(); 
       using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) 
       { 
        using (StreamWriter swEncrypt = new StreamWriter(csEncrypt)) 
        { 
         //Write all data to the stream. 
         swEncrypt.Write(plainText); 

        } 
       } 
      } 

      finally 
      { 
       // Clear the RijndaelManaged object. 
       if (aesAlg != null) 
        aesAlg.Clear();enter code here 
      } 

      // Return the encrypted bytes from the memory stream. 
      return msEncrypt.ToArray(); 
     } 
    } 
} 

このC#コードを実行して、AES/CBCを使用した暗号化を行っています。 私の入力が与えられたbelow.and私の出力が通りです: 36 E7 78 F8 B8 97 15 6C C3 73 EA A8 1B 12 71 C2 A0 5A F2 40 60 89 6B 8 70 90 C9 B6 75 57 F7 22 73 3D 15 AB B1 D5 E5 73 85 E A5 7E A9 D2 7C F2 48 C9 2D DF 6A 4E CA CB 31 AC D1 2 B2 C3 DB 89CESとオンラインツールでAES暗号化を使用すると異なる出力を得る

しかし、オンラインツールでは、異なるいくつかの...(最後の行は、鉱山の出力が異なる)を示している: 36 e7 78 f8 b8 97 15 6c c3 73 ea a8 1b 12 71 c2 a0 5a f2 40 60 89 6b 08 70 90 c9 b6 75 57 f7 22 73 3d 15 ab b1 d5 e5 73 85 0e a5 7e a9 d2 7c f2 ff aa 96 0e 3e f6 aa 0c 7e c5 15 2e 97 2f fd be

私を助けてくださいオンラインツールに表示されているものと同じ出力が得られます。

+0

質問レビュー:使用しているオンラインツールの詳細をコミュニティに提供してください。 –

+0

暗号化されたデータを提供します。ブロックサイズに 'RijndaelManaged'を指定するのが最善です.AESの場合は16バイト(128ビット)にする必要があります。 – zaph

+0

'encryptStringToBytes_AES'が出力するランダムなバイナリデータに対して、' string str = Encoding.UTF7.GetString(encrypted); 'を実行しても動作しません。文字列を表さないバイトに対して 'GetString'を呼び出さないでください。 –

答えて

2

暗号化された文字列の長さは51文字です。

byte[] encryptedstring = {0x7B,0x35,0x30,0x36,0x30,0x32,0x36,0x30,0x34,0x7C,0x55,0x38,0x30,0x30,0x45,0x44,0x45,0x37,0x33,0x46,0x32,0x34,0x31,0x41,0x43,0x32,0x45,0x35,0x38,0x41,0x37,0x44,0x37,0x34,0x43,0x38,0x37,0x39,0x44,0x31,0x44,0x37,0x37,0x7C,0x34,0x44,0x42,0x36,0x43,0x34,0x7D}; 

これは、CBCモードの可能な長さではありません。

AESは、CBCを含むほとんどのモードでデータブロックで動作するブロック暗号です。

短くなる復号化の入力により、復号化コードは、おそらく何かのガベージがあったとしても、欠けているバイトを何かで埋めることになります。これが最後のブロックであるため、異なる実装ではおそらく復号化が異なるでしょう。

しかし、それはUTF-8文字列としてデータを見て:1はなしのパターンで表示され、ランダムバイトのようになります。暗号化されたデータから期待されるものではありません

"{50602604|U800EDE73F241AC2E58A7D74C879D1D77|4DB6C4}" 

これは3つの構成要素でフォーマットされたデータのように見える:それは残りの32文字の指標とすることができるので、唯一の非進数文字のように立っている第二の成分の最初の文字で

50602604 
U800EDE73F241AC2E58A7D74C879D1D77 
4DB6C4 

正しく復号化できないのは不思議ではありません。

+0

@zaph ...私はあなたと同意しました。ancryptrdの文字列は51の長さで来ています。私は合計64の長さを持つためにパディングをしていました。そしてotpytも適切に与えていませんでした。 –

+0

私はcryptomath aes電卓を使用しました。ここではキーを16進数で追加しています。以下に与えられると、iv –

+0

のためのようにしかし、あなたは、データが暗号化されたAESであると思わせる、それが全体として暗号化されたデータのようには見えません。なぜあなたはUTF-7を使用していますか、それはむしろ非標準です。 – zaph

0
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Security.Cryptography; 
using System.IO; 
using System.ComponentModel; 

namespace final_encryption 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      try 
      { 

       string original = "{50602604|U800EDE73F241AC2E58A7D74C879D1D77|4DB6C4}"; //"Here is some data to encrypt!"; 
       Console.WriteLine("Original: " + original); 
       Console.ReadLine(); 

       // Create a new instance of the RijndaelManaged 
       // class. This generates a new key and initialization 
       // vector (IV). 
       byte[] key = new byte[16]; 
       for (int i = 0; i < 16; ++i) 
       { 
        key[i] = 1; 
       } 

       byte[] iv = new byte[16]; 
       for (int i = 0; i < 16; ++i) 
       { 
        iv[i] = 1; 
       } 
       using (RijndaelManaged myRijndael = new RijndaelManaged()) 
       { 
        // myRijndael.GenerateKey(); 
        //myRijndael.GenerateIV(); 
        myRijndael.Key = key; 
        myRijndael.IV = iv; 
        // Encrypt the string to an array of bytes. 
        byte[] encrypted = EncryptStringToBytes(original, myRijndael.Key, myRijndael.IV); 

        StringBuilder s = new StringBuilder(); 
        foreach (byte item in encrypted) 
        { 
         s.Append(item.ToString("X2") + " "); 
        } 
        Console.WriteLine("Encrypted: " + s); 
        Console.ReadLine(); 

        // Decrypt the bytes to a string. 
        string decrypted = DecryptStringFromBytes(encrypted, myRijndael.Key, myRijndael.IV); 

        //Display the original data and the decrypted data. 
        Console.WriteLine("Decrypted: " + decrypted); 
        Console.ReadLine(); 
       } 

      } 
      catch (Exception ex) 
      { 
       Console.WriteLine("Error: {0}", ex.Message); 
       Console.ReadLine(); 

      } 
     } 
     static byte[] EncryptStringToBytes(string plainText, byte[] Key, byte[] IV) 
     { 
      // Check arguments. 
      if (plainText == null || plainText.Length <= 0) 
       throw new ArgumentNullException("plainText"); 
      if (Key == null || Key.Length <= 0) 
       throw new ArgumentNullException("Key"); 
      if (IV == null || IV.Length <= 0) 
       throw new ArgumentNullException("Key"); 
      byte[] encrypted; 
      // Create an RijndaelManaged object 
      // with the specified key and IV. 
      using (RijndaelManaged rijAlg = new RijndaelManaged()) 
      { 
       rijAlg.Key = Key; 
       rijAlg.IV = IV; 
       rijAlg.Mode = CipherMode.CBC; 
       rijAlg.Padding = PaddingMode.Zeros; 

       // Create a decrytor to perform the stream transform. 
       ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV); 

       // Create the streams used for encryption. 
       using (MemoryStream msEncrypt = new MemoryStream()) 
       { 
        using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) 
        { 
         using (StreamWriter swEncrypt = new StreamWriter(csEncrypt)) 
         { 



          //Write all data to the stream. 

          swEncrypt.Write(plainText); 
         } 
         encrypted = msEncrypt.ToArray(); 
        } 
       } 
      } 


      // Return the encrypted bytes from the memory stream. 
      return encrypted; 

     } 

     static string DecryptStringFromBytes(byte[] cipherText, byte[] Key, byte[] IV) 
     { 
      // Check arguments. 
      if (cipherText == null || cipherText.Length <= 0) 
       throw new ArgumentNullException("cipherText"); 
      if (Key == null || Key.Length <= 0) 
       throw new ArgumentNullException("Key"); 
      if (IV == null || IV.Length <= 0) 
       throw new ArgumentNullException("Key"); 

      // Declare the string used to hold 
      // the decrypted text. 
      string plaintext = null; 

      // Create an RijndaelManaged object 
      // with the specified key and IV. 
      using (RijndaelManaged rijAlg = new RijndaelManaged()) 
      { 
       rijAlg.Key = Key; 
       rijAlg.IV = IV; 
       rijAlg.Mode = CipherMode.CBC; 
       rijAlg.Padding = PaddingMode.Zeros; 

       // Create a decrytor to perform the stream transform. 
       ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV); 

       // Create the streams used for decryption. 
       using (MemoryStream msDecrypt = new MemoryStream(cipherText)) 
       { 
        using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) 
        { 
         using (StreamReader srDecrypt = new StreamReader(csDecrypt)) 
         { 

          // Read the decrypted bytes from the decrypting stream 
          // and place them in a string. 
          plaintext = srDecrypt.ReadToEnd(); 
         } 
        } 
       } 

      } 

      return plaintext; 
     } 
    } 
} 

私は現在、オンラインツールで適切な答えを示している問題を解決しました。

MY出力: 36 E7 78 F8のB8 97 15(c)C3 73 EA A8 1B 12 71 C2 A0(a)F2 40 60 89 6B 08 70 90 C9 B6 75 57 F7 22 73 3D 15 AB B1 D5 E5 73 85 0E A5 7E A9 D2 7C F2 FF AA 96 0E 3E F6 AA 0C 7E C5 15 2E 97 2F FD BE

+0

これは面白いと予期せぬ復号質問です。基本的には、割り当てとして暗号化されていないデータを復号化しています。 – zaph

+0

ありがとう私の質問に関するすべての会話のすべて –

関連する問題