• VB.Net 字符串加密类


    Public Class Cls_JM
    
    
        '使用
        'Dim Jm As New Cls_JM(2)
        'Dim strTmp As String
    
        'Jm.jiemi(strTmp)
        'Jm.Jiami(strTmp)
    
        Private TripleDes As New System.Security.Cryptography.TripleDESCryptoServiceProvider
    
        Private Function TruncateHash(ByVal key As String, ByVal length As Integer) As Byte()
            Dim sha1 As New System.Security.Cryptography.SHA1CryptoServiceProvider  ' Hash the key.  
            Dim keyBytes() As Byte = System.Text.Encoding.Unicode.GetBytes(key)
            Dim hash() As Byte = sha1.ComputeHash(keyBytes)  ' Truncate or pad the hash. 
            ReDim Preserve hash(length - 1)
            Return hash
        End Function
    
        Sub New(ByVal key As String)  ' Initialize the crypto provider.  
            TripleDes.Key = TruncateHash(key, TripleDes.KeySize  8)
            TripleDes.IV = TruncateHash("", TripleDes.BlockSize  8)
        End Sub
    
        Public Function jiami(ByVal plaintext As String) As String  ' Convert the plaintext string to a byte array. 
            Dim plaintextBytes() As Byte = System.Text.Encoding.Unicode.GetBytes(plaintext)  ' Create the stream. 
            Dim ms As New System.IO.MemoryStream  ' Create the encoder to write to the stream. 
            Dim encStream As New System.Security.Cryptography.CryptoStream(ms, TripleDes.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write)  ' Use the crypto stream to write the byte array to the stream. 
            encStream.Write(plaintextBytes, 0, plaintextBytes.Length)
            encStream.FlushFinalBlock()  ' Convert the encrypted stream to a printable string. 
            Return Convert.ToBase64String(ms.ToArray)
        End Function
    
        Public Function jiemi(ByVal encryptedtext As String) As String  ' Convert the encrypted text string to a byte array. 
            Dim encryptedBytes() As Byte = Convert.FromBase64String(encryptedtext)  ' Create the stream. 
            Dim ms As New System.IO.MemoryStream  ' Create the decoder to write to the stream. 
            Dim decStream As New System.Security.Cryptography.CryptoStream(ms, TripleDes.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write)  ' Use the crypto stream to write the byte array to the stream. 
            decStream.Write(encryptedBytes, 0, encryptedBytes.Length)
            decStream.FlushFinalBlock()  ' Convert the plaintext stream to a string. 
            Return System.Text.Encoding.Unicode.GetString(ms.ToArray)
        End Function
    
    
    End Class
  • 相关阅读:
    关于form表单的相同name问题
    MySQL数据库视图
    Blazor
    查看Oracle正在执行的任务
    比较不错的几款开源的WPF Charts报表控件
    Raft算法
    EntityFramework 使用Linq处理内连接(inner join)、外链接(left/right outer join)、多表查询
    systemd、upstart和system V 枯木
    MRTG生成首页报错解决方法 枯木
    dd备份和恢复 枯木
  • 原文地址:https://www.cnblogs.com/yhsc/p/4393670.html
Copyright © 2020-2023  润新知