2011-03-09 19 views
7

私は、.NET 3.5を使用してバイトの配列を暗号化/難読化(もちろん解読/解読)する方法を探しています。.NETの秘密を使ってバイト配列を暗号化/難読化する簡単な方法は?

基本的には:

byte[] aMixedUp = Encrypt(aMyByteData, "THIS IS THE SECRET KEY USED TO ENCRYPT"); 

と反対側に:

byte[] aDecrypted = Decrypt(aMixedUp, "THIS IS THE SECRET KEY USED TO ENCRYPT"); 

それは弾丸の証拠である必要はありません。考え方は、ユーザーがASCIIにマップする場合にバイト内の内容を直接見るのを防ぐことですが、ROT13よりも優れているはずです。

.NETライブラリには、簡単に使用できるものがありますか?

+0

最も簡単な方法は、[ここに私の質問](http://stackoverflow.com/questions/11762/c-cryptographicexception-padding-is-invalid-and-cannot-be-removed)である – Blorgbeard

+0

だからベース変換は問題外ですか? Base64またはBase2に変換すると言うことができますか?私はそれがROT13よりも良いと思う! – Ali

+1

@Blorgbeard:それを答えにしてみてはいかがですか、私はあなたに信用を与えるでしょう!それはまさに私が目指してきたものです! – Krumelur

答えて

7

文字列を暗号化/復号化するためのコードです。暗号化された文字列はBase64でエンコードされてXMLなどへのシリアライズが容易です。このコードを簡単に変換して、文字列ではなくバイト配列で直接動作させることができます。私が発見した

/// <summary> 
/// Create and initialize a crypto algorithm. 
/// </summary> 
/// <param name="password">The password.</param> 
private static SymmetricAlgorithm GetAlgorithm(string password) 
{ 
    var algorithm = Rijndael.Create(); 
    var rdb = new Rfc2898DeriveBytes(password, new byte[] { 
     0x53,0x6f,0x64,0x69,0x75,0x6d,0x20,    // salty goodness 
     0x43,0x68,0x6c,0x6f,0x72,0x69,0x64,0x65 
    }); 
    algorithm.Padding = PaddingMode.ISO10126; 
    algorithm.Key = rdb.GetBytes(32); 
    algorithm.IV = rdb.GetBytes(16); 
    return algorithm; 
} 


/// <summary> 
/// Encrypts a string with a given password. 
/// </summary> 
/// <param name="clearText">The clear text.</param> 
/// <param name="password">The password.</param> 
public static string EncryptString(string clearText, string password) 
{ 
    var algorithm = GetAlgorithm(password); 
    var encryptor = algorithm.CreateEncryptor(); 
    var clearBytes = Encoding.Unicode.GetBytes(clearText); 
    using (var ms = new MemoryStream()) 
    using (var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write)) 
    { 
     cs.Write(clearBytes, 0, clearBytes.Length); 
     cs.Close(); 
     return Convert.ToBase64String(ms.ToArray()); 
    } 
} 

/// <summary> 
/// Decrypts a string using a given password. 
/// </summary> 
/// <param name="cipherText">The cipher text.</param> 
/// <param name="password">The password.</param> 
public static string DecryptString(string cipherText, string password) 
{ 
    var algorithm = GetAlgorithm(password); 
    var decryptor = algorithm.CreateDecryptor(); 
    var cipherBytes = Convert.FromBase64String(cipherText); 
    using (var ms = new MemoryStream()) 
    using (var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Write)) 
    { 
     cs.Write(cipherBytes, 0, cipherBytes.Length); 
     cs.Close(); 
     return Encoding.Unicode.GetString(ms.ToArray()); 
    } 
} 
+1

すでに述べましたが、これは私のお気に入りです!ありがとう! – Krumelur

3

Symmetric-key algorithmこれを行う最も簡単な方法は、.NETフレームワークで見つけることができます。

ハッカーがあなたのアプリを「easly」逆コンパイルして暗号化キーを見つけることができることに注意してください。あなたのシナリオに応じて、public/privateキーシステムを使用することができます。少なくとも、バイト配列を暗号化できる人を制御できます。

4

次は、.NETフレームワークのRijndaelクラスを使用してバイト配列を暗号化および復号化するコードのサンプルです。明らかに、このクラスはあなたに一番合っています。

KEYとIVプロパティを定義し、どこか(アプリケーション設定ファイルの暗号化されたセクションなど)から取得するだけで済みます。あなたは暗号化を必要としない場合

private static byte[] EncryptBytes(IEnumerable<byte> bytes) 
    { 
     //The ICryptoTransform is created for each call to this method as the MSDN documentation indicates that the public methods may not be thread-safe and so we cannot hold a static reference to an instance 
     using (var r = Rijndael.Create()) 
     { 
      using (var encryptor = r.CreateEncryptor(KEY, IV)) 
      { 
       return Transform(bytes, encryptor); 
      } 
     } 
    } 

    private static byte[] DecryptBytes(IEnumerable<byte> bytes) 
    { 
     //The ICryptoTransform is created for each call to this method as the MSDN documentation indicates that the public methods may not be thread-safe and so we cannot hold a static reference to an instance 
     using (var r = Rijndael.Create()) 
     { 
      using (var decryptor = r.CreateDecryptor(KEY, IV)) 
      { 
       return Transform(bytes, decryptor); 
      } 
     } 
    } 

    private static byte[] Transform(IEnumerable<byte> bytes, ICryptoTransform transform) 
    { 
     using (var stream = new MemoryStream()) 
     { 
      using (var cryptoStream = new CryptoStream(stream, transform, CryptoStreamMode.Write)) 
      { 
       foreach (var b in bytes) 
        cryptoStream.WriteByte(b); 
      } 

      return stream.ToArray(); 
     } 
    } 
0

、あなただけ効果的にそれは誰かが実際に専用されていない限り、それは不可能読むために作ってあげる、HEXまたはベース64またはその種のものにすべてを変換することができます。 Here is a link that shows how in .NET

+5

デコードのヘックスやベース64のカウントは「本当に専用」とは思えません。 – Blorgbeard

関連する問題