Versión Copiada del tfs
This commit is contained in:
236
TripleDES.vb
Normal file
236
TripleDES.vb
Normal file
@@ -0,0 +1,236 @@
|
||||
Imports System.IO
|
||||
Imports System.Text
|
||||
Imports System.Security.Cryptography
|
||||
|
||||
Public Class TripleDES
|
||||
Private TripleDes As New TripleDESCryptoServiceProvider
|
||||
Private Function TruncateHash(
|
||||
ByVal key As String,
|
||||
ByVal length As Integer) As Byte()
|
||||
|
||||
Dim sha1 As New SHA1CryptoServiceProvider
|
||||
|
||||
' Hash the key.
|
||||
Dim keyBytes() As Byte =
|
||||
System.Text.Encoding.Unicode.GetBytes(key)
|
||||
Dim hash() As Byte = sha1.ComputeHash(keyBytes)
|
||||
|
||||
' Truncate OrElse pad the hash.
|
||||
ReDim Preserve hash(length - 1)
|
||||
Return hash
|
||||
End Function
|
||||
Sub New(ByVal key As String)
|
||||
' Initialize the crypto provider.
|
||||
TripleDes.Key = Encoding.ASCII.GetBytes(key) ' TruncateHash(key, TripleDes.KeySize \ 8)
|
||||
Dim B As Byte() = New Byte() {0, 0, 0, 0, 0, 0, 0, 0}
|
||||
TripleDes.IV = B
|
||||
|
||||
TripleDes.Padding = PaddingMode.None
|
||||
TripleDes.Mode = CipherMode.CBC
|
||||
|
||||
End Sub
|
||||
Public Function EncryptData(
|
||||
ByVal plaintext As String) As String
|
||||
|
||||
' Convert the plaintext string to a byte array.
|
||||
Dim plaintextBytes() As Byte = System.Text.Encoding.ASCII.GetBytes(plaintext)
|
||||
|
||||
' Create the stream.
|
||||
Dim ms As New System.IO.MemoryStream
|
||||
' Create the encoder to write to the stream.
|
||||
Dim encStream As New 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)
|
||||
'Return Convert.ToBase64String(ms.ToArray)
|
||||
Return System.Text.Encoding.UTF8.GetString(ms.ToArray)
|
||||
End Function
|
||||
|
||||
Public Function EncryptDataHex(ByVal plaintext As String) As String
|
||||
|
||||
' Convert the plaintext string to a byte array.
|
||||
Dim plaintextBytes() As Byte = System.Text.Encoding.ASCII.GetBytes(plaintext)
|
||||
|
||||
' Create the stream.
|
||||
Dim ms As New System.IO.MemoryStream
|
||||
' Create the encoder to write to the stream.
|
||||
Dim encStream As New 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()
|
||||
|
||||
Return Utilidades.ByteArrayToHex(ms.ToArray)
|
||||
End Function
|
||||
|
||||
Public Function DecryptData(
|
||||
ByVal encryptedtext As String) As String
|
||||
|
||||
' Convert the encrypted text string to a byte array.
|
||||
'Dim encryptedBytes() As Byte = Convert.FromBase64String(encryptedtext)
|
||||
Dim encryptedBytes() As Byte = Encoding.ASCII.GetBytes(encryptedtext)
|
||||
|
||||
' Create the stream.
|
||||
Dim ms As New System.IO.MemoryStream
|
||||
' Create the decoder to write to the stream.
|
||||
Dim decStream As New 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.UTF8.GetString(ms.ToArray)
|
||||
Return Convert.ToBase64String(ms.ToArray)
|
||||
End Function
|
||||
Public Function DecryptData(ByVal Encriptado() As Byte) 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 CryptoStream(ms,
|
||||
TripleDes.CreateDecryptor(),
|
||||
System.Security.Cryptography.CryptoStreamMode.Write)
|
||||
|
||||
|
||||
' Use the crypto stream to write the byte array to the stream.
|
||||
decStream.Write(Encriptado, 0, Encriptado.Length)
|
||||
decStream.FlushFinalBlock()
|
||||
|
||||
' Convert the plaintext stream to a string.
|
||||
Return System.Text.Encoding.ASCII.GetString(ms.ToArray)
|
||||
' Return Convert.ToBase64String(ms.ToArray)
|
||||
End Function
|
||||
End Class
|
||||
|
||||
'Public Class TripleDES
|
||||
|
||||
' ' define the triple des provider
|
||||
' Private m_des As New TripleDESCryptoServiceProvider
|
||||
|
||||
' ' define the string handler
|
||||
' Private m_utf8 As New UTF8Encoding
|
||||
|
||||
' ' define the local property arrays
|
||||
' Private m_key() As Byte
|
||||
' 'Private m_iv() As Byte
|
||||
|
||||
|
||||
' Private Shared IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}
|
||||
' Public Sub New(ByVal key As String)
|
||||
' Me.m_key = Encoding.ASCII.GetBytes(key)
|
||||
' m_des.Mode = CipherMode.CBC
|
||||
' m_des.Padding = PaddingMode.PKCS7
|
||||
' m_des.BlockSize = 64
|
||||
|
||||
' ' m_des.Padding = PaddingMode.None
|
||||
' ' m_des.Mode = CipherMode.ECB
|
||||
' ' Me.m_iv = iv
|
||||
' End Sub
|
||||
|
||||
' Public Sub New(ByVal key() As Byte, ByVal iv() As Byte)
|
||||
' Me.m_key = key
|
||||
' ' m_des.Padding = PaddingMode.None
|
||||
' ' m_des.Mode = CipherMode.ECB
|
||||
' ' Me.m_iv = iv
|
||||
' End Sub
|
||||
|
||||
' Public Function EncryptData(ByVal input() As Byte) As Byte()
|
||||
' Return Transform(input, m_des.CreateEncryptor(m_key, IV))
|
||||
' End Function
|
||||
|
||||
' Public Function Decrypt(ByVal input() As Byte) As Byte()
|
||||
' Return Transform(input, m_des.CreateDecryptor(m_key, IV))
|
||||
' End Function
|
||||
|
||||
' Public Function EncryptData(ByVal text As String) As String
|
||||
' Dim input() As Byte = m_utf8.GetBytes(text)
|
||||
' Dim output() As Byte = Transform(input, _
|
||||
' m_des.CreateEncryptor(m_key, IV))
|
||||
' Return Convert.ToBase64String(output)
|
||||
' End Function
|
||||
|
||||
' Public Function Decrypt(ByVal text As String) As String
|
||||
' Dim input() As Byte = Convert.FromBase64String(text)
|
||||
' Dim output() As Byte = Transform(input, _
|
||||
' m_des.CreateDecryptor(m_key, IV))
|
||||
' Return m_utf8.GetString(output)
|
||||
' End Function
|
||||
|
||||
' Private Function Transform(ByVal input() As Byte, _
|
||||
' ByVal CryptoTransform As ICryptoTransform) As Byte()
|
||||
' ' create the necessary streams
|
||||
' Dim memStream As MemoryStream = New MemoryStream
|
||||
' Dim cryptStream As CryptoStream = New _
|
||||
' CryptoStream(memStream, CryptoTransform, _
|
||||
' CryptoStreamMode.Write)
|
||||
' ' transform the bytes as requested
|
||||
' cryptStream.Write(input, 0, input.Length)
|
||||
' cryptStream.FlushFinalBlock()
|
||||
' ' Read the memory stream and convert it back into byte array
|
||||
' memStream.Position = 0
|
||||
' Dim result(CType(memStream.Length - 1, System.Int32)) As Byte
|
||||
' memStream.Read(result, 0, CType(result.Length, System.Int32))
|
||||
' ' close and release the streams
|
||||
' memStream.Close()
|
||||
' cryptStream.Close()
|
||||
' ' hand back the encrypted buffer
|
||||
' Return result
|
||||
' End Function
|
||||
' Public Shared Function DESEncrypt(ByVal Data As String, ByVal Key As String) As Byte()
|
||||
|
||||
' Try
|
||||
' Dim bykey() As Byte = System.Text.Encoding.UTF8.GetBytes(Left(Key, 24))
|
||||
|
||||
|
||||
' If String.IsNullOrEmpty(Data) Then
|
||||
|
||||
' Throw New ArgumentException("No data passed", "input")
|
||||
|
||||
' ElseIf bykey Is Nothing OrElse bykey.Length <> 24 Then
|
||||
|
||||
' Throw New ArgumentException("Invalid Key. Key must be 24 bytes length", "key")
|
||||
|
||||
' End If
|
||||
|
||||
' Dim InputByteArray() As Byte = System.Text.Encoding.UTF8.GetBytes(Data)
|
||||
|
||||
' Using ms As New IO.MemoryStream
|
||||
|
||||
' Using des As New Security.Cryptography.TripleDESCryptoServiceProvider
|
||||
|
||||
' Using cs As New Security.Cryptography.CryptoStream(ms, des.CreateEncryptor(bykey, IV), Security.Cryptography.CryptoStreamMode.Write)
|
||||
|
||||
' cs.Write(InputByteArray, 0, InputByteArray.Length)
|
||||
' cs.FlushFinalBlock()
|
||||
' Return ms.ToArray()
|
||||
|
||||
' End Using
|
||||
|
||||
' End Using
|
||||
|
||||
' End Using
|
||||
|
||||
' Catch ex As Exception
|
||||
' Throw
|
||||
' End Try
|
||||
|
||||
' End Function
|
||||
|
||||
'End Class
|
||||
Reference in New Issue
Block a user