ROT13是一種簡單的加密方式,主要是把26個英文字母前13個和後13個對調,起到一定的英文文字加密保護作用。 今天我們來看下在C#中使用ROT13加密解密的實現,我們就用一個執行個體來為大家解答一下吧.
雖然加密方式簡單,但是Windows的註冊表裡面都使用了ROT13加密,可見其使用還是比較多的。
public string ROT13Encode(string InputText)
{
int i;
char CurrentCharacter;
int CurrentCharacterCode;
string EncodedText = \"\";
//Iterate through the length of the input parameter
for (i = 0; i < InputText.Length; i++)
{
//Convert the current character to a char
CurrentCharacter = System.Convert.ToChar(InputText.Substring(i, 1));
//Get the character code of the current character
CurrentCharacterCode = (int)CurrentCharacter;
//Modify the character code of the character, - this
//so that \"a\" becomes \"n\", \"z\" becomes \"m\", \"N\" becomes \"Y\" and so on
if (CurrentCharacterCode >= 97 && CurrentCharacterCode <= 109)
{
CurrentCharacterCode = CurrentCharacterCode + 13; [Page]
}
else if (CurrentCharacterCode >= 110 && CurrentCharacterCode <= 122)
{
CurrentCharacterCode = CurrentCharacterCode - 13;
}
else if (CurrentCharacterCode >= 65 && CurrentCharacterCode <= 77)
{
CurrentCharacterCode = CurrentCharacterCode + 13;
}
else if (CurrentCharacterCode >= 78 && CurrentCharacterCode <= 90)
{
CurrentCharacterCode = CurrentCharacterCode - 13;
}
//Add the current character to the string to be returned
EncodedText = EncodedText + (char)CurrentCharacterCode;
}
return EncodedText;
}
[Page]
加密和解密的方法都一樣,把字串傳入方法返回得到的字串就是要加密和解密的字串了。