[vb.net] 암호화 키 생성 및 암호화, 복호화 예제 > vb.net

본문 바로가기

vb.net

[기타] [vb.net] 암호화 키 생성 및 암호화, 복호화 예제

회원사진
하나를하더라도최선을
2019-10-08 22:07 9,458 0

본문



Imports System.IO
Imports System.Runtime.InteropServices.ComTypes
 
Public Class cCrypt
    Dim aKey() As Byte = Nothing
    Dim bKey() As Byte = Nothing
    Dim EnPath As String = Nothing
    Dim DePath As String = Nothing
    Public Sub New()
        If Len(Dir(Application.StartupPath & "\Settings", vbDirectory)) = 0 Then MkDir(Application.StartupPath & "\Settings")
        EnPath = "Settings\EnKey.DAT"
        DePath = "Settings\DEKey.DAT"
        If File.Exists(EnPath) Then aKey = System.IO.File.ReadAllBytes(EnPath)
        If File.Exists(DePath) Then bKey = System.IO.File.ReadAllBytes(DePath)
    End Sub
 
    Public Property EnKeyPath() As String
        Get
            Return EnPath
        End Get
        Set(ByVal value As String)
            EnPath = value
        End Set
    End Property
 
    Public Property DeKeyPath() As String
        Get
            Return DePath
        End Get
        Set(ByVal value As String)
            DePath = value
        End Set
    End Property
 
    Public Property EnKey() As Byte()
        Get
            Return aKey
        End Get
        Set(ByVal value() As Byte)
            aKey = value
        End Set
    End Property
 
    Public Property DeKey() As Byte()
        Get
            Return bKey
        End Get
        Set(ByVal value() As Byte)
            bKey = value
        End Set
    End Property
 
    Dim RandomValue As New Random
    Public Function NewKeys() As String
        Dim value As New List(Of Byte)
        Dim i As Integer, n As Integer
        Dim x As New Collection
        Do
            n = RandomValue.Next(0256)
            If xAdd(x, n) Then
                value.Add(n)
            End If
        Loop Until value.Count > 255
 
        For i = 257 To 1280
            value.Add(RandomValue.Next(0256))
        Next
 
        Dim key1() As Byte = value.ToArray
        Dim key2() As Byte = value.ToArray
        For i = 0 To 255
            For n = 0 To 255
                If key1(n) = i Then
                    key2(i) = n
                    Exit For
                End If
            Next
        Next
 
        Dim oFileStream As System.IO.FileStream
        oFileStream = New System.IO.FileStream(EnPath, System.IO.FileMode.Create)
        oFileStream.Write(key1, 0, key1.Length)
        oFileStream.Close()
 
        oFileStream = New System.IO.FileStream(DePath, System.IO.FileMode.Create)
        oFileStream.Write(key2, 0, key1.Length)
        oFileStream.Close()
 
        Dim T As String = System.Text.Encoding.UTF8.GetString(key1)
        T &= vbNullChar
        T &= System.Text.Encoding.UTF8.GetString(key2)
    End Function
 
    Public Function EnCryptString(ByVal value As StringAs String
        If value Is Nothing Then Return Nothing
        If aKey Is Nothing Then
            If Not File.Exists(EnPath) Then
                If MsgBox("키가 존재하지 않습니다. 키가 없으면 프로그램 동작에 문제가 있습니다. 키를 새로 생성할까요?", vbYesNo) = vbYes Then
                    NewKeys()
                End If
            End If
            aKey = System.IO.File.ReadAllBytes(EnPath)
        End If
        Dim B() As Byte = System.Text.Encoding.UTF8.GetBytes(value)
        For i = 0 To B.Length - 1
            B(i) = aKey(B(i))
        Next
        Return Replace(BitConverter.ToString(B, 0), "-""")
    End Function
 
    Public Function DeCryptString(ByVal value As StringAs String
        If value Is Nothing Then Return Nothing
        If bKey Is Nothing Then Return Nothing  '// 복호화 작업일경우 키를 새로 생성하지 않는다.
        '    If Not File.Exists(DePath) Then NewKeys()
        '    bKey = System.IO.File.ReadAllBytes(DePath)
        'End If
        Dim Result As New List(Of Byte)
        For i = 0 To value.Length - 1 Step 2
            Result.Add(Convert.ToByte(value.Substring(i, 2), 16))
        Next
        Dim B() As Byte = Result.ToArray
        For i = 0 To B.Length - 1
            B(i) = bKey(B(i))
        Next
        Return System.Text.Encoding.UTF8.GetString(B)
    End Function
 
    Public Function EnCryptBytes(ByVal value() As Byte) As Byte()
        If aKey Is Nothing Then
            If Not File.Exists(EnPath) Then
                If MsgBox("키가 존재하지 않습니다. 키가 없으면 프로그램 동작에 문제가 있습니다. 키를 새로 생성할까요?", vbYesNo) = vbYes Then
                    NewKeys()
                End If
            End If
            aKey = System.IO.File.ReadAllBytes(EnPath)
        End If
        For i As Integer = 0 To value.Length - 1
            value(i) = aKey(value(i))
        Next
        Return value
    End Function
 
    Public Function DeCryptBytes(ByVal value() As Byte) As Byte()
        If bKey Is Nothing Then
            If Not File.Exists(EnPath) Then Return Nothing  '// 복호화시 키가 없으면 키를 생성하지 않는다.
            '    If MsgBox("키가 존재하지 않습니다. 키가 없으면 프로그램 동작에 문제가 있습니다. 키를 새로 생성할까요?", vbYesNo) = vbYes Then
            '        NewKeys()
            '    End If
            'End If
            aKey = System.IO.File.ReadAllBytes(EnPath)
        End If
        For i As Integer = 0 To value.Length - 1
            value(i) = bKey(value(i))
        Next
        Return value
    End Function
 
    Private Function xAdd(ByRef x As Collection, ByVal value As StringAs Boolean
        Try
            x.Add(value, value)
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function
End Class
 

댓글목록0

등록된 댓글이 없습니다.
게시판 전체검색