C# 帮我做XOR加密
我在C# 帮我做XOR加密,c#,encryption,encryption-symmetric,C#,Encryption,Encryption Symmetric,我在C#中编写了这段代码,用密钥加密字符串: private static int Bin2Dec(string num) { int _num = 0; for (int i = 0; i < num.Length; i++) _num += (int)Math.Pow(2, num.Length - i - 1) * int.Parse(num[i].ToString()); return _num; } private static st
C#
中编写了这段代码,用密钥加密字符串:
private static int Bin2Dec(string num)
{
int _num = 0;
for (int i = 0; i < num.Length; i++)
_num += (int)Math.Pow(2, num.Length - i - 1) * int.Parse(num[i].ToString());
return _num;
}
private static string Dec2Bin(int num)
{
if (num < 2) return num.ToString();
return Dec2Bin(num / 2) + (num % 2).ToString();
}
public static string StrXor(string str, string key)
{
string _str = "";
string _key = "";
string _xorStr = "";
string _temp = "";
for (int i = 0; i < str.Length; i++)
{
_temp = Dec2Bin(str[i]);
for (int j = 0; j < 8 - _temp.Length + 1; j++)
_temp = '0' + _temp;
_str += _temp;
}
for (int i = 0; i < key.Length; i++)
{
_temp = Dec2Bin(key[i]);
for (int j = 0; j < 8 - _temp.Length + 1; j++)
_temp = '0' + _temp;
_key += _temp;
}
while (_key.Length < _str.Length) _key += _key;
if (_key.Length > _str.Length) _key = _key.Substring(0, _str.Length);
for (int i = 0; i < _str.Length; i++)
if (_str[i] == _key[i]) { _xorStr += '0'; } else { _xorStr += '1'; }
_str = "";
for (int i = 0; i < _xorStr.Length; i += 8)
{
char _chr = (char)0;
_chr = (char)Bin2Dec(_xorStr.Substring(i, 8)); //ERROR : (Index and length must refer to a location within the string. Parameter name: length)
_str += _chr;
}
return _str;
}
有什么线索吗?如果你有一个字符,一个
char
,你可以把它转换成一个整数,一个int
然后可以使用^
操作符对其执行异或。您目前似乎没有使用该运算符,这可能是问题的根源
string EncryptOrDecrypt(string text, string key)
{
var result = new StringBuilder();
for (int c = 0; c < text.Length; c++)
result.Append((char)((uint)text[c] ^ (uint)key[c % key.Length]));
return result.ToString();
}
字符串加密加密加密(字符串文本、字符串密钥)
{
var result=新的StringBuilder();
for(int c=0;c
那种事。下面是一个较长的版本,其中包含注释,这些注释分步骤执行相同的操作,以便于学习:
string EncryptOrDecrypt(string text, string key)
{
var result = new StringBuilder();
for (int c = 0; c < text.Length; c++)
{
// take next character from string
char character = text[c];
// cast to a uint
uint charCode = (uint)character;
// figure out which character to take from the key
int keyPosition = c % key.Length; // use modulo to "wrap round"
// take the key character
char keyChar = key[keyPosition];
// cast it to a uint also
uint keyCode = (uint)keyChar;
// perform XOR on the two character codes
uint combinedCode = charCode ^ keyCode;
// cast back to a char
char combinedChar = (char)combinedCode;
// add to the result
result.Append(combineChar);
}
return result.ToString();
}
字符串加密加密加密(字符串文本、字符串密钥)
{
var result=新的StringBuilder();
for(int c=0;c
简短版本相同,但删除了中间变量,将表达式直接替换到使用它们的位置。下面是一些简单的加密和解密代码
class CEncryption
{
public static string Encrypt(string strIn, string strKey)
{
string sbOut = String.Empty;
for (int i = 0; i < strIn.Length; i++)
{
sbOut += String.Format("{0:00}", strIn[i] ^ strKey[i % strKey.Length]);
}
return sbOut;
}
public static string Decrypt(string strIn, string strKey)
{
string sbOut = String.Empty;
for (int i = 0; i < strIn.Length; i += 2)
{
byte code = Convert.ToByte(strIn.Substring(i, 2));
sbOut += (char)(code ^ strKey[(i/2) % strKey.Length]);
}
return sbOut;
}
}
类加密
{
公共静态字符串加密(字符串strIn、字符串strKey)
{
string sbOut=string.Empty;
for(int i=0;i
公共静态字节[]加密加密加密(字节[]文本,字节[]密钥)
{
byte[]xor=新字节[text.Length];
for(int i=0;i
我所能说的就是呃?:)也许这是一个教育性的练习,但您不需要将字符转换为字符串,手动对它们进行异或,然后将它们转换回字符串。正如Dec2Bin和Bin2Dec函数所证明的那样,char可以通过强制转换转换为int和int,因此只需从两个字符串中提取char,应用“^”xor运算符并放入新字符串。如果您指定得到的错误,这会有所帮助:)此外,您可能希望使用StringBuilder而不是字符串。字符串是不可变的(它们不能更改),因此_str+=\u temp;每次生成一个新字符串,这使得此方法不必要地繁重/昂贵。使用StringBuilder和.Append(temp)。@Michael Stum:事实证明,StringBuilder实际上可能会慢一些,或者与使用串联差不多。StringBuilder只对大字符串更好。@siride它可能会慢一些,但主要是对小的连接(我认为是3-4个字符串)。在他的例子中,循环是不确定的,因为它取决于输入字符串的长度,因此使用SB是一种更安全的选择(如果当然使用诸如str.length之类的合理缓冲区大小初始化它),解密函数在包含大写字母的字符串上失败。此函数是否有任何更新?是否可以使此方法仅返回字母数字值?它可以工作,但会产生奇怪的角色。我需要它们对于我的用例是人类可读的。@Dbloom要缩小使用的字符范围而不丢失信息,您需要执行第二次到编码(如十六进制或base64)的转换。请注意,这些文件根本不是“人类可读的”,但可以安全地以文本格式发送,也可以进行剪贴板操作等。
class CEncryption
{
public static string Encrypt(string strIn, string strKey)
{
string sbOut = String.Empty;
for (int i = 0; i < strIn.Length; i++)
{
sbOut += String.Format("{0:00}", strIn[i] ^ strKey[i % strKey.Length]);
}
return sbOut;
}
public static string Decrypt(string strIn, string strKey)
{
string sbOut = String.Empty;
for (int i = 0; i < strIn.Length; i += 2)
{
byte code = Convert.ToByte(strIn.Substring(i, 2));
sbOut += (char)(code ^ strKey[(i/2) % strKey.Length]);
}
return sbOut;
}
}
public static byte[] EncryptOrDecrypt(byte[] text, byte[] key)
{
byte[] xor = new byte[text.Length];
for (int i = 0; i < text.Length; i++)
{
xor[i] = (byte)(text[i] ^ key[i % key.Length]);
}
return xor;
}
static void Main(string[] args){
string input;
byte[] inputBytes;
string inputKey;
byte[] key;
do
{
input = System.Console.ReadLine();
inputBytes = Encoding.Unicode.GetBytes(input);
inputKey = System.Console.ReadLine();
key = Encoding.Unicode.GetBytes(inputKey);
//byte[] key = { 0, 0 }; if key is 0, encryption will not happen
byte[] encryptedBytes = EncryptOrDecrypt(inputBytes, key);
string encryptedStr = Encoding.Unicode.GetString(encryptedBytes);
byte[] decryptedBytes = EncryptOrDecrypt(encryptedBytes, key);
string decryptedStr = Encoding.Unicode.GetString(decryptedBytes);
System.Console.WriteLine("Encrypted string:");
System.Console.WriteLine(encryptedStr);
System.Console.WriteLine("Decrypted string:");
System.Console.WriteLine(decryptedStr);
} while (input != "-1" && inputKey != "-1");
//test:
//pavle
//23
//Encrypted string:
//BRD_W
//Decrypted string:
//pavle
}