C#简单加密 [英] C# simple encryption
问题描述
我正在学习C#(作为一个业余爱好者),并试图制定一个简单的加密程序。
从ASCII转换字符是正确的,得到了排序,但这是我被困的转变。如果我想为(n + 3)编码,我应该遵循什么思路?
您是否尝试像凯撒密码一样做? (即采取每一个字母,并向左或向右移一定数量的字母?)
如果是这样,您将需要处理字符串上的单个字符。有一个数据类型char允许这个,你可以做算术运算。要从字符串获取一个字符数组,请调用(string).ToCharArray()。然后,您可以遍历字符并移动每个字符。
char key =(char)3;
string toEncrypt =abcdef;
char [] cArray = toEncrypt.ToCharArray();
for(var i = 0; i< cArray.Length; i ++){
cArray [i] =(char)(cArray [i] + key)
}
有一件事要记住,你需要包装你的角色,如果他们超过字母表的结尾。我会把这作为你的挑战。 :)
请记住,虽然这是一种有趣的学习语言的方法,但它在现实世界中对于加密并不是很有用,并且非常容易被破坏。 >
Hi' I'm learning C# (as a hobbyist) and am trying to figure how to write a simple encryption program.
Converting characters from ASCII is fine, got that sorted, but it is the shift I am stuck on. If I want to code for (n + 3) for example, what line of thought should I be following?
Are you trying to do something like a Caesar cipher? (i.e. take every letter and shift it a certain number of letters to the left or right?)
If so, you will want to work with individual characters on your string. There's a datatype char that allows this, and you can do arithmetic operations. To get an array of chars from a string, call (string).ToCharArray(). Then you can iterate through the characters and shift each character.
char key = (char)3;
string toEncrypt = "abcdef";
char[] cArray = toEncrypt.ToCharArray();
for (var i = 0; i < cArray.Length; i++) {
cArray[i] = (char)(cArray[i] + key);
}
One thing to keep in mind is that you'll need to "wrap" your characters if they go past the end of the alphabet. I'll leave this as a challenge to you. :)
Keep in mind that while this is a fun way to learn the language, it's not useful in the real world for encryption and is extremely easily broken.
这篇关于C#简单加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!