PDA

View Full Version : تبدیل زبان vb.net به vb6



ARData
پنج شنبه 08 مهر 1400, 15:16 عصر
سلام دوستان
چندتا کد .net داشتم میخواستم بدونم امکان تبدیلش به vb6 هست یا نه ؟
ممنون از شما سروران
Namespace Bitcoin.BIP39
''' <summary>
''' A .NET implementation of the Bitcoin Improvement Proposal - 39 (BIP39)
''' BIP39 specification used as reference located here: https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki
''' Made by thashiznets@yahoo.com.au
''' v1.0.1.1
''' I ? Bitcoin :)
''' Bitcoin:1ETQjMkR1NNh4jwLuN5LxY7bMsHC9PUPSV
''' </summary>
Public Class BIP39
#region Private Attributes

Private Byte() As Friend _entropyBytes
Private Byte() As Friend _passphraseBytes
'C++‎‎‎‎ TO VB CONVERTER NOTE: This member was renamed since VB does not allow members with the same name as nested types of the enclosing type:
Private Language_Renamed As Friend _language = New Friend()
Private List As Friend (Of Integer) _wordIndexList = New Friend() 'I made this a property because then we can keep the same index and swap between languages for experimenting
Private String As Friend _mnemonicSentence = New Friend()

#endregion

#region Public Constants and Enums

Private ReadOnly Integer As Public cMinimumEntropyBits = 128
Private ReadOnly Integer As Public cMaximumEntropyBits = 8192
Private ReadOnly Integer As Public cEntropyMultiple = 32
Private ReadOnly Integer As Public cBitsInByte = 8
Private ReadOnly Integer As Public cBitGroupSize = 11
Private ReadOnly String As Public cEmptyString = ""
Private ReadOnly String As Public cSaltHeader = "mnemonic" 'this is the first part of the salt as described in the BIP39 spec
Public Enum language
English
Japanese
Spanish
ChineseSimplified
ChineseTraditional
French
Unknown
End Enum
Private ReadOnly String As Public cJPSpaceString = ChrW(&H3000).ToString() 'ideographic space used by japanese language

#endregion

#region Constructors

''' <summary>
''' Constructor to build a BIP39 object from scratch given an entropy size and an optional passphrase. Language is optional and will default to English
''' </summary>
''' <param name="entropySize">The size in bits of the entropy to be created</param>
''' <param name="passphrase">The optional passphrase. Please ensure NFKD Normalized, Empty string will be used if not provided as per spec</param>
''' <param name="language">The optional language. If no language is provided English will be used</param>
Public Sub New(Optional ByVal entropySize As Integer = cMinimumEntropyBits, Optional ByVal passphrase As String = cEmptyString, Optional ByVal language As Language = Language.English)
'check that ENT size is a multiple of 32 and at least minimun entropy size to stop silly people using tiny entropy, oh also making sure entropy size doesn't exceed our checksum bits available
If entropySize Mod cEntropyMultiple <> 0 OrElse entropySize < cMinimumEntropyBits OrElse entropySize > cMaximumEntropyBits Then
Throw (New Exception("entropy size must be a multiple of " & cEntropyMultiple & " (divisible by " & cEntropyMultiple & " with no remainder) and must be greater than " & (cMinimumEntropyBits-1) & " and less than " & (cMaximumEntropyBits+1)))
End If

'C++‎‎‎‎ TO VB CONVERTER WARNING: C++‎‎‎‎ to VB Converter cannot determine whether both operands of this division are integer types - if they are then you should use the VB integer division operator:
_entropyBytes = Utilities.GetRandomBytes(entropySize / cBitsInByte) 'crypto random entropy of the specified size
pInit(passphrase, language)
End Function

''' <summary>
''' Constructor to build a BIP39 object using supplied entropy bytes eighter from a previously created BIP39 object or another method of entropy generation.
''' </summary>
''' <param name="entropyBytes">The entropy bytes which will determine the mnemonic sentence</param>
''' <param name="passphrase">The optional passphrase. Please ensure NFKD Normalized, Empty string will be used if not supplied as per spec</param>
''' <param name="language">The optional language. If no language is provided English will be used</param>
Public Sub New(ByVal entropyBytes() As Byte, Optional ByVal passphrase As String = cEmptyString, Optional ByVal language As Language = Language.English)
'check to ensure at least 16 bytes no more than 1024 bytes and byte array is in 4 byte groups
If (entropyBytes.Length * cBitsInByte) Mod cEntropyMultiple <> 0 OrElse (entropyBytes.Length * cBitsInByte) < cMinimumEntropyBits Then
'C++‎‎‎‎ TO VB CONVERTER WARNING: C++‎‎‎‎ to VB Converter cannot determine whether both operands of this division are integer types - if they are then you should use the VB integer division operator:
Throw (New Exception("entropy bytes must be a multiple of " & (cEntropyMultiple / cBitsInByte) & " (divisible by " & (cEntropyMultiple / cBitsInByte) & " with no remainder) and must be greater than " & ((cMinimumEntropyBits / cBitsInByte) - 1) & " bytes and less than " & ((cMaximumEntropyBits / cBitsInByte) + 1) & " bytes"))
End If

_entropyBytes = entropyBytes
pInit(passphrase, language)
End Function

''' <summary>
''' Constructor to build a BIP39 object using a supplied Mnemonic sentence and passphrase. If you are not worried about saving the entropy bytes, or using custom words not in a wordlist, you should consider the static method to do this instead.
''' </summary>
''' <param name="mnemonicSentence">The mnemonic sentencs used to derive seed bytes, Please ensure NFKD Normalized</param>
''' <param name="passphrase">Optional passphrase used to protect seed bytes, defaults to empty</param>
''' <param name="language">Optional language to use for wordlist, if not specified it will auto detect language and if it can't detect it will default to English</param>
Public Sub New(ByVal mnemonicSentence As String, Optional ByVal passphrase As String = cEmptyString, Optional ByVal language As Language = Language.Unknown)
_mnemonicSentence = Utilities.NormaliseStringNfkd(mnemonicSentence.Tri m()) 'just making sure we don't have any leading or trailing spaces
_passphraseBytes = UTF8Encoding.UTF8.GetBytes(Utilities.NormaliseStri ngNfkd(passphrase))
Dim words() As String = _mnemonicSentence.Split(New SByte() { " "c })

'no language specified try auto detect it
If language.Equals(Me.Language_Renamed.Unknown) Then
_language = AutoDetectLanguageOfWords(words)

If _language.Equals(Me.Language_Renamed.Unknown) Then
'yeah.....have a bias to use English as default....
_language = Me.Language_Renamed.English
End If
End If

'if the sentence is not at least 12 characters or cleanly divisible by 3, it is bad!
If words.Length < 12 OrElse words.Length Mod 3 <> 0 Then
Throw New Exception("Mnemonic sentence must be at least 12 words and it will increase by 3 words for each increment in entropy. Please ensure your sentence is at leas 12 words and has no remainder when word count is divided by 3")
End If

_language = language
_wordIndexList = pRebuildWordIndexes(words)
_entropyBytes = pProcessIntToBitsThenBytes(_wordIndexList)
End Function

#endregion

#region Public Static Methods

''' <summary>
''' An asynchronous static method to create a new BIP39 from random entropy. The random entropy creation is CPU intensive so is run in its own Task and we await as per async pattern.
''' </summary>
''' <param name="entropySize">The size in bits of the entropy to be created</param>
''' <param name="passphrase">The optional passphrase. Please ensure NFKD Normalized, Empty string will be used if not provided as per spec</param>
''' <param name="language">The optional language. If no language is provided English will be used</param>
''' <returns>A BIP39 object</returns>
Public Shared async Function GetBIP39Async(Optional ByVal entropySize As Integer = cMinimumEntropyBits, Optional ByVal passphrase As String = cEmptyString, Optional ByVal language As Language = Language.English) As Task(Of BIP39)
'C++‎‎‎‎ TO VB CONVERTER WARNING: C++‎‎‎‎ to VB Converter cannot determine whether both operands of this division are integer types - if they are then you should use the VB integer division operator:
Dim entropyBytes() As Byte = await Utilities.GetRandomBytesAsync(entropySize / cBitsInByte)
Return New BIP39(entropyBytes, passphrase, language)
End Function

''' <summary>
''' Takes in a string[] of words and detects the language that has the highest number of matching words.
''' </summary>
''' <param name="words">The words of which you wish to derive a language</param>
''' <returns>The best attempt at a guessed Language</returns>
Public Shared Function AutoDetectLanguageOfWords(ByVal words() As String) As Language
Dim eng As New Wordlists.English()
Dim jp As New Wordlists.Japanese()
Dim es As New Wordlists.Spanish()
Dim fr As New Wordlists.French()
Dim cnS As New Wordlists.ChineseSimplified()
Dim cnT As New Wordlists.ChineseTraditional()

Dim languageCount As New List(Of Integer)(New Integer() {0,0,0,0,0,0})
Dim index As Integer

foreach(String s in words)
If True Then
If eng.WordExists(s,out index) Then
'english is at 0
languageCount(0) += 1
End If

If jp.WordExists(s, out index) Then
'japanese is at 1
languageCount(1) += 1
End If

If es.WordExists(s, out index) Then
'spanish is at 2
languageCount(2) += 1
End If

If cnS.WordExists(s, out index) Then
'chinese simplified is at 3
languageCount(3) += 1
End If

If cnT.WordExists(s, out index) AndAlso Not cnS.WordExists(s, out index) Then
'chinese traditional is at 4
languageCount(4) += 1
End If

If fr.WordExists(s, out index) Then
'french is at 5
languageCount(5) += 1
End If
End If

'no hits found for any language unknown
If languageCount.Max() = 0 Then
Return Language_Renamed.Unknown
End If

If languageCount.IndexOf(languageCount.Max()) = 0 Then
Return Language_Renamed.English
ElseIf languageCount.IndexOf(languageCount.Max()) = 1 Then
Return Language_Renamed.Japanese
ElseIf languageCount.IndexOf(languageCount.Max()) = 2 Then
Return Language_Renamed.Spanish
ElseIf languageCount.IndexOf(languageCount.Max()) = 3 Then
If languageCount(4) > 0 Then
'has traditional characters so not simplified but instead traditional
Return Language_Renamed.ChineseTraditional
End If

Return Language_Renamed.ChineseSimplified
ElseIf languageCount.IndexOf(languageCount.Max()) = 4 Then
Return Language_Renamed.ChineseTraditional
ElseIf languageCount.IndexOf(languageCount.Max()) = 5 Then
Return Language_Renamed.French
End If

Return Language_Renamed.Unknown
End Function

''' <summary>
''' Supply a mnemonic sentence with any words of your choosing not restricted to wordlists and be given seed bytes in return
''' </summary>
''' <param name="mnemonicSentence">The mnemonic sentence we will use to derive seed bytes, Please ensure NFKD Normalized</param>
''' <param name="passphrase">Optional passphrase to protect the seed bytes, Please ensure NFKD Normalized, defaults to empty string</param>
''' <returns>Seed bytes that can be used to create a root in BIP32</returns>
Public Shared Function GetSeedBytes(ByVal mnemonicSentence As String, Optional ByVal passphrase As String = cEmptyString) As Byte()
mnemonicSentence = Utilities.NormaliseStringNfkd(mnemonicSentence)
Dim salt() As Byte = Utilities.MergeByteArrays(UTF8Encoding.UTF8.GetByt es(cSaltHeader), UTF8Encoding.UTF8.GetBytes(Utilities.NormaliseStri ngNfkd(passphrase)))
Return Rfc2898_pbkdf2_hmacsha512.PBKDF2(UTF8Encoding.UTF8 .GetBytes(mnemonicSentence), salt)
End Function

''' <summary>
''' Supply a mnemonic sentence with any words of your choosing not restricted to wordlists and be given seed bytes hex encoded as a string in return
''' </summary>
''' <param name="mnemonicSentence">The mnemonic sentence we will use to derive seed bytes</param>
''' <param name="passphrase">Optional passphrase to protect the seed bytes, defaults to empty string</param>
''' <returns>Hex string encoded seed bytes that can be used to create a root in BIP32</returns>
Public Shared Function GetSeedBytesHexString(ByVal mnemonicSentence As String, Optional ByVal passphrase As String = cEmptyString) As String
mnemonicSentence = Utilities.NormaliseStringNfkd(mnemonicSentence)
Dim salt() As Byte = Utilities.MergeByteArrays(UTF8Encoding.UTF8.GetByt es(cSaltHeader), UTF8Encoding.UTF8.GetBytes(Utilities.NormaliseStri ngNfkd(passphrase)))
Return Utilities.BytesToHexString(Rfc2898_pbkdf2_hmacsha5 12.PBKDF2(UTF8Encoding.UTF8.GetBytes(mnemonicSente nce), salt))
End Function

#endregion

#region Private Methods

''' <summary>
''' Common initialisation code utilised by all the constructors. It gets all the bits and does a checksum etc. This is the main code to create a BIP39 object.
''' </summary>
Friend Sub pInit(ByVal passphrase As String, ByVal language As language)
_passphraseBytes = UTF8Encoding.UTF8.GetBytes(Utilities.NormaliseStri ngNfkd(passphrase))
_language = language
Dim allChecksumBytes() As Byte = Utilities.Sha256Digest(_entropyBytes,0,_entropyByt es.Length) 'sha256 the entropy bytes to get all the checksum bits

_entropyBytes = Utilities.SwapEndianBytes(_entropyBytes) 'seems I had to change the endianess of the bytes here to match the test vectors.....

'C++‎‎‎‎ TO VB CONVERTER WARNING: C++‎‎‎‎ to VB Converter cannot determine whether both operands of this division are integer types - if they are then you should use the VB integer division operator:
Dim numberOfChecksumBits As Integer = (_entropyBytes.Length * cBitsInByte) / cEntropyMultiple 'number of bits to take from the checksum bits, varies on entropy size as per spec
Dim entropyConcatChecksumBits As New BitArray((_entropyBytes.Length * cBitsInByte) + numberOfChecksumBits)

allChecksumBytes = Utilities.SwapEndianBytes(allChecksumBytes) 'yet another endianess change of some different bytes to match the test vectors.....

Dim index As Integer = 0

foreach(Boolean b in New BitArray(_entropyBytes))
If True Then
entropyConcatChecksumBits.Set(index, b)
index += 1
End If

' sooooo I'm doing below for future proofing....I know right now we are using up to 256 bits entropy in real world implementation and therefore max 8 bits (1 byte) of checksum....buuuut I figgure it's easy enough
' to accomodate more entropy by chaining more checksum bytes so maximum 256 * 32 = 8192 theoretical maximum entropy (plus CS).
Dim checksumBytesToUse As New List(Of Byte)()

Dim byteCount As Double = Math.Ceiling(CDbl(numberOfChecksumBits) / cBitsInByte)

For i As Integer = 0 To byteCount - 1
checksumBytesToUse.Add (allChecksumBytes(i))
Next i

Dim ba As New BitArray(checksumBytesToUse.ToArray())

'add checksum bits
For i As Integer = 0 To numberOfChecksumBits - 1
entropyConcatChecksumBits.Set(index,ba.Get(i))
index += 1
Next i

'C++‎‎‎‎ TO VB CONVERTER TODO TASK: The following line was determined to contain a copy constructor call - this should be verified and a copy constructor should be created:
'ORIGINAL LINE: _wordIndexList = pGetWordIndeces(entropyConcatChecksumBits);
_wordIndexList = pGetWordIndeces(New BitArray(entropyConcatChecksumBits))
_mnemonicSentence = pGetMnemonicSentence()

End Sub

''' <summary>
''' Uses the Wordlist Index to create a scentence ow words provided by the wordlist of this objects language attribute
''' </summary>
''' <returns>A scentence of words</returns>
Friend Function pGetMnemonicSentence() As String
'trap for words that were not in the word list when built. If custom words were used, we will not support the rebuild as we don't have the words
If _wordIndexList.Contains(-1) Then
Throw New Exception("the wordlist index contains -1 which means words were used in the mnemonic sentence that cannot be found in the wordlist and the index to sentence feature cannot be used. Perhaps a different language wordlist is needed?")
End If

Dim mSentence As String = cEmptyString
Dim wordlist As New Wordlists.Wordlist()

Select Case _language
Case Language_Renamed.English
wordlist = New Wordlists.English()

Case Language_Renamed.Japanese
wordlist = New Wordlists.Japanese()

Case Language_Renamed.Spanish
wordlist = New Wordlists.Spanish()

Case Language_Renamed.ChineseSimplified
wordlist = New Wordlists.ChineseSimplified()

Case Language_Renamed.ChineseTraditional
wordlist = New Wordlists.ChineseTraditional()

Case Language_Renamed.French
wordlist = New Wordlists.French()

Case Else
wordlist = New Wordlists.English()
End Select

For i As Integer = 0 To _wordIndexList.Count - 1
mSentence &= wordlist.GetWordAtIndex(_wordIndexList(i))
If i+1 < _wordIndexList.Count Then
mSentence &= " "
End If
Next i

Return mSentence
End Function

''' <summary>
''' Process entropy + CS into an index list of words to get from wordlist
''' </summary>
''' <returns>An index, each int is a line in the wiordlist for the language of choice</returns>
Friend Function pGetWordIndeces(ByVal entropyConcatChecksumBits As BitArray) As List(Of Integer)
Dim wordIndexList As New List(Of Integer)()

'yea....loop in a loop....what of it!!! Outer loop is segregating bits into 11 bit groups and the inner loop is processing the 11 bits before sending them to be encoded as an int.
For i As Integer = 0 To entropyConcatChecksumBits.Length - 1 Step cBitGroupSize
Dim toInt As New BitArray(cBitGroupSize)
Dim i2 As Integer = 0
Do While i2 < cBitGroupSize AndAlso i<entropyConcatChecksumBits.Length
toInt.Set(i2, entropyConcatChecksumBits.Get(i+i2))
i2 += 1
Loop

'C++‎‎‎‎ TO VB CONVERTER TODO TASK: The following line was determined to contain a copy constructor call - this should be verified and a copy constructor should be created:
'ORIGINAL LINE: wordIndexList.Add(pProcessBitsToInt(toInt));
wordIndexList.Add(pProcessBitsToInt(New BitArray(toInt))) 'adding encoded int to word index
Next i

Return wordIndexList
End Function

''' <summary>
''' Takes in the words of a mnemonic sentence and it rebuilds the word index, having the valid index allows us to hot swap between languages/word lists :)
''' </summary>
''' <param name="wordsInMnemonicSentence"> a string array containing each word in the mnemonic sentence</param>
''' <returns>The word index that can be used to build the mnemonic sentence</returns>
Friend Function pRebuildWordIndexes(ByVal wordsInMnemonicSentence() As String) As List(Of Integer)
Dim wordIndexList As New List(Of Integer)()
Dim langName As String = cEmptyString

Dim wordlist As New Wordlists.Wordlist()

Select Case _language
Case Language_Renamed.English
wordlist = New Wordlists.English()
langName = "English"

Case Language_Renamed.Japanese
wordlist = New Wordlists.Japanese()
langName = "Japanese"

Case Language_Renamed.Spanish
wordlist = New Wordlists.Spanish()
langName = "Spanish"

Case Language_Renamed.ChineseSimplified
wordlist = New Wordlists.ChineseSimplified()
langName = "Chinese Simplified"

Case Language_Renamed.ChineseTraditional
wordlist = New Wordlists.ChineseTraditional()
langName = "Chinese Traditional"

Case Language_Renamed.French
wordlist = New Wordlists.French()
langName = "French"

Case Else
wordlist = New Wordlists.English()
langName = "English"
End Select

foreach(String s in wordsInMnemonicSentence)
If True Then
Dim idx As Integer = -1

If Not wordlist.WordExists(s, out idx) Then
Throw New Exception("Word " & s & " is not in the wordlist for language " & langName & " cannot continue to rebuild entropy from wordlist")
End If

wordIndexList.Add (idx)
End If

Return wordIndexList
End Function

''' <summary>
''' Me encoding an integer between 0 and 2047 from 11 bits...
''' </summary>
''' <param name="bits">The bits to encode into an integer</param>
''' <returns>integer between 0 and 2047</returns>
Friend Function pProcessBitsToInt(ByVal bits As BitArray) As Integer

If bits.length <> cBitGroupSize Then
'to do throw not 11 bits exception
End If

Dim number As Integer = 0
Dim base2Divide As Integer = 1024 'it's all downhill from here...literally we halve this for each bit we move to.

'literally picture this loop as going from the most significant bit across to the least in the 11 bits, dividing by 2 for each bit as per binary/base 2
foreach(Boolean b in bits)
If True Then
If b Then
number = number + base2Divide
End If

...
End Namespace

mehdi-lotfi
پنج شنبه 08 مهر 1400, 20:14 عصر
اخه میخای چی کار؟ ؟

ARData
شنبه 10 مهر 1400, 09:54 صبح
حداقل از دوستان کسی در مورد فرمول PBKDF2 اطلاعاتی دارند (دنیای رمز ارزها) ؟