Avatar billede SommerFyr Seniormester
25. marts 2020 - 10:23 Der er 2 kommentarer og
1 løsning

fra C shap to vb.net

Hej jeg har lidt problemer med at omskrive dette script til vb.net

using System;
using System.Linq;
using System.Security.Cryptography;

namespace TOTP.Lib
{
    public static class EncodingExtensions2
    {
        private static readonly byte[] mapping = { 26, 27, 28, 29, 30, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 };

        public static byte[] ToByteArray(this string secret)
        {
            secret = secret.ToUpperInvariant();
            byte[] byteArray = new byte[(secret.Length + 7) / 8 * 5];

            long shiftingNum = 0L;
            int srcCounter = 0;
            int destCounter = 0;
            for (int i = 0; i < secret.Length; i++)
            {
                long num = (long)mapping[secret[i] - 50];
                shiftingNum |= num << (35 - srcCounter * 5);

                if (srcCounter == 7 || i == secret.Length - 1)
                {
                    for (int j = 0; j < 5; j++)
                    {
                        byteArray[destCounter++] = (byte)((shiftingNum >> (32 - j * 8)) & 0xff);
                    }
                    shiftingNum = 0L;
                }
                srcCounter = (srcCounter + 1) % 8;
            }

            return byteArray;
        }
    }

    public static class TOTP
    {
        public static string GetHotp(string base32EncodedSecret, long counter)  {
            byte[] message = BitConverter.GetBytes(counter).Reverse().ToArray(); // Assuming Intel machine (little endian)
            byte[] secret = base32EncodedSecret.ToByteArray();

            byte[] hash;
            using (HMACSHA1 hmac = new HMACSHA1(secret, true)) {
                hash = hmac.ComputeHash(message);
            }
            int offset = hash[hash.Length - 1] & 0xf;
            int truncatedHash = ((hash[offset] & 0x7f) << 24) | ((hash[offset + 1] & 0xff) << 16) | ((hash[offset + 2] & 0xff) << 8) | (hash[offset + 3] & 0xff);
            int hotp = truncatedHash % 1000000; // 6-digit code and hence 10 power 6, that is a million
            return hotp.ToString("D6");
        }

        public static string GetTotp(string base32EncodedSecret)
        {
            DateTime epochStart = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
            long counter = (long)Math.Floor((DateTime.UtcNow - epochStart).TotalSeconds / 30);
            return GetHotp(base32EncodedSecret, counter);
        }
    }

    public static class EncodingExtensions
    {
        private static readonly byte[] mapping = { 26, 27, 28, 29, 30, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25};

        public static byte[] ToByteArray(this string secret)
        {
            secret = secret.ToUpperInvariant();
            byte[] byteArray = new byte[(secret.Length + 7) / 8 * 5];

            long shiftingNum = 0L;
            int srcCounter = 0;
            int destCounter = 0;
            for (int i = 0; i < secret.Length; i++)
            {
                long num = (long)mapping[secret[i] - 50];
                shiftingNum |= num << (35 - srcCounter * 5);

                if (srcCounter == 7 || i == secret.Length - 1)
                {
                    for (int j = 0; j < 5; j++)
                    {
                        byteArray[destCounter++] = (byte)((shiftingNum >> (32 - j * 8)) & 0xff);
                    }
                    shiftingNum = 0L;
                }
                srcCounter = (srcCounter + 1) % 8;
            }

            return byteArray;
        }
    }
}

Har prøve flere steder uden held..  så håber der er en der kan hjælpe
Avatar billede SommerFyr Seniormester
25. marts 2020 - 10:25 #1
scripte kommer her fra https://github.com/zhiliangxu/TOTP
Avatar billede arne_v Ekspert
25. marts 2020 - 13:42 #2
Disse to stykke rkode synes at opføre sig ens:


using System;
using System.Linq;
using System.Security.Cryptography;
using System.Threading;

namespace TOTP.Lib
{
    public static class TOTP
    {
        public static string GetHotp(string base32EncodedSecret, long counter)  {
            byte[] message = BitConverter.GetBytes(counter).Reverse().ToArray(); // Assuming Intel machine (little endian)
            byte[] secret = base32EncodedSecret.ToByteArray();

            byte[] hash;
            using (HMACSHA1 hmac = new HMACSHA1(secret, true)) {
                hash = hmac.ComputeHash(message);
            }
            int offset = hash[hash.Length - 1] & 0xf;
            int truncatedHash = ((hash[offset] & 0x7f) << 24) | ((hash[offset + 1] & 0xff) << 16) | ((hash[offset + 2] & 0xff) << 8) | (hash[offset + 3] & 0xff);
            int hotp = truncatedHash % 1000000; // 6-digit code and hence 10 power 6, that is a million
            return hotp.ToString("D6");
        }

        public static string GetTotp(string base32EncodedSecret)
        {
            DateTime epochStart = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
            long counter = (long)Math.Floor((DateTime.UtcNow - epochStart).TotalSeconds / 30);
            return GetHotp(base32EncodedSecret, counter);
        }
    }

    public static class EncodingExtensions
    {
        private static readonly byte[] mapping = { 26, 27, 28, 29, 30, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25};

        public static byte[] ToByteArray(this string secret)
        {
            secret = secret.ToUpperInvariant();
            byte[] byteArray = new byte[(secret.Length + 7) / 8 * 5];

            long shiftingNum = 0L;
            int srcCounter = 0;
            int destCounter = 0;
            for (int i = 0; i < secret.Length; i++)
            {
                long num = (long)mapping[secret[i] - 50];
                shiftingNum |= num << (35 - srcCounter * 5);

                if (srcCounter == 7 || i == secret.Length - 1)
                {
                    for (int j = 0; j < 5; j++)
                    {
                        byteArray[destCounter++] = (byte)((shiftingNum >> (32 - j * 8)) & 0xff);
                    }
                    shiftingNum = 0L;
                }
                srcCounter = (srcCounter + 1) % 8;
            }

            return byteArray;
        }
    }
}

namespace TOTP.App
{
    public class Program
    {
        public static void Main(string[] args)
        {
            string secret = "HOOZZ4QRPSWBNPAN";
            while (true)
            {
                Console.WriteLine("{0} {1}", DateTime.Now, TOTP.Lib.TOTP.GetTotp(secret));
                Thread.Sleep(1000 * 3);
            }
        }
    }
}



Imports System
Imports System.Linq
Imports System.Security.Cryptography
Imports System.Threading
Imports System.Runtime.CompilerServices

Module EncodingExtensions
    Private ReadOnly mapping As Byte() = {26, 27, 28, 29, 30, 255, _
        255, 255, 255, 255, 255, 255, _
        255, 255, 255, 0, 1, 2, _
        3, 4, 5, 6, 7, 8, _
        9, 10, 11, 12, 13, 14, _
        15, 16, 17, 18, 19, 20, _
    21, 22, 23, 24, 25}
    <Extension> _
    Public Function ToByteArray(secret As String) As Byte()
        secret = secret.ToUpperInvariant()
        Dim byteArray As Byte() = New Byte(((secret.Length + 7) \ 8) * 5 - 1) {}
        Dim shiftingNum As Long = 0L
        Dim srcCounter As Integer = 0
        Dim destCounter As Integer = 0
        For i As Integer = 0 To secret.Length - 1
            Dim num As Long = CLng(mapping(AscW(secret(i)) - 50))
            shiftingNum = shiftingNum Or num << (35 - srcCounter * 5)

            If srcCounter = 7 OrElse i = secret.Length - 1 Then
                For j As Integer = 0 To 4
                    byteArray(destCounter) = CByte((shiftingNum >> (32 - j * 8)) And &Hff)
                    destCounter = destCounter + 1
                Next
                shiftingNum = 0L
            End If
            srcCounter = (srcCounter + 1) Mod 8
        Next
        Return byteArray
    End Function
End Module

Namespace TOTP.Lib
    Public NotInheritable Class TOTP
        Private Sub New()
        End Sub
        Public Shared Function GetHotp(base32EncodedSecret As String, counter As Long) As String
            Dim message As Byte() = BitConverter.GetBytes(counter).Reverse().ToArray()
            ' Assuming Intel machine (little endian)
            Dim secret As Byte() = base32EncodedSecret.ToByteArray()
            Dim hash As Byte()
            Using hmac As New HMACSHA1(secret, True)
                hash = hmac.ComputeHash(message)
            End Using
            Dim offset As Integer = hash(hash.Length - 1) And &Hf
            Dim truncatedHash As Integer = ((hash(offset) And &H7f) << 24) Or ((hash(offset + 1) And &Hff) << 16) Or ((hash(offset + 2) And &Hff) << 8) Or (hash(offset + 3) And &Hff)
            Dim hotp As Integer = truncatedHash Mod 1000000
            ' 6-digit code and hence 10 power 6, that is a million
            Return hotp.ToString("D6")
        End Function

        Public Shared Function GetTotp(base32EncodedSecret As String) As String
            Dim epochStart As New DateTime(1970, 1, 1, 0, 0, 0, _
                0, DateTimeKind.Utc)
            Dim counter As Long = CLng(Math.Truncate(Math.Floor((DateTime.UtcNow - epochStart).TotalSeconds / 30)))
            Return GetHotp(base32EncodedSecret, counter)
        End Function
    End Class
End Namespace

Namespace TOTP.App
    Public Class Program
        Public Shared Sub Main(args As String())
            Dim secret As String = "HOOZZ4QRPSWBNPAN"
            While True
                Console.WriteLine("{0} {1}", DateTime.Now, TOTP.[Lib].TOTP.GetTotp(secret))
                Thread.Sleep(1000 * 3)
            End While
        End Sub
    End Class
End Namespace
Avatar billede SommerFyr Seniormester
25. marts 2020 - 14:01 #3
Takker arne_v Det var lige det jeg skulle bruge..
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
IT-kurser om Microsoft 365, sikkerhed, personlig vækst, udvikling, digital markedsføring, grafisk design, SAP og forretningsanalyse.

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester