2013-01-23 72 views
3

我今天正在通過加密,我的同事告訴我一個更簡單/自定義的方式來做到這一點,我所做的是在那裏列出的,所以你能告訴我什麼類型的加密或hashign是什麼?他告訴我這是混合了某種哈希和加密。對不起,我是新來的愚蠢問題。 :) 下面的方法需要一個字符串(在我的情況下是密碼)。反轉它並製作字典(添加字典項目在下面列出)並匹配鍵和值並返回加密數據。加密名稱

public static string Encrypt(string source) 
{ 
    string enCrypted = ""; 
    try 
    { 
     source = Reverse(source); 
     Dictionary<char, char> sourceTable = AddDictionaryItems(); 

     char[] sourceArray = source.ToCharArray(); 
     StringBuilder encryptedData = new StringBuilder(); 
     foreach (char chr in sourceArray) 
     { 
      KeyValuePair<char, char> Pair; 
      Pair = sourceTable.First(tuple => tuple.Key == chr); 
      encryptedData.Append(Pair.Value); 
     } 
     enCrypted=encryptedData.ToString(); 
    } 
    catch (Exception ex) 
    { 

    } 
    return enCrypted; 
} 

以下方法將項目添加到字典中,項目基本上基於ascii代碼。我循環所有的asciis,然後相應的charachters被添加到字典中。但真正的技巧是在字典中添加項目時。例如,當我在字典中添加'A'作爲關鍵字時,它的值就是'D'。變量「jump」指定鍵和值之間的差異。所以如果我的密碼是'ABC',它會返回'DEF'或'FED',如果我逆轉那件事。

public static Dictionary<char, char> AddDictionaryItems() 
{ 
    Dictionary<char, char> dc = new Dictionary<char, char>(); 
    try 
    { 
     int initial = 33; 
     int jump = 3; 
     int final = 127 - jump; 

     for (int ascii = initial; ascii < final; ascii++) 
     { 
      dc.Add((char)ascii, (char)(ascii + jump)); 
     } 
     for (int ascii = final; ascii < final + jump; ascii++) 
     { 
      dc.Add((char)ascii, (char)(initial)); 
      initial++; 
     } 
    } 
    catch (Exception ex) 
    { 
     throw ex; 
    } 
    return dc; 
} 
+3

'ABC' - >'DEF'是凱撒密碼的一個實例,它是一種像在高速公路上錯開車道一樣安全的加密方法。 – mmgp

+0

你在這裏得到了一個點:) –

回答

7

這將是一個Substitution Cipher,但除非你正在以此爲樂,不使用任何東西這種方法需要的安全性。 1000's of people crack this encryption method every day for fun(查看你當地的報紙,它可能也有一個,它通常在填字遊戲旁邊)。

+0

ahahhah你讓我,是的兄弟,你有一個點,是的我記得我在我的網絡課程學習ceaser密碼。謝謝你提醒我。我只是爲了好玩而使用這個東西。更進一步,我添加了兩個guid,使其看起來更難:)但你的觀點是有效的 –

0

傢伙,我已經定製我的代碼

public static string Reverse(string str) 
    { 
     char[] charArray = str.ToCharArray(); 
     Array.Reverse(charArray); 
     return new string(charArray); 
    } 

    public static string Encrypt(string source) 
    { 
     string prefix = Guid.NewGuid().ToString(); 
     string infix = Guid.NewGuid().ToString(); 
     string postfix = Guid.NewGuid().ToString(); 

     int length = source.Length; 
     string firstHalf = source.Substring(0, length/2); 
     string secondHalf = source.Substring(length/2); 

     string ConcatedPassword = prefix + firstHalf + infix + secondHalf + postfix; 

     string enCrypted = ""; 
     try 
     { 
      ConcatedPassword = Reverse(ConcatedPassword); 
      Dictionary<char, char> sourceTable = AddDictionaryItems(); 

      char[] sourceArray = ConcatedPassword.ToCharArray(); 
      StringBuilder encryptedData = new StringBuilder(); 
      foreach (char chr in sourceArray) 
      { 
       KeyValuePair<char, char> Pair; 
       Pair = sourceTable.First(tuple => tuple.Key == chr); 
       encryptedData.Append(Pair.Value); 
      } 
      enCrypted = encryptedData.ToString(); 
     } 
     catch (Exception ex) 
     { 

     } 
     return enCrypted; 
    } 

    public static string Decrypt(string source) 
    { 
     string deCrypted = ""; 
     try 
     { 
      source = Reverse(source); 
      Dictionary<char, char> sourceTable = AddDictionaryItems(); 

      char[] sourceArray = source.ToCharArray(); 
      StringBuilder decryptedData = new StringBuilder(); 
      foreach (char chr in sourceArray) 
      { 
       KeyValuePair<char, char> Pair; 
       Pair = sourceTable.First(tuple => tuple.Value == chr); 
       decryptedData.Append(Pair.Key); 
      } 
      deCrypted = decryptedData.ToString(); 
      string prefixRemoved = deCrypted.Remove(0, 36); 
      string postfixRemoved = prefixRemoved.Remove(prefixRemoved.Length - 36); 
      string infixRemoved = postfixRemoved.Remove((postfixRemoved.Length - 36)/2, 36); 
      deCrypted = infixRemoved; 
     } 
     catch (Exception ex) 
     { 

     } 
     return deCrypted; 
    } 

    public static Dictionary<char, char> AddDictionaryItems() 
    { 
     Dictionary<char, char> dc = new Dictionary<char, char>(); 
     try 
     { 
      int initial = 33; 
      int jump = 10; 
      int final = 127 - jump; 

      for (int ascii = initial; ascii < final; ascii++) 
      { 
       dc.Add((char)ascii, (char)(ascii + jump)); 
      } 
      for (int ascii = final; ascii < final + jump; ascii++) 
      { 
       dc.Add((char)ascii, (char)(initial)); 
       initial++; 
      } 
     } 
     catch (Exception ex) 
     { 

     } 
     return dc; 
    } 

protected void Page_Load(object sender, EventArgs e) 
{ 
    string password = "[email protected]"; 
    string encrypted = Encrypt(password); 
    string decrypted = Decrypt(encrypted); 
} 

加密是不安全的,但它的一個開端。我希望它能幫助別人從頭開始寫他自己的方法。