简体   繁体   English

使用C#从event.message.text返回的字符串进行3des解密

[英]3des decryption in c# from a string returned by event.message.text

How do I properly convert the input text so that it decrypts properly I get an error saying 我如何正确转换输入文本以便正确解密,我收到一条错误消息:

Error CS1503 Argument 1: cannot convert from 'string' to 'char[]' 错误CS1503参数1:无法从“字符串”转换为“字符[]”

whats the proper way to parse the first parameter of TransfromFinalBlock of the decryption interface? 解析解密接口的TransfromFinalBlock的第一个参数的正确方法是什么? thanks 谢谢

private static void Bot_OnMessage4(object sender, MessageEventArgs e)
    {
        ICryptoTransform decrypt = des.CreateDecryptor();
        String[] dd = new String[1];
        dd[1]= utf8.GetString(decrypt.TransformFinalBlock(utf8.GetBytes(**e.Message.Text**, 0, utf8.GetBytes(e.Message.Text).Length)));
        Bot.SendTextMessageAsync(e.Message.Chat.Id, "message to decrypt is  \n" + e.Message.Text );
        Bot.SendTextMessageAsync(e.Message.Chat.Id, "decrypted message is  \n" + dd[1]);

You seem to be already trying to fix your issues, correctly blaming your input: 您似乎已经在尝试解决问题,正确地归咎于您的意见:

utf8.GetBytes(e.Message.Text, 0, utf8.GetBytes(e.Message.Text).Length)

The problem is that modern crypto acts on bytes, and returns a ciphertext where each byte can have any value . 问题是现代加密算法作用于字节,并返回一个密文,其中每个字节可以具有任何值 This means that the ciphertext may also contain bytes that do not represent any character . 这意味着密文也可能包含不代表任何字符的字节。 In other words, the ciphertext is not really text . 换句话说,密文不是真正的文本

So there are two things you can do about it: 因此,您可以做两件事:

  1. keep handling the ciphertext as bytes or byte streams; 继续将密文作为字节或字节流处理;
  2. encode the ciphertext to a binary encoding such as base 64 and decode before you decrypt. 将密文编码为二进制编码(例如base 64)并在解密之前解码。

Keeping the ciphertext in binary should be preferred, but it is not always possible if the interface with the rest of the system (the Message in this case) requires a textual string. 最好将密文保留为二进制,但是如果与系统其余部分的接口(在本例中为Message )需要文本字符串,则并非总是可能的。


Your error doesn't have anything to do about this, but if you follow above advice you don't need utf8.GetBytes anymore. 您的错误与此无关,但是,如果您遵循上述建议,则不再需要utf8.GetBytes

You should use the single argument GetBytes method to get rid of the compile error, but your code would still fail during runtime (although it could succeed because the right bytes are generated by chance). 您应该使用单个参数GetBytes方法来摆脱编译错误,但是您的代码在运行时仍然会失败(尽管它成功,因为偶然生成了正确的字节)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM