Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to figure out some code a coworker wrote, but the documentation for TransformFinalBlock does not really explain anything. It says it "transforms the specified region of the specified byte array" and returns "the computed transform", but it doesn't explain what kind of transform is performed. Here is the code my coworker wrote:
VB.NET
Public Shared Function Encrypt(ByVal str As String, ByVal strKey As String) As String
    cryptDES3.Key = cryptMD5Hash.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strKey))
    cryptDES3.Mode = CipherMode.ECB
    Dim desdencrypt As ICryptoTransform = cryptDES3.CreateEncryptor()
    Dim buff() As Byte = ASCIIEncoding.ASCII.GetBytes(str)
    Return (Convert.ToBase64String(desdencrypt.TransformFinalBlock(buff, 0, buff.Length)))
End Function

I'm thinking this is not the correct way to perform encryption (I'm using a technique that makes use of CryptoStream), but I can't be sure the above code is incorrect. Mabye this is just a clever way to avoid a stream when you're working with byte arrays anyway. Not sure. I thought I'd save myself a few hours in case any of you know more about encryption than I do.
Posted
Comments
AspDotNetDev 5-Apr-11 16:43pm    
Note: nobody should use this code. It will cause some data corruption due to the use of ASCII encoding.

1 solution

If you wanted to encrypt an arbitrary block of bytes in an array, this is one possible way to do it. Yes, it's actually encrypting the string and returning the result as a Base64 string.
 
Share this answer
 
Comments
AspDotNetDev 5-Apr-11 15:55pm    
Do you know where the name comes from? Why is it the "final" block it is transforming?
Dave Kreskowiak 5-Apr-11 16:55pm    
Nope, I have no idea why it has that name.
AspDotNetDev 5-Apr-11 17:18pm    
Ok. In any event, it seems the code is broken in another way, so I'm just going to create some new functions and deprecate the existing ones.
Dave Kreskowiak 5-Apr-11 18:29pm    
Sounds good to me. The code posted isn't very flexible.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900