ICryptoTransform.TransformFinalBlock实际做什么? [英] What Does ICryptoTransform.TransformFinalBlock Actually Do?

查看:533
本文介绍了ICryptoTransform.TransformFinalBlock实际做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出同事写的一些代码,但是 TransformFinalBlock 的文档并没有真正解释任何内容.它说它转换指定字节数组的指定区域"并返回计算的转换",但是没有解释执行哪种转换.这是我的同事写的代码:

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:

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


我认为这不是执行加密的正确方法(我使用的是利用 CryptoStream 的技术),但是我不确定以上代码不正确. Mabye这只是在无论如何使用字节数组时避免流的一种聪明方法.没有把握.我想自己可以节省几个小时,以防万一你们比我更了解加密.


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.

推荐答案

如果您想加密任意一个加密块,数组中的字节数,这是一种可行的方法.是的,它实际上是对字符串进行加密并将结果作为Base64字符串返回.
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.


这篇关于ICryptoTransform.TransformFinalBlock实际做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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