数据解密问题 [英] Data decryption issue

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

问题描述

我正在尝试使用一些加密/解密程序来处理我的数据,而且加密效果很好,我一直在进入

消息填充无效,无法删除关于解密片。

从我能看到的一切,我在这里正确地做事我的代码是

如下:


private const string PassStr =" MyPrivateKey";

private static readonly byte [] PassSalt = new byte [] {Byte Byte

Byte Byte Byte};

private static readonly已启用en;


静态加密()

{

en = new Enabled() ;

en.enabled = true;

}


private static byte [] TransDown(char [] TransString)

{

byte [] ResultStr = new byte [TransString.Length];


for(int i = 0; i< TransString.Length; i ++)

ResultStr [i] = Convert.ToByte(TransString [i]);


返回ResultStr;

}


private static char [] TransUp(Byte [] TransString)

{

char [] ResultStr = new char [TransString.Length];


for(int i = 0; i< TransString.Length; i ++)

ResultStr [i] = Convert.ToChar(TransString [i]);


返回ResultStr;

}


private static char [] TransDown(String TransString)

{

char [] ResultStr = new char [TransString.Length] ;


for(int i = 0;我< TransString.Length; i ++)

ResultStr [i] = Convert.ToChar(TransString [i]);


返回ResultStr;

}


public static char [] Encrypt(char [] InputString)

{

if(en.enabled)

{

if((InputString!= null)&&(InputString.Length 0))

{

char [] ResultStr = new char [32];

MemoryStream memoryStream;

CryptoStream cryptoStream;

RijndaelManaged rijndael = new RijndaelManaged();

ResultStr = TransDown(PassStr);

rijndael.Key = TransDown(ResultStr);

rijndael.IV = PassSalt;

rijndael.Padding = PaddingMode.PKCS7;

rijndael.Mode = CipherMode.CBC;

memoryStream = new MemoryStream();

cryptoStream = new CryptoStream(memoryStream,
rijndael.CreateEncryptor(),CryptoStreamMode.Write);

cryptoStream.Write(TransDown(InputString),0,

InputString.Length);

cryptoStream.Flus hFinalBlock();

// ResultStr = TransUp(memoryStream.ToArray());

cryptoStream.Close();

返回TransUp( memoryStream.ToArray());

}

else

返回TransDown(" NULL");

}

其他

返回TransDown(未授权);

}


public static char [] Decrypt(char [] InputString)

{

if(en.enabled)

{

if((InputString!= null)&& (InputString.Length 0))

{

char [] ResultStr = new char [32];

byte [] ResultByte = new byte [InputString.Length];

MemoryStream memoryStream = new

MemoryStream(TransDown(InputString));

memoryStream.Position = 0;

CryptoStream cryptoStream;

RijndaelManaged rijndael = new RijndaelManaged();

ResultStr = TransDown(PassStr);

rijndael。 Key = TransDown(ResultStr);

rijndael.IV = PassSalt;

rijndael.Padding = PaddingMode.PKCS7;

rijndael.Mode = CipherMode .CBC;

cryptoStream = new CryptoStream(memoryStream,
rijndael.CreateDecryptor(),CryptoStreamMode.Read);

cryptoStream.Read(ResultByte ,0,InputString.Length);

cryptoStream.Close();

返回TransUp(ResultByte);

}

其他

返回TransDown("");

}

else

返回TransDown( 未授权&qu ot;);

}


我得到的信息是:


消息6522,等级16,状态2,第1行

在执行用户定义的例程期间发生.NET Framework错误或

聚合''Decrypt'':

系统.Security.Cryptography.CryptographicExceptio n:填充无效,

无法删除。

System.Security.Cryptography.CryptographicExceptio n:

at System.Security.Cryptography.RijndaelManagedTransf orm.DecryptData(Byte []

inputBuffer,Int32 inputOffset,Int32 inputCount,Byte []& outputBuffer,

Int32 outputOffset,PaddingMode paddingMode,Boolean fLast)

at

System.Security.Cryptography.RijndaelManagedTransf orm.TransformFinalBlock(Byte []

inputBuffer,Int32 inputOffset,Int32 inputCount)

在System.Security.Cryptography.CryptoStream.Read(Byt e []缓冲区,Int32

偏移,Int32计数)

在SQLEncryption.Encryption.Decrypt(Char [] InputString)


谁能告诉我哪里出错了?我不知道密码学

组件是否足以让我知道我做错了什么。谢谢。

I''m trying to get some encryption/decryption routines going to take care of
my data, and while the encryption is working great, I keep running into the
message "Padding is invalid and cannot be removed" on the decryption piece.
From everything I can see, I am doing things correctly here My code is as
follows:

private const string PassStr = "MyPrivateKey";
private static readonly byte[] PassSalt = new byte[] { Byte Byte
Byte Byte Byte};
private static readonly Enabled en;

static Encryption()
{
en = new Enabled();
en.enabled = true;
}

private static byte[] TransDown(char[] TransString)
{
byte[] ResultStr = new byte[TransString.Length];

for (int i = 0; i < TransString.Length; i++)
ResultStr[i] = Convert.ToByte(TransString[i]);

return ResultStr;
}

private static char[] TransUp(Byte[] TransString)
{
char[] ResultStr = new char[TransString.Length];

for (int i = 0; i < TransString.Length; i++)
ResultStr[i] = Convert.ToChar(TransString[i]);

return ResultStr;
}

private static char[] TransDown(String TransString)
{
char[] ResultStr = new char[TransString.Length];

for (int i = 0; i < TransString.Length; i++)
ResultStr[i] = Convert.ToChar(TransString[i]);

return ResultStr;
}

public static char[] Encrypt(char[] InputString)
{
if (en.enabled)
{
if ((InputString != null) && (InputString.Length 0))
{
char[] ResultStr = new char[32];
MemoryStream memoryStream;
CryptoStream cryptoStream;
RijndaelManaged rijndael = new RijndaelManaged();
ResultStr = TransDown(PassStr);
rijndael.Key = TransDown(ResultStr);
rijndael.IV = PassSalt;
rijndael.Padding = PaddingMode.PKCS7;
rijndael.Mode = CipherMode.CBC;
memoryStream = new MemoryStream();
cryptoStream = new CryptoStream(memoryStream,
rijndael.CreateEncryptor(), CryptoStreamMode.Write);
cryptoStream.Write(TransDown(InputString), 0,
InputString.Length);
cryptoStream.FlushFinalBlock();
//ResultStr = TransUp(memoryStream.ToArray());
cryptoStream.Close();
return TransUp(memoryStream.ToArray());
}
else
return TransDown("NULL");
}
else
return TransDown("Not Authorized");
}

public static char[] Decrypt(char[] InputString)
{
if (en.enabled)
{
if ((InputString != null) && (InputString.Length 0))
{
char[] ResultStr = new char[32];
byte[] ResultByte = new byte[InputString.Length];
MemoryStream memoryStream = new
MemoryStream(TransDown(InputString));
memoryStream.Position = 0;
CryptoStream cryptoStream;
RijndaelManaged rijndael = new RijndaelManaged();
ResultStr = TransDown(PassStr);
rijndael.Key = TransDown(ResultStr);
rijndael.IV = PassSalt;
rijndael.Padding = PaddingMode.PKCS7;
rijndael.Mode = CipherMode.CBC;
cryptoStream = new CryptoStream(memoryStream,
rijndael.CreateDecryptor(), CryptoStreamMode.Read);
cryptoStream.Read(ResultByte, 0, InputString.Length);
cryptoStream.Close();
return TransUp(ResultByte);
}
else
return TransDown(" ") ;
}
else
return TransDown("Not Authorized");
}

The message I am getting is:

Msg 6522, Level 16, State 2, Line 1
A .NET Framework error occurred during execution of user defined routine or
aggregate ''Decrypt'':
System.Security.Cryptography.CryptographicExceptio n: Padding is invalid and
cannot be removed.
System.Security.Cryptography.CryptographicExceptio n:
at System.Security.Cryptography.RijndaelManagedTransf orm.DecryptData(Byte[]
inputBuffer, Int32 inputOffset, Int32 inputCount, Byte[]& outputBuffer,
Int32 outputOffset, PaddingMode paddingMode, Boolean fLast)
at
System.Security.Cryptography.RijndaelManagedTransf orm.TransformFinalBlock(Byte[]
inputBuffer, Int32 inputOffset, Int32 inputCount)
at System.Security.Cryptography.CryptoStream.Read(Byt e[] buffer, Int32
offset, Int32 count)
at SQLEncryption.Encryption.Decrypt(Char[] InputString)

Can anyone tell me where I am going wrong? I don''t know the cryptography
components well enough to have a clue about what I am doing wrong. Thanks.

推荐答案

2008年8月1日星期五15:33:10 -0700,Tom Andrecht

< to **********@nospam.dmacorporation.comwrote:
On Fri, 01 Aug 2008 15:33:10 -0700, Tom Andrecht
<to**********@nospam.dmacorporation.comwrote:

我正试图让一些加密/解密程序得到照顾



我的数据,虽然加密效果很好,但我一直在运行



消息" ;填充无效,无法删除关于解密

件。

从我能看到的一切,我在这里正确地做事我的代码是

as

如下:
I''m trying to get some encryption/decryption routines going to take care
of
my data, and while the encryption is working great, I keep running into
the
message "Padding is invalid and cannot be removed" on the decryption
piece.
From everything I can see, I am doing things correctly here My code is
as
follows:



所有应有的尊重,我不知道如果你不能成功解密

数据,你可以断言加密工作得很好。难道你的解密是正确的,但是因为加密是错误的,所以解密步骤仍然失败了吗? :)


无论如何,我不是你加密/解密的专家。但是

我注意到你似乎在互换使用字节和字符,

这通常是一个坏主意。据我所知,你所做的转换在这种情况下是有效的,但我可以告诉你,在许多情况下,他们不会是b $ b。它们也可能在这里无效(在你发布的代码中至少有一部分代码,对我来说似乎很可能)。


而不是你的TransUp和TransDown方法,您可能希望

考虑使用Encoding

类提供的字符到字节转换。这将允许您在定义.NET字符串和实际字节的

Unicode字符之间进行可靠的往返转换。


Pete

All due respect, I don''t see how if you can''t successfully decrypt the
data, you can assert that "the encryption is working great". Isn''t it
possible that your decryption is correct, but because the encryption is
wrong, the decryption step still fails? :)

Anyway, I''m not an expert in this encryption/decryption you''re doing. But
I do note that you seem to be using bytes and characters interchangeably,
which is generally a bad idea. For all I know, the conversions you''re
doing are valid in this context, but I can tell you that in many contexts
they wouldn''t be. It''s possible they are invalid here as well (in at
least one part of the code you posted, it seems probable to me).

Instead of your "TransUp" and "TransDown" methods, you might want to
consider using the character-to-byte conversions provided by the Encoding
class. That will allow you a reliable, round-trip conversion between the
Unicode characters that define a .NET string and actual bytes.

Pete


不幸的是我尝试了内置的转换例程,并且无法让它们工作。也许我刚刚在德尔福呆了太长时间才能再次想出这个

的东西。就加密例程而言,我知道它可以工作

,因为我可以一次成功解密一条记录。问题

在我试图解密多条记录时发挥作用(从MSSQL传入
)。前一两个工作很棒,然后我就搞定了。

我需要做些什么来删除额外的填充?到目前为止,我还没有看到




Tom


Peter Duniho < Np ********* @nnowslpianmk.com在留言中写道

news:op *************** @ petes-computer.local ...
Unfortunately I tried the built-in conversion routines and couldn''t get them
to work. Maybe I''ve just been in Delphi too long to be able to figure this
stuff out again. As far as the Encrypt routine goes, I know it works
because I can successfully decrypt a single record at a time. The problem
comes into play when I''m trying to decrypt more than one record (passed in
from MSSQL). First one or two work great, then i get this blowing up. Is
there something I need to do to remove extra padding? I haven''t seen
anything like that so far.

Tom

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...

周五,2008年8月1日15:33:10 -0700,Tom Andrecht

< to **** ******@nospam.dmacorporation.comwrote:
On Fri, 01 Aug 2008 15:33:10 -0700, Tom Andrecht
<to**********@nospam.dmacorporation.comwrote:

>我正在努力让一些加密/解密程序得到照顾

我的数据,虽然加密效果很好,但我仍然遇到
消息Padding无效且无法删除。关于解密
一块。
从我能看到的一切,我在这里正确地做事我的代码是
如下:
>I''m trying to get some encryption/decryption routines going to take care
of
my data, and while the encryption is working great, I keep running into
the
message "Padding is invalid and cannot be removed" on the decryption
piece.
From everything I can see, I am doing things correctly here My code is
as
follows:


所有应有的尊重,我不知道如果你不能成功解密

数据,你可以断言加密效果很好。难道你的解密是正确的,但是因为加密是错误的,所以解密步骤仍然失败了吗? :)


无论如何,我不是你加密/解密的专家。但是

我注意到你似乎在互换使用字节和字符,

这通常是一个坏主意。据我所知,你所做的转换在这种情况下是有效的,但我可以告诉你,在许多情况下,他们不会是b $ b。它们也可能在这里无效(在你发布的代码中至少有一部分代码,对我来说似乎很可能)。


而不是你的TransUp和TransDown方法,您可能希望

考虑使用Encoding

类提供的字符到字节转换。这将允许您在定义.NET字符串和实际字节的

Unicode字符之间进行可靠的往返转换。


Pete


All due respect, I don''t see how if you can''t successfully decrypt the
data, you can assert that "the encryption is working great". Isn''t it
possible that your decryption is correct, but because the encryption is
wrong, the decryption step still fails? :)

Anyway, I''m not an expert in this encryption/decryption you''re doing. But
I do note that you seem to be using bytes and characters interchangeably,
which is generally a bad idea. For all I know, the conversions you''re
doing are valid in this context, but I can tell you that in many contexts
they wouldn''t be. It''s possible they are invalid here as well (in at
least one part of the code you posted, it seems probable to me).

Instead of your "TransUp" and "TransDown" methods, you might want to
consider using the character-to-byte conversions provided by the Encoding
class. That will allow you a reliable, round-trip conversion between the
Unicode characters that define a .NET string and actual bytes.

Pete



2008年8月1日星期五16:04:45 -0700,Tom Andrecht

< to **** ******@nospam.dmacorporation.comwrote:
On Fri, 01 Aug 2008 16:04:45 -0700, Tom Andrecht
<to**********@nospam.dmacorporation.comwrote:

不幸的是我尝试了内置的转换程序而无法获得

他们

上班。
Unfortunately I tried the built-in conversion routines and couldn''t get
them
to work.



我恭敬地建议你再试一次。它们实际上并不难以使用(它们与您编写的特殊用途方法基本相同,除非他们进行转换正确)。如果您有关于其使用的具体问题,请随时在这里发布这些

问题。当然在我们所有人中间都会有人可以提供帮助。

I respectfully suggest that you try again. They aren''t actually that hard
to use (they are basically the same as the special-purpose methods you''ve
written, except that they do the conversion correctly). If you have
specific questions about their use, please feel free to post those
questions here. Surely among all of us there''d be someone who can help.


也许我刚刚在德尔福呆了太长时间才能想到这个/>
再次填充。就加密例程而言,我知道它可以工作

,因为我可以一次成功解密一条记录。

问题

在我试图解密多个记录时发挥作用(通过

in
$ b来自MSSQL的$ b)。
Maybe I''ve just been in Delphi too long to be able to figure this
stuff out again. As far as the Encrypt routine goes, I know it works
because I can successfully decrypt a single record at a time. The
problem
comes into play when I''m trying to decrypt more than one record (passed
in
from MSSQL).



这并不能告诉你加密有效。它只告诉你,当你只有一条记录时,加密和解密都可以工作。

因为它们只使用一条记录,而且_something_失败

当你有多个记录时,你仍然不知道这是否是不止一个中有缺陷的加密,解密或两者的加密,解密或两者

记录场景。


至于实际上是什么错误,我会指出,因为你发布的代码是
不简洁但完整代码样本,特别是

没有证明你在多个记录中使用它的确切情况

情景,可能根本不可能解释你做错了什么。


如果你发布一个可靠的演示实际简洁但完整的

代码示例会好得多你遇到的问题。


Pete

That doesn''t tell you that the encryption works. It only tells you that
the encryption and decryption both work when you have only one record.
Since they both work with just one record, and since _something_ fails
when you have more than one record, you still don''t know whether it''s the
encryption, decryption, or both that are flawed in the "more than one
record" scenario.

As for what''s actually wrong, I will point out that since the code you
posted isn''t a concise-but-complete sample of code, and in particular does
not demonstrate how exactly you use it in the "more than one record"
scenario, it may not be possible at all to explain what you''re doing wrong.

It would be much better if you would post an actual concise-but-complete
code sample that reliably demonstrates the problem you''re having.

Pete


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

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