VB.Net TDES加密问题 [英] VB.Net TDES Encryption Problem

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

问题描述

我正在将一些VB6代码转换为VB.Net.该代码对16字节的数据,三重DES,128位密钥,ECB模式进行加密.使用相同的数据和相同的密钥,VB6代码给了我2个块,而VB.Net代码给了我3个块.前两个块与VB6代码中的两个块匹配.如果从VB.Net数据中删除一个字节,则会得到两个块.我的理解是,在加密17个字节的数据之前,我不应该获得三个块.有谁知道为什么我要从VB.Net获得三个加密数据块?

这是VB.Net代码.

I am converting some VB6 code to VB.Net. The code encrypts 16 bytes of data, triple DES, 128 bit key, ECB mode. Using the same data and the same key, the VB6 code gives me 2 blocks and the VB.Net code gives me 3 blocks. The first two blocks match the two blocks from the VB6 code. If I remove one byte from the VB.Net data, I get two blocks. My understanding is that I should not get three blocks until I encrypt 17 bytes of data. Does anyone know why I am getting three blocks of encrypted data from VB.Net?

Here is the VB.Net code.

Dim TDes As New TripleDESCryptoServiceProvider
Dim KeyString As String =  "0102030405060708090A0B0C0D0E0F10"
Dim DataString As String = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" 
Dim DataBytes(15) As Byte
Dim KeyBytes(15) As Byte
Dim EncryptBytes() As Byte
Dim EncryptString As String = ""
Dim x As Byte
For x = 0 To KeyString.Length - 1 Step 2
    KeyBytes(x / 2) = Val("&h" & Mid(KeyString, x + 1, 2))
Next x
For x = 0 To DataString.Length - 1 Step 2
    DataBytes(x / 2) = Val("&h" & Mid(DataString, x + 1, 2))
Next x
TDes.KeySize = 128
TDes.Key = KeyBytes
TDes.Mode = CipherMode.ECB
Dim ms As New System.IO.MemoryStream
Dim encStream As New CryptoStream(ms, TDes.CreateEncryptor(), _
    System.Security.Cryptography.CryptoStreamMode.Write)
encStream.Write(DataBytes, 0, DataBytes.Length)
encStream.FlushFinalBlock()
EncryptBytes = ms.ToArray()
For x = 0 To EncryptBytes.Length - 1
    EncryptString = EncryptString & Mid("0" & Hex(EncryptBytes(x)), _
        Len(Hex(EncryptBytes(x))), 2)
Next x
MessageBox.Show(EncryptString & " - " & Len(EncryptString) / 2)



谢谢,
Mike



Thanks,
Mike

推荐答案

我怀疑这取决于填充和AFAIK为PKCS7的默认填充模式.参见RFC5652第6.3段
( http://tools.ietf.org/html/rfc5652#section-6.3 [ ^ ]),它表明PKCS7填充总是添加至少1个字节.因此16个字节的纯文本应该加密为3个密文块(使用PKCS7填充).
I suspect this is down to padding and the default padding mode which AFAIK is PKCS7. See RFC5652 para 6.3
(http://tools.ietf.org/html/rfc5652#section-6.3[^]) which shows that PKCS7 padding always adds at least 1 byte. So 16 bytes of plain text should encrypt to 3 blocks of cipher text (using PKCS7 padding).


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

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