2010-11-15 17 views
1

在我的winForm的UI中,我要求用戶輸入用戶名和密碼。我將該密碼作爲文本存儲在我的項目的Settings(.settings文件)中。該應用程序是一個視覺工作室插件.NET:在我的項目.setting文件中解密密碼有哪些選項

我怎樣才能加密和解密密碼之前和之後,我把它輸入到設置文件?

我並不需要任何幻想。這只是一個小內部應用程序。但是,我想知道我的所有選項

感謝 鮑勃

回答

1

我這個DD吧:

public static string Decrypt(string stringToDecrypt) 
    { 
     UnicodeEncoding byteConverter = new UnicodeEncoding(); 
     byte[] dataToEncrypt = byteConverter.GetBytes(stringToDecrypt); 
     byte[] decryptedData = null; 
     try 
     { 
      using (RSACryptoServiceProvider rsaCryptoServiceProvider = new RSACryptoServiceProvider()) 
      { 
       rsaCryptoServiceProvider.FromXmlString(_key); 
       byte[] decryptBytes = Encoding.Default.GetBytes(Properties.Settings.Default.SqlPassword); 

       decryptedData = rsaCryptoServiceProvider.Decrypt(decryptBytes, false); 
      } 
     } 
     catch (Exception ex) 
     { 
      //TODO Do proper logging 
      Console.WriteLine("Decrypt failed: " + ex.Message); 
     } 

     return byteConverter.GetString(decryptedData); 
    } 

    public static string Encrypt(string stringToEncrypt) 
    { 
     try 
     { 
      UnicodeEncoding byteConverter = new UnicodeEncoding(); 
      byte[] dataToEncrypt = byteConverter.GetBytes(stringToEncrypt); 

      using (RSACryptoServiceProvider rsaCryptoServiceProvider = new RSACryptoServiceProvider()) 
      { 
       rsaCryptoServiceProvider.FromXmlString(_key); 
       byte[] encryptedData = rsaCryptoServiceProvider.Encrypt(dataToEncrypt, false); 

       return Encoding.Default.GetString(encryptedData); 
      } 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine("Encrypt failed: " + ex.Message); 
     } 
    } 
2

使用。NET加密功能簡單的東西as in this article

+1

這是2003年的一篇文章。自.NET 1以來沒有任何變化? – Bob 2010-11-15 11:17:33

+0

可能已經添加了一些高級的東西,但是因爲您正在尋找簡單的東西,所以使用這個舊的示例不會受到傷害。至少你可以試試:) – 2010-11-15 11:19:47

0

我有我自己的服務(寫在我的項目之一),你可以用我的代碼,如果你想:)你可以加密/解密的byte []和字符串

//*********************************************************************** 
// Assembly   : Common 
// Author   : SERKANH 
// Created   : 09-27-2009 
// 
// Last Modified By : SERKANH 
// Last Modified On : 09-27-2009 
// Description  : 
// 
// Copyright  : (c) Microsoft. All rights reserved. 
//*********************************************************************** 
using System; 
using System.IO; 
using System.Security.Cryptography; 
using System.Text; 

namespace Common 
{ 
    /// <summary> 
    /// This class provides 3DES encryption and decryption routines. 
    /// 
    /// Serkan Hekimoglu 
    /// </summary> 
    public class TripleDes 
    { 
     readonly byte[] _key = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24 }; 
     readonly byte[] _iv = { 8, 7, 6, 5, 4, 3, 2, 1 }; 

     // define the triple des provider 
     private readonly TripleDESCryptoServiceProvider _mDes = new TripleDESCryptoServiceProvider(); 

     // define the string handler 
     private readonly UTF8Encoding _mUtf8 = new UTF8Encoding(); 

     // define the local property arrays 
     private readonly byte[] _mKey; 
     private readonly byte[] _mIv; 

     /// <summary> 
     /// Default constructor 
     /// </summary> 
     public TripleDes() 
     { 
      _mKey = _key; 
      _mIv = _iv; 
     } 

     /// <summary> 
     /// Parameterized constructor 
     /// </summary> 
     /// <param name="key"></param> 
     /// <param name="iv"></param> 
     public TripleDes(byte[] key, byte[] iv) 
     { 
      _mKey = key; 
      _mIv = iv; 
     } 

     /// <summary> 
     /// Encrypts the given byte array input 
     /// </summary> 
     /// <param name="input">Input value</param> 
     /// <returns>Encrypted result</returns> 
     public byte[] Encrypt(byte[] input) 
     { 
      return Transform(input, _mDes.CreateEncryptor(_mKey, _mIv)); 
     } 

     /// <summary> 
     /// Decrypts the given encrypted byte array input 
     /// </summary> 
     /// <param name="input">Encrypted byte array input</param> 
     /// <returns>Decrypted result</returns> 
     public byte[] Decrypt(byte[] input) 
     { 
      return Transform(input, _mDes.CreateDecryptor(_mKey, _mIv)); 
     } 

     /// <summary> 
     /// Encrypts the given string input 
     /// </summary> 
     /// <param name="text">Input value</param> 
     /// <returns>Encrypted result</returns> 
     public string Encrypt(string text) 
     { 
      byte[] input = _mUtf8.GetBytes(text); 
      byte[] output = Transform(input, _mDes.CreateEncryptor(_mKey, _mIv)); 
      return Convert.ToBase64String(output); 
     } 

     /// <summary> 
     /// Decrypts the given encrypted string input 
     /// </summary> 
     /// <param name="text">Encrypted string input</param> 
     /// <returns>Decrypted result</returns> 
     public string Decrypt(string text) 
     { 
      byte[] input = Convert.FromBase64String(text); 
      byte[] output = Transform(input, _mDes.CreateDecryptor(_mKey, _mIv)); 
      return _mUtf8.GetString(output); 
     } 

     private static byte[] Transform(byte[] input, ICryptoTransform cryptoTransform) 
     { 
      // create the necessary streams 
      using (MemoryStream memStream = new MemoryStream()) 
      { 
       using (CryptoStream cryptStream = new CryptoStream(memStream, cryptoTransform, CryptoStreamMode.Write)) 
       { 
        // transform the bytes as requested 
        cryptStream.Write(input, 0, input.Length); 
        cryptStream.FlushFinalBlock(); 
        // Read the memory stream andconvert it back into byte array 
        memStream.Position = 0; 
        byte[] result = memStream.ToArray(); 
        // close and release the streams 
        memStream.Close(); 
        cryptStream.Close(); 
        // hand back the encrypted buffer 
        return result; 
       } 
      } 
     } 
    } 

}