沿bina函数的长度重复Encrypt函数

时间:2019-04-13 13:54:42

标签: c#

我想在bina的整个长度上重复Encrypt函数/怎么做?

private void carbonFiberButton11_Click_1(object sender, EventArgs e)
        {
            textBox1.Text = PairConcat(Encrypt(), bina());
        }

    public static string PairConcat(string Encrypt, string bina)
    {
        StringBuilder result = new StringBuilder();
        int i = 0;
        for(; i<Encrypt.Length & i < bina.Length; i++)
        {
            result.Append(Encrypt[i].ToString());
            result.Append(bina[i].ToString());
        }
        result.Append(Encrypt.Substring(i));
        result.Append(bina.Substring(i));
        return result.ToString();

    }

例如:

string bina = "1234567";
string Encrypt = "abcdefg";
textbox1.text = 1a2b3c4d.. ;but it is doesn't works if I have different length:

string bina = "12345"
string Encrypt = "abc"
textbox1.text = 1a2b3c45 , but I need - 1a2b3c4a5b. 

加密功能:

string Encrypt() //random to binary
        {
            var encrypt = textBox4.Text;

            StringBuilder binary = new StringBuilder();           
            for (int i = 0; i < encrypt.Length; i++)
            {
                binary.Append(Convert.ToString(encrypt[i], 2).PadLeft(8, '0'));
            }
            return binary.ToString();

        }

我不知道该怎么办/请帮忙

1 个答案:

答案 0 :(得分:0)

只需使Encrypt字符串与bina字符串一样长。

// Calculate smallest multiple of Encrypt.Length at least as long as bina.Length
int lb = bina.Length;
int le = Encrypt.Length;
int bufferLength = (lb + le - 1) / le * le;

var sb = new StringBuilder(Encrypt, bufferLength);
while (sb.Length < lb) {
    sb.Append(Encrypt);
}
Encrypt = sb.ToString();

string result = String.Join("", bina.Zip(Encrypt, (a, b) => a.ToString() + b));

LINQ Zip方法通过提供两个序列中的项对直到一个序列结束来组合2个序列。这里的序列由字符组成。

如果不需要调整内部缓冲区的大小,则StringBuilder的工作效率最高。我用整数算术来计算。