Create a gist now

Instantly share code, notes, and snippets. example for SSO
Public Class UserData
Public uid As String
Public expires As String = DateTime.Now.AddDays(1).ToString("s",
Public customer_email As String
Public customer_name As String
End Class
Public Class MultiPass
Public status As Boolean = False
Public multipass As String
Public signature As String
End Class
Public Class Auth
Public Shared Function encryptUserData(user_data As UserData) As
Dim ret As New MultiPass
Dim site_key As String =
Dim api_key As String =
' Encode the data into a JSON object
Dim s As New JavaScriptSerializer()
Dim json_data As String = s.Serialize(user_data)
' Using byte arrays now instead of strings
Dim data As Byte() = Encoding.ASCII.GetBytes(json_data)
Dim iv As Byte() = Nothing
Dim encrypted As Byte() = Nothing
Dim prepended As Byte() = Nothing
Dim prepended_base64 As String = Nothing
Dim signature As Byte() = Nothing
'use the managed object to do the encryption..
Using aesAlg As New RijndaelManaged()
'define encryption details..
aesAlg.KeySize = 128
aesAlg.Mode = CipherMode.CBC
aesAlg.Padding = PaddingMode.PKCS7
'create the 16-byte salted hash (used as encryption key)..
Dim sha1__1 As SHA1 = SHA1.Create()
Dim saltedHash As Byte() =
sha1__1.ComputeHash(Encoding.ASCII.GetBytes(api_key & site_key))
Array.Resize(saltedHash, 16)
aesAlg.Key = saltedHash
'generate a random 16 byte IV..
iv = aesAlg.IV
'encrypt data..
Dim encryptor As ICryptoTransform =
aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV)
Using msEncrypt As New MemoryStream()
Using csEncrypt As New CryptoStream(msEncrypt, encryptor,
csEncrypt.Write(data, 0, data.Length)
End Using
encrypted = msEncrypt.ToArray()
End Using
End Using
'prepend encrypted data with the IV..
prepended = New Byte(iv.Length + (encrypted.Length - 1)) {}
iv.CopyTo(prepended, 0)
encrypted.CopyTo(prepended, iv.Length)
'base64 and url encode encrypted data..
prepended_base64 = Convert.ToBase64String(prepended,
ret.multipass = System.Web.HttpUtility.UrlEncode(prepended_base64)
'create signature..
Using myHMACSHA1 As New HMACSHA1(Encoding.ASCII.GetBytes(api_key))
signature =
ret.signature =
End Using
ret.status = True
End Function
End Class


What syntax highlight did you use for Gist for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment