0

我写了一个自定义加密的代码。这是一个挑战。现在由于某些原因它不起作用,例如:

加密(“abc”,“大家好”)返回:sdfhsjfjs 解密(“abc”,“sdfhsjfjs”)返回:diuifidu

    public int CountCharInStringAccordingToArray(string Text)
{
    int Count = 0;
    foreach (char x in Text)
    {
        Count++;
    }
    return Count - 1;
}

public int CountCharInString(string Text)
{
    int Count = 0;
    foreach (char x in Text)
    {
        Count++;
    }
    return Count;
}
public string Encrypt(string Key, string PlainText)
{
    int[] TempKey = new int[CountCharInString(Key)];
    int[] TempText = new int[CountCharInString(PlainText)];
    int[] EncryptedInt = new int[CountCharInString(PlainText)];
    char[] EncryptedChar = new char[CountCharInString(PlainText)];
    for (int i = 0; i < CountCharInStringAccordingToArray(Key); i++)
    {
        TempKey[i] = (int)Key[i];
    }
    for (int i = 0; i < CountCharInStringAccordingToArray(PlainText); i++)
    {
        TempText[i] = (int)PlainText[i];
    }
    int Index = 0;
    for (int i = 0; i < CountCharInStringAccordingToArray(PlainText); i++)
    {
        if (Index == CountCharInStringAccordingToArray(Key))
        {
            Index = 0;
        }
        EncryptedInt[i] = TempKey[Index] + TempText[i];
        Index++;
        EncryptedChar[i] = (char)EncryptedInt[i];
    }
    return new string(EncryptedChar);
}

public string Decrypt(string Key, string EncryptedText)
{
    int[] TempKey = new int[CountCharInString(Key)];
    int[] TempText = new int[CountCharInString(EncryptedText)];
    int[] DecryptedInt = new int[CountCharInString(EncryptedText)];
    char[] DecryptedChar = new char[CountCharInString(EncryptedText)];
    for (int i = 0; i < CountCharInStringAccordingToArray(Key); i++)
    {
        TempKey[i] = (int)Key[i];
    }
    for (int i = 0; i < CountCharInStringAccordingToArray(EncryptedText); i++)
    {
        TempText[i] = (int)EncryptedText[i];
    }
    int Index = 0;
    for (int i = 0; i < CountCharInStringAccordingToArray(EncryptedText); i++)
    {
        if (Index == CountCharInStringAccordingToArray(Key))
        {
            Index = 0;
        }
        DecryptedInt[i] = TempText[i] - TempKey[Index];
        Index++;
        DecryptedChar[i] = (char)DecryptedInt[i];
    }
    return new string(DecryptedChar);
}

我也知道字符串有一个长度属性,只是我忘记更正它。

4

2 回答 2

2

换行

return Count - 1;

CountCharInStringAccordingToArray

return Count;

您的代码的表示:

String That(String key, String text, int sign) {
    return new String(Enumerable.Range(0, text.Length).Select((x, i) => (char)(text.ToArray()[i]+sign*key.ToArray()[i%key.Length])).ToArray());
}

public String Encrypt(String key, String text) {
    return That(key, text, 1);
}

public String Decrypt(String key, String text) {
    return That(key, text, -1);
}

它的工作原理很简单。看图:

EgB5n.jpg

中的字符key被重复使用以添加到text并产生一个认为已加密的序列。解密只是通过减法进行的反向操作。

一个字节可以存储的最大值是,但可见字符0x0ff的最大值是,即和。0x7e~0x7e+0x7e=0xfc

所以只要你的key和text在可见字符范围内,就不会造成溢出。也就是说,您可以正确地将加密序列解密为原始序列。

于 2013-03-28T03:53:21.767 回答
0

获取字符串长度和输出字符串

使用系统;

class MainClass {   
  public static void Main() {   
    string str1 = "ABCDEabcde1234567890";   

    Console.WriteLine("str1: " + str1); 

    Console.WriteLine("Length of str1: " + str1.Length);   
  }
}
于 2013-03-28T03:11:35.403 回答