Advertisement
Guest User

Encodings

a guest
Aug 22nd, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.57 KB | None | 0 0
  1. Sub Main
  2.  
  3.  
  4.     Dim input As String = "My name is Inigo Montoya. You killed my father. Prepare to die.ꬾ"
  5.    
  6.     Dim encodings = {
  7.     Encoding.Unicode,
  8.     Encoding.BigEndianUnicode,
  9.     Encoding.UTF8,
  10.     New UTF8Encoding(False),
  11.     Encoding.UTF7,
  12.     Encoding.UTF32,
  13.     Encoding.ASCII,
  14.     Encoding.Default}.Concat(Encoding.GetEncodings().Select(Function(e) e.GetEncoding)).ToArray
  15.  
  16.     encodings.Select(Function(e, i) New With {.Index = i, .Encoding = e, .Preamble= String.Join(" ",array.ConvertAll(e.GetPreamble,Function(b) b.ToString("X2")))}).Dump("Encodings")
  17.     Using aes As System.Security.Cryptography.aes = System.Security.Cryptography.Aes.Create()
  18.  
  19.         Dim index As Integer = Integer.Parse(Util.ReadLine("Pick an encoding (0) for unicode, (6) for Ascii, (2,3) for UTF8(BOM,no BOM)")) Mod encodings.Count
  20.         Util.ClearResults
  21.  
  22.         Dim picked = encodings(index).Dump("Picked encoding")
  23.         String.Join(" ",array.ConvertAll(picked.GetPreamble,Function(b) b.ToString("X2"))).Dump("Preamble")
  24.  
  25.  
  26.         Dim encoded = encodings.Select(Function(e) New With {
  27.         .input = input,
  28.         .Encoding = e,
  29.         .Bytes = encode(input, e),
  30.         .BytesToHex = String.Join(" ", Array.ConvertAll(.Bytes, Function(b) b.ToString("X2"))),
  31.         .Encrypted = EncryptBytes_Aes(.Bytes, aes),
  32.         .EncryptedToHex = String.Join(" ", Array.ConvertAll(.Encrypted, Function(b) b.ToString("X2")))
  33.         })
  34.  
  35.  
  36.         Dim decoded = encoded.Select(Function(enc) New With {.Encoded = enc, .Decoded = encodings.Select(Function(de) New With {
  37.         .Encoding = de,
  38.         .BytesToString = Decode(enc.Bytes, de),
  39.         .EncryptedBytesToString = Decode(enc.Encrypted, de),
  40.         .Decrypted = New With {
  41.             .Decrypted = DecryptBytes_Aes(enc.Encrypted, aes),
  42.             .DecryptedBytesToHex = String.Join(" ", Array.ConvertAll(.Decrypted, Function(b) b.ToString("X2"))),
  43.             .DecryptedToString = Decode(.Decrypted, de),
  44.             .DecryptedEqualsBytes = Enumerable.SequenceEqual(enc.Bytes, .Decrypted)}
  45.             })
  46.         })
  47.  
  48.         'verbose info
  49.         decoded(index).Dump("Decoding Info")
  50.         'decoded.Dump  'Or use this to dump all options
  51.  
  52.  
  53.         Dim output = From de In decoded
  54.                      Select New With {.EncodedBy = de.Encoded.Encoding.EncodingName, .Decoded = de.Decoded.Select(Function(d) New With {.DecodingName = d.Encoding.EncodingName, .String = d.BytesToString, .StringFromEncrypted = d.EncryptedBytesToString})}
  55.  
  56.         output(index).Dump("Decoded Strings")
  57.  
  58.  
  59.     End Using
  60.  
  61.  
  62.  
  63. End Sub
  64.  
  65. Function Decode(bytes As Byte(), Enc As System.Text.Encoding) As String
  66.     Return enc.GetString(bytes)
  67. End Function
  68.  
  69.  
  70. Function encode(str As String, Enc As System.Text.Encoding) As Byte()
  71.     Return enc.GetPreamble.Concat( enc.GetBytes(str)).ToArray
  72. End Function
  73.  
  74.  
  75.  
  76.  
  77. Function EncryptBytes_Aes(ByVal plainTextBytes As Byte(), AESProvider As Aes) As Byte()
  78.  
  79.     Dim encryptor As ICryptoTransform = AESProvider.CreateEncryptor()
  80.  
  81.     Using msEncrypt As New MemoryStream()
  82.         Using csEncrypt As New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)
  83.             Using swEncrypt As New BinaryWriter(csEncrypt)
  84.                 swEncrypt.Write(plainTextBytes)
  85.             End Using
  86.             Return msEncrypt.ToArray()
  87.         End Using
  88.     End Using
  89.  
  90. End Function
  91.  
  92. Function DecryptBytes_Aes(ByVal cipherText() As Byte, AESProvider As Aes) As Byte()
  93.  
  94.     Dim decryptor As ICryptoTransform = AESProvider.CreateDecryptor()
  95.     Dim output As New List(Of Byte)
  96.  
  97.     Using msDecrypt As New MemoryStream(cipherText)
  98.  
  99.         Using csDecrypt As New CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)
  100.  
  101.             Dim tmp As Integer = csDecrypt.ReadByte
  102.             While tmp <> -1
  103.                 output.Add(CByte(tmp))
  104.                 tmp = csDecrypt.ReadByte
  105.             End While
  106.             Return output.ToArray
  107.         End Using
  108.     End Using
  109. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement