- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我必须编写一个对完整字节进行操作的 Vigenere 加密/解密函数(通过 tcp 加密和发送文件,然后在另一端解密)。我的加密功能似乎在工作(或多或少,没有解密功能无法真正测试它)。
这是加密函数的代码:
public static Byte[] encryptByteVigenere(Byte[] plaintext, string key)
{
Byte[] result= new Byte[plaintext.Length];
key = key.Trim().ToUpper();
int keyIndex = 0;
int keylength = key.Length;
for (int i = 0; i < plaintext.Length; i++)
{
keyIndex = keyIndex % keylength;
int shift = (int)key[keyIndex] - 65;
result[i] = (byte)(((int)plaintext[i] + shift) % 256);
keyIndex++;
}
return result;
}
然而,解密函数,即使以几乎相同的方式编写,也会导致错误。“试图除以零。”
解密函数代码:
public static Byte[] decryptByteVigenere(Byte[] ciphertext, string key)
{
Byte[] result = new Byte[ciphertext.Length];
key = key.Trim().ToUpper();
int keyIndex = 0;
int keylength = key.Length;
for (int i = 0; i < ciphertext.Length; i++)
{
keyIndex = keyIndex % keylength;
int shift = (int)key[keyIndex] - 65;
result[i]= (byte)(((int)ciphertext[i] + 256 - shift) % 256);
keyIndex++;
}
return result;
}
该行的错误点keyIndex = keyIndex % keylength;但令我感到奇怪的是,第一个函数中的代码几乎相同,而且似乎没有造成任何麻烦。我在收到的字段上测试它,它没有加密就正确到达。谁能帮我解决这个问题?
编辑:正在使用解密函数代码的方法/线程:
public void fileListenThread()
{
try
{
fileServer.Start();
String receivedFileName = "test.dat";
String key = (textKlucz.Text).ToUpper();
while (true)
{
fileClient = fileServer.AcceptTcpClient();
NetworkStream streamFileServer = fileClient.GetStream();
int thisRead = 0;
int blockSize = 1024;
Byte[] dataByte = new Byte[blockSize];
Byte[] dataByteDecrypted = new Byte[blockSize];
FileStream fileStream = new FileStream(receivedFileName, FileMode.Create);
while (true)
{
thisRead = streamFileServer.Read(dataByte, 0, blockSize);
dataByteDecrypted = Program.decryptByteVigenere(dataByte, key);
fileStream.Write(dataByteDecrypted, 0, thisRead);
if (thisRead == 0)
break;
}
fileStream.Close();
}
}
catch (SocketException e)
{
MessageBox.Show("SocketException: " + e, "Wystąpił wyjątek", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
最佳答案
好的,问题确实是发送/接收方法,而不是函数本身。我仍然不知道是什么导致了这个问题,但是重写这些函数很有帮助。感谢您的意见!
我把它留在这里,以防将来有人需要这样的功能……尽管这是一件微不足道的事情。
干杯。
关于c# - Byte Vigenere Cipher,解密错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13369178/
我有以下代码: foreach (byte b in bytes) { byte inv = byte.MaxValue - b; // Add the new value to a
我需要从这个文本文件source.txt中读取内容并将内容反向写入这个文本文件destination.txt。读取和写入必须使用逐字节完成! 我使用 BufferedReader 和 Buffered
我需要存储大量 RGB 颜色对象。对于某些常见用途,这些占用了我的应用程序总内存的 8% 到 12%。我目前将其定义如下: class MyColor { byte red; byte green;
我有一个由字节数组表示的整数。 byte[] result = getResult(); resultInt1 = Integer.parseInt(Bytes.toString(result));/
我正在尝试使用 Rusoto 库调用 AWS Lambda 函数。该请求有一个 JSON 编码的有效负载,我目前将其作为一个字符串,但该库为此坚持使用 bytes::bytes::Bytes 结构。我
我正在尝试基于 Tokio's example 编写一个 TCP 服务器. 当我尝试发送缓冲区时,编译器返回错误 0277。 我的代码:(playground) extern crate tokio;
我知道我可以通过 IList 进行枚举,例如: public byte[] ConvertToByteArray(IList> list) { IList newList = new List
考虑这样一个文本文件: Some text here. --- More text another line. --- Third part of text. 我想把它分成三部分,用---分隔符分开。
如果我有一个字节变量:byte b = 0; 为什么以下工作: b++; b += 1; // compiles ...但这不是吗? b = b + 1; // compile er
我有一个简单的字节数组,我想从中获取颜色。我的计划是用红色表示三位,绿色表示三位,蓝色表示两位。 8 位。 我认为颜色是正确的: 如有错误请指正 byte[] colours = new byte[
我的目标是比较两个字节数组中的两个字符串值。它实际上需要创建两个新的字符串对象才能使用 contains 方法。是选择正确还是有什么办法可以使用优化方式而不使用新的关键字。 if(new String
我正在使用github.com/tarm/serial来连接一些串行仪器。在开发过程中,我使用/dev/ttyp0和/dev/ptyp0对,其中go进程连接到一个,我使用screen连接到另一个。我编
好的,所以如果一个字节是 8 位,那么半字节就是 4 位。并且您可以将四分之一字节作为 2 位(尽管我想,如果有的话,它会被称为双位)。 虽然这是一致的,但如果我使用这个词,有人会感到困惑(或惊讶)吗
我在解释文件时遇到问题。文件构建如下: "name"-@-"date"-@-"author"-@-"signature" 签名是一个字节数组。当我读回文件时,我将其解析为 String 并拆分它: m
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 10 年前。 Improve thi
Java 让我很难过,因为它需要 ArrayList 的包装类秒。我将如何添加 byte[]到 ArrayList ? 最佳答案 LOL 认为我必须包装所有东西。 ArrayList作品。谢谢一晒。
我有一个 16 字节的 md5 散列,我需要使用 XOR 将其“折叠”成 4 字节数据:{1st 4 bytes} XOR {2nd 4 bytes} XOR {3rd 4 bytes} XOR {4
我正在学习SMSC smc91cx驱动代码,我学习了如何根据Application Note 9-6的说明编写smc91c111网卡的测试代码。 .我无法理解“传输数据包”下的以下说明: Write
我必须附加(可变数量的)字节数组。集合似乎只适用于包装类,即 Byte。大约 20 小时后,我想到了这个,并且它有效,但我想知道它是否可以改进(添加到列表,但欢迎任何其他改进建议:),即 Collec
我有两个基本相同的操作: insert_bytes(from, count) delete_bytes(start, stop) -> delete_bytes(from, count) insert
我是一名优秀的程序员,十分优秀!