2014-02-20 82 views
0

我正在學習如何對字符串進行編碼和解碼。這是一種將我在網上找到的純文本解碼爲chob文本的方法。使用Base64String對已解碼的字符串進行編碼

public static string Decode(string chiperText) 
{ 
    byte[] numArray = Convert.FromBase64String(chiperText); 
    byte[] numArray1 = new byte[(int)numArray.Length - 1]; 
    byte num = (byte)(numArray[0]^188); 
    for (int i = 1; i < (int)numArray.Length; i++) 
    { 
     numArray1[i - 1] = (byte)(numArray[i]^188^num); 
    } 
    return Encoding.ASCII.GetString(numArray1); 
} 

我的問題是我不知道如何編碼到原始狀態。我嘗試這種方法,它不起作用。

public static string Encode(string plainText) 
{ 
    byte[] bytes = Encoding.ASCII.GetBytes(plainText); 

    byte[] results = new byte[(int)bytes.Length - 1]; 

    byte num = (byte)(bytes[0]^188); 
    for (int i = 1; i < bytes.Length; i++) 
    { 
     results[i - 1] = (byte)(bytes[i]^188^num); 
    } 

    return Convert.ToBase64String(results); 
} 
+4

**不要發明自己的密碼**,也不要使用隨機在線發明的密碼。這是相當不安全的。 – SLaks

+0

@SLaks不,它不是我的。我發現它幾乎沒有什麼變化。我只是學習不了多少。 – user3334858

+0

不要使用其他隨機人發明的加密。這個密碼是無用的。 – SLaks

回答

0

雖然我SLaks完全同意意見,上述不構成任何形式的密碼,你應該使用,下面的過程會產生「加密」的數據,你正在尋找解密的:

public static string Encode(string plainText) 
{ 
    byte[] numArray = System.Text.Encoding.Default.GetBytes(plainText); 
    byte[] numArray1 = new byte[(int)numArray.Length + 1]; 
    // Generate a random byte as the seed used 
    (new Random()).NextBytes(numArray1); 
    byte num = (byte)(numArray1[0]^188); 
    numArray1[0] = numArray1[0]; 
    for (int i = 0; i < (int)numArray.Length; i++) 
    { 
     numArray1[i + 1] = (byte)(num^188^numArray[i]); 
    } 
    return Convert.ToBase64String(numArray1); 
} 

請不要在一秒鐘內考慮將此作爲「加密」敏感數據的方法。

+1

謝謝你馬丁。我學習邏輯。這不是一個「真正的」密碼學。非常感謝。 – user3334858

相關問題