2014-04-11 3 views
1

10 개의 IP 주소를 저장해야하는 C# 응용 프로그램을 구현 중입니다. 그래서 응용 프로그램에 데이터베이스를 통합하면 자원 낭비라고 생각합니다. 나는 그 주소가 안전해야하기 때문에 XML이나 텍스트 파일을 사용할 수 없다. 나는 내 자신의 파일 형식을 구현하고 그것을 사용하도록 제안했다.
1. 별도의 파일 형식을 대신 구현하는 방법이 있습니까?
2. 새 파일 형식을 구현하는 방법이없고 무엇이 가장 좋은지 확인하십시오.데이터베이스없이 데이터를 영구 저장하십시오.

+0

포함 된 리소스로 설정이나 파일에 저장하지 않는 이유는 무엇입니까? 암호화/암호 해독 작업도 만들 수 있습니다. – CuriousPen

+0

고맙습니다. – Sandaru

답변

2

파일에 저장하고 파일을 암호화하지 않도록합니다 다른 프로그램에서 읽을 수 있습니다.

+0

암호화 알고리즘에 대한 많은 제안을 해주 셨습니다. – Sandaru

1

암호화 된 데이터가 안전하지 않을 위험이 없으면 Salt를 적용하고 Text File 또는 Xml에 저장하십시오.

이 샘플을 참조하십시오

using System.Security.Cryptography; 

public static string EncodePasswordToBase64(string password) 
{ byte[] bytes = Encoding.Unicode.GetBytes(password); 
    byte[] inArray = HashAlgorithm.Create("SHA1").ComputeHash(bytes); 
    return Convert.ToBase64String(inArray); 
} 

해싱이 방법의 문자열을 envcrypt하는 SHA1를 사용하여 적용됩니다.

2

당신은 세부 사항에게 sqlite 데이터베이스 또는 file에 저장 할 수 있습니다,

당신이 강력한 암호화를 사용하여 개인 다음 encrypt 파일

1

암호화 문자열로 물건을 유지하려는 경우. 여기에 내가 사용하는 두 가지 방법. 그것은 강력하게 암호화하고 또한 그것에 소금을 추가합니다.

public static string EncryptString(string sData, string sKey) 
    { 
     // instance of the Rihndael. 
     RijndaelManaged RijndaelManagedCipher = new RijndaelManaged(); 

     // string to byte array. 
     byte[] UnicodeText = System.Text.Encoding.Unicode.GetBytes(sData); 

     // adign dirt to the string to make it harder to guess using a dictionary attack. 
     byte[] Dirty = Encoding.ASCII.GetBytes(sKey.Length.ToString()); 

     // The Key will be generated from the specified Key and dirt. 
     PasswordDeriveBytes FinalKey = new PasswordDeriveBytes(sKey, Dirty); 

     // Create a encryptor from the existing FinalKey bytes.   
     ICryptoTransform Encryptor = RijndaelManagedCipher.CreateEncryptor(FinalKey.GetBytes(32), FinalKey.GetBytes(16)); 

     // Create a MemoryStream that is going to hold the encrypted bytes 
     MemoryStream memoryStream = new MemoryStream(); 

     // Create a CryptoStream 
     CryptoStream cryptoStream = new CryptoStream(memoryStream, Encryptor, CryptoStreamMode.Write); 

     // write the encryption 
     cryptoStream.Write(UnicodeText, 0, UnicodeText.Length); 

     // write final blocks to the memory stream 
     cryptoStream.FlushFinalBlock(); 

     // Convert to byte array the encrypted data 
     byte[] CipherBytes = memoryStream.ToArray(); 

     // Close streams. 
     memoryStream.Close(); 
     cryptoStream.Close(); 

     // Convert to byte array to string 
     string EncryptedData = Convert.ToBase64String(CipherBytes); 

     // Return the encrypted string 
     return EncryptedData; 

    } 

    public static string DecryptString(string sData, string sKey) 
    { 
     // instance of rijndael 
     RijndaelManaged RijndaelCipher = new RijndaelManaged(); 

     // convert to byte aray the encrypted data 
     byte[] EncryptedData = Convert.FromBase64String(sData); 

     // add dirt to the key like when encrypthing 
     byte[] Dirty = Encoding.ASCII.GetBytes(sKey.Length.ToString()); 

     // get the finalkey o be used 
     PasswordDeriveBytes FinalKey = new PasswordDeriveBytes(sKey, Dirty); 

     // Create a decryptor with the key 
     ICryptoTransform Decryptor = RijndaelCipher.CreateDecryptor(FinalKey.GetBytes(32), FinalKey.GetBytes(16)); 

     // load to memory stream the encrypted data 
     MemoryStream memoryStream = new MemoryStream(EncryptedData); 

     // Create a CryptoStream on the memory stream holding the data 
     CryptoStream cryptoStream = new CryptoStream(memoryStream, Decryptor, CryptoStreamMode.Read); 

     // Length is unknown but need placeholder big enought for decrypted data 
     // we know the decrypted version cannot ever be longer than the crypted version 
     // since we added bunch of garbage to it so the length of encrypted data is safe to use 
     byte[] UnicodeText = new byte[EncryptedData.Length]; 

     // Start decrypting 
     int DecryptedCount = cryptoStream.Read(UnicodeText, 0, UnicodeText.Length); 

     //close streams 
     memoryStream.Close(); 
     cryptoStream.Close(); 

     // load decrypted data to string 
     string DecryptedData = Encoding.Unicode.GetString(UnicodeText, 0, DecryptedCount); 

     // Return decrypted string 
     return DecryptedData; 
    } 

에 추가이

이제 단순히

public class Settings 
{ 
    public const string EncryptionKey = "somekey"; 
    public List<string> IP = new List<string>();  

    public string getClassEncrypted() 
    { 
     return EncryptString(new JavaScriptSerializer().Serialize(this), EncryptionKey); 
    } 

    public Settings getClassDecrypted(string sClassEcrypted) 
    { 
     return new JavaScriptSerializer().Deserialize<Settings>(DecryptString(sClassEcrypted, EncryptionKey)); 
    } 
} 

하나 IP를 같은 클래스는 단지 파일 Settings.getClassEncrypted(); 하고 시간이 때 다음 얻기 위해 쓰기로 설정되어 있는지 확인 값을 다시 텍스트 파일을 읽고이 같은 무언가로 다시로드하십시오 :

string sFileText = ...; // from the file saved 
var setting = new Settings.getClassDecrypted(sFileText); 

이제 모든 수업을 들으셔야합니다. 그리고 클래스도 직렬화됩니다.

+0

매우 감사합니다. 매우 유용합니다. – Sandaru

+0

나는 실제로 각 행에 주석을 달기에는 충분히 친절했습니다. :) 그들이 암호화에 대해 전혀 이해하지 못한다고 불평 한 제 동료 들께 감사드립니다. – Franck

관련 문제