自定义加密#2

时间:2013-03-28 03:07:15

标签: c# encryption

我写了一个自定义加密的代码。这是一个挑战。现在它由于某些原因而无效:

加密(“abc”,“大家好”) 返回:sdfhsjfjs 解密(“abc”,“sdfhsjfjs”) Retruns: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);
}

我也知道字符串有长度属性,我忘了纠正它。

2 个答案:

答案 0 :(得分: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,即~和{{ 1}}。

所以只要您的密钥和文字都在可见字符范围内,就不会导致溢出。也就是说,您可以正确地将加密序列解密为原始序列。

答案 1 :(得分:0)

Get string length and output string

使用System;

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

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

    Console.WriteLine("Length of str1: " + str1.Length);   
  }
}