C#中使用ROT13加密解密
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]
加密和解密的方法都一样,把字符串传入方法返回得到的字符串就是要加密和解密的字符串了。
虽然加密方式简单,但是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]
加密和解密的方法都一样,把字符串传入方法返回得到的字符串就是要加密和解密的字符串了。
本文来自博客园,作者:古道轻风,转载请注明原文链接:https://www.cnblogs.com/88223100/archive/2008/10/01/1302807.html
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析