|
| 1 | +Imports System.Security.Cryptography |
| 2 | +Imports System.Text |
| 3 | +Imports SkyReader_GUI.frmMain |
| 4 | +Public Class AES |
| 5 | + 'Public Shared HeaderBytes() As Byte = Nothing |
| 6 | + 'Encryption Notes |
| 7 | + 'So for 24 block it will be- |
| 8 | + '<first 0x20 bytes of sector 0> <24> <0x35-byte constant> |
| 9 | + |
| 10 | + '<first 0x20 bytes of sector 0> <1-byte block index> <0x35-byte constant> |
| 11 | + 'The First 20 Bytes is 32 Decimal Values |
| 12 | + 'Why is the Middle Block 24 as Hex? |
| 13 | + 'The Constant is: 53 Values Decimal |
| 14 | + |
| 15 | + 'THEN, MD5 Hash the above bytes. |
| 16 | + 'Encryption/Decryption Code |
| 17 | + |
| 18 | + 'The Following blocks (Offsets) are NOT encrypted |
| 19 | + 'All Offsets are in Hex and counted as such. |
| 20 | + '0x000 Through 0x70 |
| 21 | + '0x0B0 |
| 22 | + '0x0F0 |
| 23 | + '0x130 |
| 24 | + '0x170 |
| 25 | + '0x1B0 |
| 26 | + '0x1F0 |
| 27 | + '0x230 |
| 28 | + '0x270 |
| 29 | + '0x2B0 |
| 30 | + '0x2F0 |
| 31 | + '0x330 |
| 32 | + '0x370 |
| 33 | + '0x3B0 |
| 34 | + '0X3F0 |
| 35 | + |
| 36 | + Shared HeaderBytes(31) As Byte |
| 37 | + Public Shared FullKey(85) As Byte |
| 38 | + Shared AES As New RijndaelManaged |
| 39 | + Public Shared Magic() As Byte = {&H20, &H43, &H6F, &H70, &H79, &H72, &H69, &H67, &H68, &H74, &H20, &H28, &H43, &H29, &H20, &H32, &H30, &H31, &H30, &H20, &H41, &H63, &H74, &H69, &H76, &H69, &H73, &H69, &H6F, &H6E, &H2E, &H20, &H41, &H6C, &H6C, &H20, &H52, &H69, &H67, &H68, &H74, &H73, &H20, &H52, &H65, &H73, &H65, &H72, &H76, &H65, &H64, &H2E, &H20} |
| 40 | + |
| 41 | + |
| 42 | + 'Get the Header |
| 43 | + Shared Sub Header() |
| 44 | + AES.Padding = PaddingMode.Zeros |
| 45 | + AES.Mode = CipherMode.ECB |
| 46 | + |
| 47 | + Dim Counter As Integer = 0 'Necessary to add one to the Byte array Offset |
| 48 | + 'Dim HeadByteCounter As Integer = 0 |
| 49 | + 'Go up to 0x21 to get all 32 Bytes of Header |
| 50 | + Do Until Counter = 32 |
| 51 | + HeaderBytes(Counter) = WholeFile(Counter) |
| 52 | + 'HeadByteCounter += &H1 |
| 53 | + Counter += 1 |
| 54 | + Loop |
| 55 | + 'MessageBox.Show(BitConverter.ToString(HeaderBytes), "Head") |
| 56 | + End Sub |
| 57 | + |
| 58 | + '#TODO: |
| 59 | + 'Get 16 Bytes read in to Byte array for Encryption/Decryption |
| 60 | + |
| 61 | + Shared Sub GetKey(ByVal AreaKey As Byte) |
| 62 | + ReDim FullKey(85) 'Reset and don't preserve |
| 63 | + HeaderBytes.CopyTo(FullKey, 0) |
| 64 | + 'MessageBox.Show(BitConverter.ToString(FullKey), "Header into FullKey") |
| 65 | + FullKey(32) = AreaKey |
| 66 | + 'MessageBox.Show(BitConverter.ToString(FullKey), "Header and Area into FullKey") |
| 67 | + Magic.CopyTo(FullKey, 33) |
| 68 | + |
| 69 | + 'FullKey now has the Correct Data. |
| 70 | + End Sub |
| 71 | + 'Encrypt |
| 72 | + Shared Function AESE(ByVal input As Byte(), ByVal key As Byte()) As Byte() |
| 73 | + Dim AES As New RijndaelManaged |
| 74 | + AES.Padding = PaddingMode.Zeros |
| 75 | + AES.Key = CalculateMD5Hash(key) |
| 76 | + AES.Mode = CipherMode.ECB |
| 77 | + Dim DESEncrypter As ICryptoTransform = AES.CreateEncryptor |
| 78 | + Dim Buffer As Byte() = input |
| 79 | + Return DESEncrypter.TransformFinalBlock(Buffer, 0, Buffer.Length) |
| 80 | + End Function |
| 81 | + 'Decrypt |
| 82 | + Shared Function AESD(ByVal input As Byte(), ByVal key As Byte()) As Byte() |
| 83 | + Dim AES As New RijndaelManaged |
| 84 | + AES.Padding = PaddingMode.Zeros |
| 85 | + AES.Key = CalculateMD5Hash(key) |
| 86 | + AES.Mode = CipherMode.ECB |
| 87 | + Dim DESDecrypter As ICryptoTransform = AES.CreateDecryptor |
| 88 | + Dim Buffer As Byte() = input |
| 89 | + Return DESDecrypter.TransformFinalBlock(Buffer, 0, Buffer.Length) |
| 90 | + End Function |
| 91 | + |
| 92 | + 'Calculate MD5 |
| 93 | + Public Shared Function CalculateMD5Hash(ByVal input As Byte()) As Byte() |
| 94 | + Dim md5 As MD5 = MD5.Create() |
| 95 | + Dim hash As Byte() = md5.ComputeHash(input) |
| 96 | + Return hash |
| 97 | + End Function |
| 98 | + |
| 99 | + 'Return a Hex String from Byte Array. |
| 100 | + Public Shared Function ByteArrayToString(ba As Byte()) As String |
| 101 | + Dim hex As New StringBuilder(ba.Length * 2) |
| 102 | + For Each b As Byte In ba |
| 103 | + hex.AppendFormat("{0:x2}", b) |
| 104 | + Next |
| 105 | + Return hex.ToString() |
| 106 | + End Function |
| 107 | + |
| 108 | + 'This Generates the Bytes from the Hex String. |
| 109 | + Public Shared Function StringToByteArray(s As String) As Byte() |
| 110 | + ' remove any spaces from, e.g. "A0 20 34 34" |
| 111 | + 's = s.Replace(" "c, "") |
| 112 | + ' make sure we have an even number of digits |
| 113 | + If (s.Length And 1) = 1 Then |
| 114 | + Throw New FormatException("Odd string length when even string length is required.") |
| 115 | + End If |
| 116 | + |
| 117 | + ' calculate the length of the byte array and dim an array to that |
| 118 | + Dim nBytes = s.Length \ 2 |
| 119 | + Dim a(nBytes - 1) As Byte |
| 120 | + |
| 121 | + ' pick out every two bytes and convert them from hex representation |
| 122 | + For i = 0 To nBytes - 1 |
| 123 | + a(i) = Convert.ToByte(s.Substring(i * 2, 2), 16) |
| 124 | + Next |
| 125 | + |
| 126 | + Return a |
| 127 | + End Function |
| 128 | +End Class |
0 commit comments