| 暗号化を行うクラス | ||
Imports System.Security.Cryptography
'文字列の暗号化、復号化を行うクラス
Public Class Crypto
'キー
Private mKey As Byte()
'初期化ベクタ
Private mIV As Byte()
'暗号化キー
Public Property Key() As Byte()
Get
Return mKey
End Get
Set(ByVal Value As Byte())
mKey = Value
End Set
End Property
'初期ベクタ
Public Property IV() As Byte()
Get
Return mIV
End Get
Set(ByVal Value As Byte())
mIV = Value
End Set
End Property
'文字列の暗号化
Public Function Encrypt(ByVal value As String,Byval size as Integer) As Byte()
Debug.Assert(Not (mKey Is Nothing))
Debug.Assert(Not (mIV Is Nothing))
Try
If value.Trim = "" Then
Return Nothing
End If
Dim byteArray(size) As Byte
Dim ms As New IO.MemoryStream(byteArray)
'Dim ms As New IO.MemoryStream()
Dim rm As New RijndaelManaged
Dim cs As New CryptoStream(ms, rm.CreateEncryptor(mKey, mIV), _
CryptoStreamMode.Write)
Dim sw As New IO.StreamWriter(cs)
'ストリームに書き出し
sw.WriteLine(value)
'書き込まれたバッファを読み込み
'byteArray = ms.GetBuffer
sw.Close()
cs.Close()
Return byteArray
Catch ex As IndexOutOfRangeException
Throw New InvalidOperationException("Keyは8バイト、IVは16バイトの指定が必要です")
Catch ex As Exception
Throw ex
End Try
End Function
'バイトデータを復号化
Public Function Decrypt(ByVal value As Byte()) As String
Debug.Assert(Not (mKey Is Nothing))
Debug.Assert(Not (mIV Is Nothing))
'何もデータがないときは空欄を返す
If value Is Nothing Then
Return ""
End If
Dim ms As New IO.MemoryStream(value)
Dim rm As New RijndaelManaged
Dim cs As New CryptoStream(ms, rm.CreateDecryptor(mKey, mIV), CryptoStreamMode.Read)
Dim sr As New IO.StreamReader(cs)
Dim retStr As String
retStr = sr.ReadLine
sr.Close()
cs.Close()
Return retStr
End Function
End Class
|
| 呼び出し側 | ||
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows フォーム デザイナで生成されたコード "
Public Sub New()
MyBase.New()
' この呼び出しは Windows フォーム デザイナで必要です。
InitializeComponent()
' InitializeComponent() 呼び出しの後に初期化を追加します。
End Sub
' Form は、コンポーネント一覧に後処理を実行するために dispose をオーバーライドします。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
' Windows フォーム デザイナで必要です。
Private components As System.ComponentModel.IContainer
' メモ : 以下のプロシージャは、Windows フォーム デザイナで必要です。
'Windows フォーム デザイナを使って変更してください。
' コード エディタを使って変更しないでください。
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(64, 72)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(136, 32)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 12)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim cr As New Crypto
Dim key As Byte() = {&H41, &H42, &H43, &H44, &H45, &H46, &H47, &H33, &H41, &H42, &H43, &H44, &H45, _
&H46, &H47, &H33, &H41, &H42, &H43, &H44, &H45, &H46, &H47, &H33, &H41, &H42, _
&H43, &H44, &H45, &H46, &H47, &H33}
Dim iv As Byte() = {&H0, &H1, &H2, &H3, &H4, &H5, &H6, &H7, &H8, &H9, &HA, &HB, &HC, &HD, &HE, &HE}
cr.Key = key
cr.IV = iv
Dim str As String = "12345678901234567"
Console.WriteLine("元の値:" & str)
Dim ret As Byte()
ret = cr.Encrypt(str, 31)
Dim retStr As String
retStr = cr.Decrypt(ret)
Console.WriteLine("結果:" & retStr)
End Sub
End Class
|