如何使用 RijndaelManaged 和 PKCS5 填充加密 vb.net 中的字符串?

发布于 2024-09-27 20:17:35 字数 525 浏览 4 评论 0原文

我使用以下代码来初始化加密...

 Dim symmetricKey As New System.Security.Cryptography.RijndaelManaged()
 With symmetricKey
   .Key = Encoding.ASCII.GetBytes(Key)
   .IV = Encoding.ASCII.GetBytes(IV)
   .Mode = CipherMode.CBC
   .BlockSize = 128 
   .KeySize = 128 
   .Padding = PaddingMode.PKCS7
End With

要求是使用PKCS5。 vb.net 中的填充模式仅包括

  • ANSIX923
  • ISO10126
  • PKCS7

所以我认为没有 PKCS5 的方法。有什么办法可以添加吗,还是需要自己写一个加密方法?如果是这样-我该怎么写?是否有可靠的 DLL 支持它?

I use the following code to initialize encryption...

 Dim symmetricKey As New System.Security.Cryptography.RijndaelManaged()
 With symmetricKey
   .Key = Encoding.ASCII.GetBytes(Key)
   .IV = Encoding.ASCII.GetBytes(IV)
   .Mode = CipherMode.CBC
   .BlockSize = 128 
   .KeySize = 128 
   .Padding = PaddingMode.PKCS7
End With

The requirement is to use PKCS5. Padding modes in vb.net only include

  • ANSIX923
  • ISO10126
  • None
  • PKCS7
  • Zeros

So I don't think there is a method for PKCS5. Is there any way to add it, or do I need to write an encryption method myself? If so - how do I write that? Is there a reliable DLL that will support it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

噩梦成真你也成魔 2024-10-04 20:17:35

PKCS7 填充和 PKCS5 填充是相同的。在这种情况下,它们是同义词。

编辑:

PKCS#7 填充在 PKCS#7 规范 部分中进行了描述10.3. PKCS#5 填充在 PKCS#5 规范 第 6.1.1 节中进行了描述步骤 4. 通过检查您可以看到,填充算法是相同的。

PKCS7 padding and PKCS5 padding are the same thing. In this context they are synonyms.

EDIT:

The PKCS#7 padding is described in the PKCS#7 spec in section 10.3. PKCS#5 padding is described in the PKCS#5 spec in section 6.1.1 step 4. As you can see by examination, the padding algorithms are identical.

泪意 2024-10-04 20:17:35

我猜你需要其他人来读取你的加密数据,然后只理解这种填充。

您可能知道,PKCS5 的解释如下:

PKCS#5 填充的工作原理如下:为填充块的剩余字节分配一个数字,该数字是为填充块而添加的字节数。例如,如果我们有一个 16 字节的块,并且只填充了 11 字节,则需要填充 5 字节。这 5 个字节都被分配值“5”,用于 5 个字节的填充。

好吧,您已经有了信息 - 将字符串编码为 byte[],对其进行扩展,使其与 16 个字节对齐,然后根据配方填充其余部分。然后,使用 Padding.None 进行加密。

估计应该不会这么麻烦吧。无论如何,没有字符串加密,所以既然你将这些东西编码为 byte[] ,...

string message="lorem ipsum and stuff";
byte[] result=Text.Encode(message);
int packets=result.Length/16;
int paddingSize=16-(result.Length-(packets*16));
if (paddingSize!=16) 
{
    byte[] newbuffer=new byte[result.Length+paddingSize];
    packets.CopyTo(newbuffer);
    for (int n=result.Length;n<newbuffer.Length;n++)
    {
        newbuffer[n]=16-paddingsize;
    }
}
//  then, encrypt result or newbuffer, depending on if padding is 16 or not

注意:代码不在我的脑海中,它根本无法运行...

I guess that you need someone else to read your encrypted data, and then only understand that kind of padding.

As you probably know, PKCS5 is explained as:

PKCS#5 padding works as follows: the bytes remaining to fill a block are assigned a number, which is the number of bytes that were added to fill the block. For instance, if we have an 16-byte block, and only 11 bytes are filled, then we have 5 bytes to pad. Those 5 bytes are all assigned the value "5", for the 5 bytes of padding.

Well, you have your info - encode the string to byte[], extend it so it is aligned to 16 bytes, and fill the rest according to the recipe. Then, encrypt with Padding.None.

Guess it shouldn't be so troublesome. Anyway, there is no string encryption, so since you encode the stuff to byte[] anyway, ...

string message="lorem ipsum and stuff";
byte[] result=Text.Encode(message);
int packets=result.Length/16;
int paddingSize=16-(result.Length-(packets*16));
if (paddingSize!=16) 
{
    byte[] newbuffer=new byte[result.Length+paddingSize];
    packets.CopyTo(newbuffer);
    for (int n=result.Length;n<newbuffer.Length;n++)
    {
        newbuffer[n]=16-paddingsize;
    }
}
//  then, encrypt result or newbuffer, depending on if padding is 16 or not

NOTE: code is out of my head, it's not runable at all...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文