加密问题 [英] a problem with encryption

查看:51
本文介绍了加密问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是问题:我没有得到编码时需要的输出和使用rijndael alghoritm解码数据的


查看代码并查看问题所在实际上是:


请将此代码粘贴到Visual Studio中并编译并运行它;所以

你可以看到实际问题是什么。


谢谢。


代码:


使用System;

使用System.IO;

使用System.Text;

使用System.Security。密码学;

命名空间ConsoleApplication1

{

class MyMainClass

{

public static void Main()

{

string original =" Original string" ;;

string roundtrip;

ASCIIEncoding textConverter = new ASCIIEncoding();

RijndaelManaged myRijndael = new RijndaelManaged();

byte [] fromEncrypt;

byte [] encrypted;

byte [] toEncrypt;

byte [] key;

byte [] IV;

//创建一个新密钥和初始化向量。

myRijndael.GenerateKey();

myRijndael.GenerateIV();

//获取密钥和IV。

key = myRijndael.Key;

IV = myRijndael.IV;

//获取加密器。

ICryptoTransform encryptor = myRijndael.CreateEncryptor(key,IV);

//加密数据。

MemoryStream msEncrypt = new MemoryStream();

CryptoStream csEncrypt = new CryptoStream(msEncrypt,encryptor,
CryptoStreamMode.Write);

//将数据转换为字节数组。

toEncrypt = textConverter.GetBytes(original);

//将所有数据写入加密流并刷新它。

csEncrypt.Write(toEncrypt,0,toEncrypt.Length) ;

csEncrypt.FlushFinalBlock();

//获取加密的字节数组。

encrypted = msEncrypt.ToArray();


//这里我通过网络流发送数据

//创建通过tcp网络发送的字节数组

byte [] finalized = new byte [key.Length + IV.Length + encrypted.Length];

//将所有值合并为单字节数组

key.CopyTo(finalized,0);

IV.CopyTo(最终确定,32);

encrypted.CopyTo(最终确定,48);

//这里是通过网络发送数组的tcp代码。它工作正常,

并没有问题。

//为了simplicitiy'的缘故,这里我只是模拟一个新的应用程序

使用从第一个应用程序获得的值。

//模拟新应用程序

//创建将在解密过程中使用的值并传递给它/>
通过网络

byte [] key1 =新字节[32];

byte [] IV1 =新字节[16];

byte [] encrypted1 = new byte [finalized.Length-48];

//从传递的字节数组中读取所有值,然后正确地划分它们。

for(int i = 0; i< 32; i ++)

{

key1 [i] = finalized [i];

}

for(int i = 32; i< 48; i ++)

{

IV1 [i-32] = finalized [i]; < ($ i $ 48; i< finalized.Length; i ++)

{

encrypted1 [
} i-48] =已完成[i];

}

//现在使用值来获得结果:

//获取解密器使用相同的密钥和IV加密器。

ICryptoTransform decryptor = myRijndael.CreateDecryptor(key1,IV1);

//现在使用解密器解密以前加密的消息

MemoryStream msDecrypt = new MemoryStream(encrypted1);

CryptoStream csDecrypt = new CryptoStream(msDecrypt,decryptor,

CryptoStreamMode.Read);

fromEncrypt = new byte [encrypted1.Length];

//从加密流中读取数据。

csDecrypt.Read(fromEncrypt,0,fromEncrypt.Length);

//将字节数组转换回字符串。

roundtrip = textConverter.GetString(fromEncrypt);

//显示原始数据和解密数据以查看实际

问题的位置:

Console.WriteLine(" Original string:{0}",original +" _");

Console.WriteLine(" String I got to another application:{0}",roundtrip +

" _");

//你猜怎么着!结果字符串最后有一些虚拟内容,它是

//而不是我编码的数据。实际上它就在那里,但我真的不想要最后的结果。我放了_签名只是为了看到我收到的数据有一个

的问题。

}

}

}

This is the problem: I do not get the output I need when encoding and
decoding data using rijndael alghoritm.
Look at the code and see what the problem is actually:

Please paste this code into your Visual Studio and compile it + run it; so
you can see what the actual problem is.

Thanks.

code:

using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;
namespace ConsoleApplication1
{
class MyMainClass
{
public static void Main()
{
string original = "Original string";
string roundtrip;
ASCIIEncoding textConverter = new ASCIIEncoding();
RijndaelManaged myRijndael = new RijndaelManaged();
byte[] fromEncrypt;
byte[] encrypted;
byte[] toEncrypt;
byte[] key;
byte[] IV;
//Create a new key and initialization vector.
myRijndael.GenerateKey();
myRijndael.GenerateIV();
//Get the key and IV.
key = myRijndael.Key;
IV = myRijndael.IV;
//Get an encryptor.
ICryptoTransform encryptor = myRijndael.CreateEncryptor(key, IV);
//Encrypt the data.
MemoryStream msEncrypt = new MemoryStream();
CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor,
CryptoStreamMode.Write);
//Convert the data to a byte array.
toEncrypt = textConverter.GetBytes(original);
//Write all data to the crypto stream and flush it.
csEncrypt.Write(toEncrypt, 0, toEncrypt.Length);
csEncrypt.FlushFinalBlock();
//Get encrypted array of bytes.
encrypted = msEncrypt.ToArray();

//Here I send data trough network stream
//create byte array to be sent trough tcp network
byte[] finalized = new byte[key.Length+IV.Length+encrypted.Length];
//merge all values into single byte array
key.CopyTo(finalized,0);
IV.CopyTo(finalized,32);
encrypted.CopyTo(finalized,48);
//here goes tcp code with sending the array trough network. it works fine,
and is no problem.
//For simplicitiy''s sake, here i''ll just simulate a new application that
uses values it got from the first application.
//SIMULATED NEW APPLICATION
//Create values that will be used in decryption process and that are passed
trough network
byte[] key1 = new byte[32];
byte[] IV1 = new byte[16];
byte[] encrypted1 = new byte[finalized.Length-48];
//read all values from the passed byte array and divid those correctly.
for (int i=0; i<32; i++)
{
key1[i]=finalized[i];
}
for (int i=32; i<48; i++)
{
IV1[i-32]=finalized[i];
}
for (int i=48; i<finalized.Length; i++)
{
encrypted1[i-48]=finalized[i];
}
//now use values to get the result:
//Get a decryptor that uses the same key and IV as the encryptor.
ICryptoTransform decryptor = myRijndael.CreateDecryptor(key1, IV1);
//Now decrypt the previously encrypted message using the decryptor
MemoryStream msDecrypt = new MemoryStream(encrypted1);
CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor,
CryptoStreamMode.Read);
fromEncrypt = new byte[encrypted1.Length];
//Read the data out of the crypto stream.
csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);
//Convert the byte array back into a string.
roundtrip = textConverter.GetString(fromEncrypt);
//Display the original data and the decrypted data to see where the actual
problem is:
Console.WriteLine("Original string: {0}", original + "_");
Console.WriteLine("String I got to another application: {0}", roundtrip +
"_");
//Guess what! The result string has some dummy stuff at the end and it is
//just not the data I encoded. It is actually there, but I really don''t want
//that sh*t at the end. I placed "_" sign just to see that there is a
problem with data I got.
}
}
}

推荐答案

crawlerxp< ny ****** @ email.htnet.hr>写道:
crawlerxp <ny******@email.htnet.hr> wrote:
这就是问题:我在编码和使用rijndael alghoritm解码数据时没有得到我需要的输出。
查看代码,看看问题是什么实际上:

请将此代码粘贴到Visual Studio中并编译并运行它;所以
你可以看到实际问题是什么。
This is the problem: I do not get the output I need when encoding and
decoding data using rijndael alghoritm.
Look at the code and see what the problem is actually:

Please paste this code into your Visual Studio and compile it + run it; so
you can see what the actual problem is.




< snip>


问题在于你假设解密的大小将是加密大小相同的

- 它不是。你实际上只读了15个字节

(而且,你假设他们都会一次性阅读,这是个坏主意)但是将编码传递给16字节长的缓冲区。


*总是*使用Stream.Read的返回值。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复小组,请不要给我发邮件



<snip>

The problem is that you''re assuming the decrypted size will be the same
as the encrypted size - it''s not. You''re only actually reading 15 bytes
(and moreover, you''re assuming they''ll all be read in one go, which is
a bad idea) but passing the encoding a buffer 16 bytes long.

*Always* use the return value of Stream.Read.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

crawlerxp< ny ****** @ email.htnet.hr>写道:
crawlerxp <ny******@email.htnet.hr> wrote:
这就是问题:我在编码和使用rijndael alghoritm解码数据时没有得到我需要的输出。
查看代码,看看问题是什么实际上:

请将此代码粘贴到Visual Studio中并编译并运行它;所以
你可以看到实际问题是什么。
This is the problem: I do not get the output I need when encoding and
decoding data using rijndael alghoritm.
Look at the code and see what the problem is actually:

Please paste this code into your Visual Studio and compile it + run it; so
you can see what the actual problem is.




< snip>


问题在于你假设解密的大小将是加密大小相同的

- 它不是。你实际上只读了15个字节

(而且,你假设他们都会一次性阅读,这是个坏主意)但是将编码传递给16字节长的缓冲区。


*总是*使用Stream.Read的返回值。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复小组,请不要给我发邮件



<snip>

The problem is that you''re assuming the decrypted size will be the same
as the encrypted size - it''s not. You''re only actually reading 15 bytes
(and moreover, you''re assuming they''ll all be read in one go, which is
a bad idea) but passing the encoding a buffer 16 bytes long.

*Always* use the return value of Stream.Read.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

>问题是你假设解密的大小与加密大小相同

- 它不是。你实际上只读了15个字节

(而且,你假设他们都会一次性阅读,这是一个不好的想法。但是传递编码一个16字节长的缓冲区。
>The problem is that you''re assuming the decrypted size will be the same
as the encrypted size - it''s not. You''re only actually reading 15 bytes
(and moreover, you''re assuming they''ll all be read in one go, which is a
bad idea) but passing the encoding a buffer 16 bytes long.
*总是*使用Stream.Read的返回值。
*Always* use the return value of Stream.Read.




嗯,问题不在于网络操作和数据发送。它是在

解密数据。

问题是我总是得到n到16个字节用零填充。


所以我刚换了一行:


roundtrip = textConverter.GetString(fromEncrypt);


with:

往返=

textConverter.GetString(fromEncrypt).TrimEnd(Conve rt.ToChar(0));


这样我总能得到原始数据我已经加密了。


谢谢顺便说一句。


我有另一个问题:

什么时候我正在通过网络流将这些数据从客户端发送到服务器,

我总是创建大到足以接受来自客户端应用程序的可能数据的字节类型数组。 />
我是否必须始终创建它足够大以支持任何可能的数据

大小,或者我可以读取块中的所有内容然后将其合并为单个
$ b例如$ b字节数组。


现在就是这样做的:


客户代码:

(这是从主代码剪切的连接线程代码:)


尝试

{

this.hostName = this.textBox2.Text;

TcpClient client = new TcpClient(hostName,portNum);


NetworkStream ns = client.GetStream() ;


//响应缓冲区的大小

byte [] bytes = new byte [1024];


//使用自定义加密类加密给定数据

bit256_RijndaelEnCryptorC enkripted = new bit256_RijndaelEnCryptorC();


//从文本框加密字符串

encrypted.EnCrypt(this.textBox1.Text);


//创建数据发送缓冲区

byte [] byteTime = new byte [encrypted.ReleaseEnCrypted).Length];


//填充它

byteTime = encrypted.ReleaseEnCrypted();


//通过流写入

ns.Write(byteTime,0,byteTime.Length);


//收到回复

int bytesRead = ns.Read(bytes,0,by tes.Length);


client.Close();


}


服务器代码:


TcpClient client = listener.AcceptTcpClient();


NetworkStream ns = client.GetStream();


//传入数据的缓冲区

byte [] bytes = new byte [4096];


//从ns读取数据

int bytesRead = ns.Read(bytes,0,bytes.Length);


//在工作缓冲区中输入数据

byte [] return = new byte [bytesRead];


for(int u = 0; U< bytesRead; u ++)

{

返回[u] =字节[u];

}


//创建要在加密过程中使用的数据变量

byte [] key = new byte [32];

byte [] IV = new byte [16];

byte [] encrypted = new byte [returned.Length-48];


//从传入流中删除可用数据

for(int i = 0; i< 32; i ++)

{

key [i] = return [i];

}

for(int i = 32; i< 48; i ++)

{

IV [i-32] = return [i];

}

for(int i = 48; i< returned.Length; i ++)

{

加密[i-48] =返回[i];

}


//创建自定义加密(此次解密)类

bit256_RijndaelEnCryptorC dekripted = new

bit256_RijndaelEnCryptorC(key,IV,encrypted);

encrypted.DeCrypt();


string result = encrypted.ReleaseDeCrypted();

byte [] byteTime = Encoding.ASCII.GetBytes(" se rver执行了

操作!);


尝试

{

ns.Write( byteTime,0,byteTime.Length);

ns.Close();

}

client.Close();


***通过开发人员指南发送 http://www.developersdex.com ***

不要只是参加USENET ......获得奖励!



Well, the problem was not in network operations and data send. It was in
decrypted data.
The problem was I did always get n to 16 bytes filled with zeros.

So I''ve just swapped line:

roundtrip = textConverter.GetString(fromEncrypt);

with:

roundtrip =
textConverter.GetString(fromEncrypt).TrimEnd(Conve rt.ToChar(0));

That way I always get the original data I''ve encrypted.

Thanks btw.

I have another question:
When I''m sending this data trough network stream from client to server,
I always create byte type array big enough to accept possible data from
the client application.
Do I have to always create it big enough to support any possible data
size, or I can read everything in blocks and then merge it to a single
byte array for example.

This is how it is done by now:

client code:
(this is a connection thread code cut from the main code:)

try
{
this.hostName = this.textBox2.Text;
TcpClient client = new TcpClient(hostName, portNum);

NetworkStream ns = client.GetStream();

//size of response buffer
byte[] bytes = new byte[1024];

//using custom encryption class to encrypt given data
bit256_RijndaelEnCryptorC enkripted = new bit256_RijndaelEnCryptorC();

//encrypt string from the textbox
encrypted.EnCrypt(this.textBox1.Text);

//create data-to-be-sent buffer
byte[] byteTime = new byte[encrypted.ReleaseEnCrypted).Length];

//fill it
byteTime = encrypted.ReleaseEnCrypted();

//write it trough stream
ns.Write(byteTime, 0, byteTime.Length);

//receive a response
int bytesRead = ns.Read(bytes, 0, bytes.Length);

client.Close();

}

server code:

TcpClient client = listener.AcceptTcpClient();

NetworkStream ns = client.GetStream();

//buffer for incoming data
byte[] bytes = new byte[4096];

//read data from the ns
int bytesRead = ns.Read(bytes, 0, bytes.Length);

//input data in a work buffer
byte[] returned = new byte[bytesRead];

for (int u=0; u<bytesRead; u++)
{
returned[u]=bytes[u];
}

//create data variables to be used in encryption process
byte[] key = new byte[32];
byte[] IV = new byte[16];
byte[] encrypted = new byte[returned.Length-48];

//strip usable data from the incoming stream
for (int i=0; i<32; i++)
{
key[i]=returned[i];
}
for (int i=32; i<48; i++)
{
IV[i-32]=returned[i];
}
for (int i=48; i<returned.Length; i++)
{
encrypted[i-48]=returned[i];
}

//create a custom encryption (this time decryption) class
bit256_RijndaelEnCryptorC dekripted = new
bit256_RijndaelEnCryptorC(key,IV,encrypted);
encrypted.DeCrypt();

string result = encrypted.ReleaseDeCrypted();

byte[] byteTime = Encoding.ASCII.GetBytes("server performed
operations!");

try
{
ns.Write(byteTime, 0, byteTime.Length);
ns.Close();
}
client.Close();

*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!


这篇关于加密问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆