' Collection of code snippets by Arne Vajhøj 
' (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
﻿Imports System
Imports System.Security.Cryptography
Imports System.Text

Namespace Encrypt
    Public Class TestSigning
        ' sign: Byte() + Byte() keys -> Byte()
        ' verify: Byte() + Byte() signature + Byte() keys -> bool
        Public Shared Function Sign(content As Byte(), keyblob As Byte(), ha As HashAlgorithm) As Byte()
            Dim alg As New RSACryptoServiceProvider()
            alg.ImportCspBlob(keyblob)
            Return alg.SignData(content, ha)
        End Function
        Public Shared Function Verify(content As Byte(), signature As Byte(), keyblob As Byte(), ha As HashAlgorithm) As Boolean
            Dim alg As New RSACryptoServiceProvider()
            alg.ImportCspBlob(keyblob)
            Return alg.VerifyData(content, ha, signature)
        End Function
        ' Base64 encode: Byte() -> String
        ' Base64 decode: String -> Byte()
        Private Shared Function Encode(b As Byte()) As String
            Return Convert.ToBase64String(b)
        End Function
        Private Shared Function Decode(s As String) As Byte()
            Return Convert.FromBase64String(s)
        End Function
        ' sign: String + Byte() keys -> Base64 String
        ' verify: String + Base64 String signature + Byte() keys -> bool
        Public Shared Function Sign(content As String, keyblob As Byte(), ha As HashAlgorithm) As String
            Return Encode(Sign(Encoding.UTF8.GetBytes(content), keyblob, ha))
        End Function
        Public Shared Function Verify(content As String, signature As String, keyblob As Byte(), ha As HashAlgorithm) As Boolean
            Return Verify(Encoding.UTF8.GetBytes(content), Decode(signature), keyblob, ha)
        End Function
        ' test
        Private Const TEXT As String = "This is some random text to be used to test encryption."
        Private Shared Sub Test(label As String, bits As Integer, ha As HashAlgorithm)
            Console.WriteLine(label & ":")
            ' generate keys
            Dim alg As New RSACryptoServiceProvider() With { _
                .KeySize = bits _
            }
            Dim keyblob As Byte() = alg.ExportCspBlob(True)
            ' real test
            Console.WriteLine("  " & TEXT)
            Dim sig As String = Sign(TEXT, keyblob, ha)
            Console.WriteLine("  " & sig)
            Dim ok As Boolean = Verify(TEXT, sig, keyblob, ha)
            Console.WriteLine("  " & ok)
            Dim ok2 As Boolean = Verify(TEXT & "X", sig, keyblob, ha)
            Console.WriteLine("  " & ok2)
        End Sub
        Public Shared Sub Main(args As String())
            Test("SHA-1 160 bit with RSA 3072 bit", 3072, New SHA1CryptoServiceProvider())
            Test("SHA-2 256 bit with RSA 3072 bit", 3072, New SHA256CryptoServiceProvider())
            Test("SHA-2 512 bit with RSA 3072 bit", 3072, New SHA512CryptoServiceProvider())
        End Sub
    End Class
End Namespace
