Sebenarnya lama dah aku nak try buat random IV untuk Rjindael Encryption ni. Actually Jimin pernah tunjuk kat aku tapi tu dah setahun yang lepas rasanya. Mostly, sample kat internet dia buat ambik dari Hash (SHA atau MD5) dari salt. 32 byte pakai untuk key , 16 byte pakai untuk IV.
Semalam aku sempat try sambil2 aku tolong staf aku buat secure config.
Apa kejadahnya IV ni?
IV ni sebenarnya adalah Initiallized Vector. IV ni akan menentukan output encryption. kalau pakai statik IV, password yang sama dan data yang sama, hasil output akan konsisten je setiap kali encrypt. Kalau pakai random IV ni, password sama dan data yang sama, hasil outputnya akan berbeza setiap kali encrypt. Jadi, ini menjadikan ia lebih susah untuk dihack(aku rasala). Tambah2 lagi kalau digunakan dalam watermarking atau steganography.
Nak cerita lebih2 pun payah, takut x paham pulak. aku letak code dia kat sini. Korang try la sendiri kalau nak la. X nak sudah..... hahahhahah...
Imports System.Security.Cryptography
Imports System.IO
Public Class clsAESJCE
Public Shared Function Encrypt(ByVal data() As Byte, ByVal key As String) As Byte()
Dim ms As New MemoryStream
Dim AES As New System.Security.Cryptography.RijndaelManaged
Dim SHA As New SHA512Managed
Dim keybyte() As Byte = System.Text.ASCIIEncoding.ASCII.GetBytes(key.ToCharArray)
Dim HashByte() As Byte = SHA.ComputeHash(keybyte)
Dim keys(31) As Byte
Dim IV(15) As Byte
System.Array.Copy(HashByte, 0, keys, 0, 32)
'System.Array.Copy(HashByte, 32, IV, 0, 16)
AES.KeySize = 256
'AES.BlockSize = 256
AES.Mode = CipherMode.CBC
AES.Padding = PaddingMode.PKCS7
AES.GenerateIV()
Dim newdata() As Byte
Try
Dim objCryptoStream As CryptoStream
objCryptoStream = New CryptoStream(ms, AES.CreateEncryptor(keys, IV), CryptoStreamMode.Write)
objCryptoStream.Write(data, 0, data.Length)
objCryptoStream.FlushFinalBlock()
newdata = ms.ToArray()
objCryptoStream.Close()
ms.Close()
Catch ex As Exception
End Try
Return newdata
End Function
Public Shared Function Decrypt(ByVal data() As Byte, ByVal key As String) As Byte()
Dim ms As New MemoryStream(data)
Dim AES As New System.Security.Cryptography.RijndaelManaged
Dim SHA As New SHA512Managed
Dim keybyte() As Byte = System.Text.ASCIIEncoding.ASCII.GetBytes(key.ToCharArray)
Dim HashByte() As Byte = SHA.ComputeHash(keybyte)
Dim keys(31) As Byte
Dim IV(15) As Byte
System.Array.Copy(HashByte, 0, keys, 0, 32)
'System.Array.Copy(HashByte, 32, IV, 0, 16)
AES.KeySize = 256
'AES.BlockSize = 256
AES.Mode = CipherMode.CBC
AES.Padding = PaddingMode.PKCS7
AES.GenerateIV()
Dim newdata(data.Length - 1) As Byte
Dim iRead As Integer = -1
Try
Dim objCryptoStream As CryptoStream
objCryptoStream = New CryptoStream(ms, AES.CreateDecryptor(keys, IV), CryptoStreamMode.Read)
iRead = objCryptoStream.Read(newdata, 0, newdata.Length)
objCryptoStream.FlushFinalBlock()
objCryptoStream.Close()
ms.Close()
Catch ex As Exception
End Try
ReDim data(iRead - 1)
System.Array.Copy(newdata, 0, data, 0, iRead)
Return data
End Function
End Class
Malaya Merdeka - from Ash's vinyl collection
9 months ago
0 comments:
Post a Comment