高级加密标准 AES(Rijndael) vbnet 类

上传人:飞*** 文档编号:40348161 上传时间:2018-05-25 格式:DOC 页数:18 大小:53.50KB
返回 下载 相关 举报
高级加密标准 AES(Rijndael) vbnet 类_第1页
第1页 / 共18页
高级加密标准 AES(Rijndael) vbnet 类_第2页
第2页 / 共18页
高级加密标准 AES(Rijndael) vbnet 类_第3页
第3页 / 共18页
高级加密标准 AES(Rijndael) vbnet 类_第4页
第4页 / 共18页
高级加密标准 AES(Rijndael) vbnet 类_第5页
第5页 / 共18页
点击查看更多>>
资源描述

《高级加密标准 AES(Rijndael) vbnet 类》由会员分享,可在线阅读,更多相关《高级加密标准 AES(Rijndael) vbnet 类(18页珍藏版)》请在金锄头文库上搜索。

1、高级加密标准 AES(Rijndael) 类高级加密标准 AES(Rijndael) 类Imports System.Security.CryptographyImports System.IO 参数:文件(原始文件-目标文件) 密码 初始化向量 密码长度 加密或解密的块长度 参数:字符串 密码 初始化向量 密码长度 加密或解密的块长度 参数都是 字符串 或 数字,没有字节 支持 任何文件加密、支持中文密码、中文初始化向量 【只支持文本文件之类】-说明:注释部分与紧挨的下面行是对应的。 与 Line 01 对应,2 选择 1-说明:本行与 Line 01 行的在整个函数中只能启用一行。 可

2、以把所有注释行全部删除,对本类没有任何影响。 Public Class ClsAES -高级加密标准 2008.2.16 2008.2.29 修订支持二进制文件 Ver:0.82 e-mail:Dim Key() As Byte, IV() As ByteDim RijndaelAlg As Rijndael = Rijndael.CreateDim strLen As Integer, g As Integer, k As IntegerDim outfStream As FileStreamDim infStream As FileStreamFunction EncryptFileToF

3、ile(ByVal fromFileName As String, ByVal toFileName As String, _ByVal varKey As String, ByVal varKeySize As Integer, ByVal varIV As String, ByVal varBlockSize As Integer) As Boolean 加密文件 2008.2.15If UCase(Trim(toFileName) = UCase(Trim(fromFileName) ThenMsgBox(“源文件与目标文件不能相同!“, MsgBoxStyle.Critical, “A

4、ES“)Exit FunctionEnd IfIf File.Exists(fromFileName) = False ThenMsgBox(“源文件不存在!“, MsgBoxStyle.Critical, “AES“)Exit FunctionEnd IfTryoutfStream = File.Open(toFileName, FileMode.Create)infStream = File.OpenRead(fromFileName) 与 Line 01 对应,2 选择 1Catch ex As ExceptionMsgBox(“加密打开文件遇到错误:“ + ex.Message, Ms

5、gBoxStyle.Critical, “AES“)Exit FunctionEnd TryTryIf varKeySize 192 And varKeySize 128 And varBlockSize 256 Then BlockSize 大小必须只有 这 3 种outfStream.Close()infStream.Close()MsgBox(“块大小必须是 128、192 和 256 中的一个。“)Exit FunctionEnd IfstrLen = Int(varKeySize / 8)Key(g) = Convert.ToByte(varKey.Chars(g)Key = Sys

6、tem.Text.Encoding.Default.GetBytes(varKey) 二进制读取,否则中文字符变成 3 个字节If strLen 128 And varKeySize 256 Then keysize 大小必须只有 这 3 种outfStream.Close()infStream.Close()MsgBox(“密码长度必须是 128、192 和 256 中的一个。“)Exit FunctionEnd IfIf varBlockSize 192 And varBlockSize 192 And varKeySize 128 And varBlockSize 256 Then Bl

7、ockSize 大小必须只有 这 3 种MStream.Close()MsgBox(“块大小必须是 128、192 和 256 中的一个。“)Exit FunctionEnd IfstrLen = Int(varKeySize / 8)Key(g) = Convert.ToByte(varKey.Chars(g)Key = System.Text.Encoding.Default.GetBytes(varKey) 二进制读取,否则中文字符变成 3 个字节If strLen 128 And varKeySize 256 Then keysize 大小必须只有 这 3 种MsgBox(“密码长度必

8、须是 128、192 和 256 中的一个。“)Exit FunctionEnd IfIf varBlockSize 192 And varBlockSize 256 Then BlockSize 大小必须只有 这 3 种MsgBox(“块大小必须是 128、192 和 256 中的一个。“)Exit FunctionEnd IfstrLen = Int(varKeySize / 8)Key(g) = Convert.ToByte(varKey.Chars(g)Key = System.Text.Encoding.Default.GetBytes(varKey) 二进制读取,否则中文字符变成

9、3 个字节If strLen UBound(Key) + 1 ThenMsgBox(“密码长度不能超过 “ + strLen.ToString + “ 个字符。“)Exit FunctionEnd IfReDim Preserve Key(strLen - 1)strLen = Int(varBlockSize / 8)IV = System.Text.Encoding.Default.GetBytes(varIV) 二进制读取,否则中文字符变成 3 个字节If strLen UBound(IV) + 1 ThenMsgBox(“块长度不能超过 “ + strLen.ToString + “

10、个字符。“)Exit FunctionEnd IfReDim Preserve IV(strLen - 1)RijndaelAlg.BlockSize = varBlockSizeRijndaelAlg.KeySize = varKeySizestrLen = Len(Data) / 2 全部都是 16 进制,可以采用 Len 函数Dim firstString() As StringDim tmpStr As StringReDim firstString(strLen - 1)k = 0tmpStr = DataDim tmpMid = “For g = 0 To strLen - 1fi

11、rstString(g) = Microsoft.VisualBasic.Left(tmpStr, 2)If Len(tmpStr) = 2 ThenfirstString(g) = Microsoft.VisualBasic.Right(tmpStr, 2)Exit ForEnd IftmpMid = Microsoft.VisualBasic.Right(tmpStr, Len(tmpStr) - 2)tmpStr = tmpMidNext 编程了 Hex 的 ASC,需要转 Dec 的 ASCDim myint As Integer = Convert.ToInt32(“1A“, 16)

12、Dim MStream As New MemoryStream(strLen - 1) ASC-字符Dim mBT(strLen - 1) As Bytek = 0For g = 0 To UBound(firstString)mBT(k) = Convert.ToInt32(firstString(g), 16) k = k + 1NextMStream.Write(mBT, 0, mBT.Length)MStream.Position = 0Dim byteArray = New Byte(CType(MStream.Length, Integer) Dim count = MStream

13、.Read(byteArray, 0, MStream.Length)MStream.Position = 0Dim cStream As New CryptoStream(MStream, _RijndaelAlg.CreateDecryptor(Key, IV), _CryptoStreamMode.Read)Dim sReader As New StreamReader(cStream, System.Text.Encoding.Default)Dim val As String = Nothingval = sReader.ReadToEndsReader.Close()cStream.Close()MStream.Close()DecryptStrToStr = valCatch ex As ExceptionMsgBox(“解密字符串遇到错误:“ + ex.Message, MsgBoxStyle.Critical, “AES“)End TryEnd FunctionEnd Class -高级加密标准

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号