VB.NET如何实现DES加密
发表于:2025-02-22 作者:千家信息网编辑
千家信息网最后更新 2025年02月22日,这篇文章主要介绍了VB.NET如何实现DES加密,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。VB.NET DES加密代码:Impor
千家信息网最后更新 2025年02月22日VB.NET如何实现DES加密
这篇文章主要介绍了VB.NET如何实现DES加密,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
VB.NET DES加密代码:
Imports System Imports System.Collections.Generic Imports System.Text Imports System.IO Imports System.Security Imports System.Security.Cryptography Namespace ZU14 NotInheritable Public Class DES Private iv As String = "1234的yzo" Private key As String = "123在yzo" '/'/ DES加密偏移量,必须是>=8位长的字符串 '/ Public Property IV() As String Get Return iv End Get Set iv = value End Set End Property '/'/ DES加密的私钥,必须是8位长的字符串 '/ Public Property Key() As String Get Return key End Get Set key = value End Set End Property '/'/ 对字符串进行DES加密 '/ '/ 待加密的字符串 '/加密后的BASE64编码的字符串 Public Function Encrypt(sourceString As String) As String Dim btKey As Byte() = Encoding.Default.GetBytes(key) Dim btIV As Byte() = Encoding.Default.GetBytes(iv) Dim des As New DESCryptoServiceProvider() Dim ms As New MemoryStream() Try Dim inData As Byte() = Encoding.Default.GetBytes(sourceString) Try Dim cs As New CryptoStream(ms, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write) Try cs.Write(inData, 0, inData.Length) cs.FlushFinalBlock() Finally cs.Dispose() End Try Return Convert.ToBase64String(ms.ToArray()) Catch End Try Finally ms.Dispose() End Try End Function 'Encrypt '/'/ 对DES加密后的字符串进行解密 '/ '/ 待解密的字符串 '/解密后的字符串 Public Function Decrypt(encryptedString As String) As String Dim btKey As Byte() = Encoding.Default.GetBytes(key) Dim btIV As Byte() = Encoding.Default.GetBytes(iv) Dim des As New DESCryptoServiceProvider() Dim ms As New MemoryStream() Try Dim inData As Byte() = Convert.FromBase64String(encryptedString) Try Dim cs As New CryptoStream(ms, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write) Try cs.Write(inData, 0, inData.Length) cs.FlushFinalBlock() Finally cs.Dispose() End Try Return Encoding.Default.GetString(ms.ToArray()) Catch End Try Finally ms.Dispose() End Try End Function 'Decrypt '/'/ 对文件内容进行DES加密 '/ '/ 待加密的文件绝对路径 '/ 加密后的文件保存的绝对路径 Overloads Public Sub EncryptFile(sourceFile As String, destFile As String) If Not File.Exists(sourceFile) Then Throw New FileNotFoundException("指定的文件路径不存在!", sourceFile) End If Dim btKey As Byte() = Encoding.Default.GetBytes(key) Dim btIV As Byte() = Encoding.Default.GetBytes(iv) Dim des As New DESCryptoServiceProvider() Dim btFile As Byte() = File.ReadAllBytes(sourceFile) Dim fs As New FileStream(destFile, FileMode.Create, FileAccess.Write) Try Try Dim cs As New CryptoStream(fs, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write) Try cs.Write(btFile, 0, btFile.Length) cs.FlushFinalBlock() Finally cs.Dispose() End Try Catch Finally fs.Close() End Try Finally fs.Dispose() End Try End Sub 'EncryptFile '/'/ 对文件内容进行DES加密,加密后覆盖掉原来的文件 '/ '/ 待加密的文件的绝对路径 Overloads Public Sub EncryptFile(sourceFile As String) EncryptFile(sourceFile, sourceFile) End Sub 'EncryptFile '/'/ 对文件内容进行DES解密 '/ '/ 待解密的文件绝对路径 '/ 解密后的文件保存的绝对路径 Overloads Public Sub DecryptFile(sourceFile As String, destFile As String) If Not File.Exists(sourceFile) Then Throw New FileNotFoundException("指定的文件路径不存在!", sourceFile) End If Dim btKey As Byte() = Encoding.Default.GetBytes(key) Dim btIV As Byte() = Encoding.Default.GetBytes(iv) Dim des As New DESCryptoServiceProvider() Dim btFile As Byte() = File.ReadAllBytes(sourceFile) Dim fs As New FileStream(destFile, FileMode.Create, FileAccess.Write) Try Try Dim cs As New CryptoStream(fs, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write) Try cs.Write(btFile, 0, btFile.Length) cs.FlushFinalBlock() Finally cs.Dispose() End Try Catch Finally fs.Close() End Try Finally fs.Dispose() End Try End Sub 'DecryptFile '/'/ 对文件内容进行DES解密,加密后覆盖掉原来的文件 '/ '/ 待解密的文件的绝对路径 Overloads Public Sub DecryptFile(sourceFile As String) DecryptFile(sourceFile, sourceFile) End Sub 'DecryptFile End Class 'DES End Namespace 'ZU14
VB.NET DES加密使用方法:
Dim des As New ZU14.DES() des.IV = "abcd哈哈笑" des.Key = "必须八位" Dim es As String = des.Encrypt("在") Console.WriteLine(es) Console.Write(des.Decrypt(es)) des.EncryptFile("d:\a.txt", "d:\b.txt") des.DecryptFile("d:\b.txt") Console.ReadKey(True)
感谢你能够认真阅读完这篇文章,希望小编分享的"VB.NET如何实现DES加密"这篇文章对大家有帮助,同时也希望大家多多支持,关注行业资讯频道,更多相关知识等着你来学习!
加密
文件
字符
字符串
路径
内容
篇文章
代码
价值
使用方法
兴趣
同时
方法
更多
朋友
知识
编带
编码
行业
资讯
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
公司服务器连接不了电脑
完善全市网络安全保障体系
服务器上插u盘支持热插拔吗
软考数据库工程师新大纲
泗洪环保网络技术是什么
软件开发单位进行索赔
中经网数据库登录入口
软件开发cg
电脑网络安全模式下能使用吗
四川正规软件开发服务应用
网络安全保障管理
数据库诊断工具来源
脚本软件开发郑州
苹果4没有通话记录数据库
服务器要招收管理员
腾讯云服务器管理器如何使用
服务器ip查询网站
软件开发预算报价单
到客网络技术有限公司怎么样
大二层和普通的网络技术
服务器域控制器
事业单位网络安全使用宣传
智慧大脑网络安全
枣庄网络安全性
无锡特殊网络技术销售厂家
华为服务器下载安装包校验失败
富孟网络安全吗
服务器要招收管理员
重庆合川西部网络安全城地址
重庆贷款软件开发公司有哪些