|
Hello, I still use VB.Net once. I have a college project assignment to make cryptography. but I have experienced an error in the program. Please help me.
***program code that displays an error
'Pembentukan Kunci
Public Sub GetIDEAKey(ByVal pstrKey As String)
Dim strKunci As String = ""
Dim nRound = 1, nKey As Integer = 0
'Dim K(9, 6) As String
'Dim KD(9, 6) As String
Dim i As Integer
Dim j As Integer
'Biner kunci - 128 bit
strKunci = StringtoBinary(pstrKey)
'---- Pembentukan Key Enkripsi ----
For i = 1 To 7
'Kelompokkan ke 8 buah subkey (pjg 16 bit)
For j = 1 To 128 Step 16
'Tambah nKey
nKey = nKey + 1
If nKey = 7 Then
'Tambah Round
nRound = nRound + 1
'Reset nilai nKey
nKey = 1
End If
If (nRound & nKey) < 95 Then
'Kunci per round
KE(nRound - 1, nKey - 1) = strKunci.Substring(j - 1, j - 1 +16)***
End If
Next
'Untuk putaran terakhir tidak usah rotasi kiri
If i <> 7 Then
strKunci = FRotateLeftShift(strKunci, 25)
End If
Next
|
|
|
|
|
Assuming the line marked with "***" is the one that throws the error:
strKunci.Substring(j - 1, j - 1 +16)
Either:
(j - 1) is greater than or equal to the length of strKunci ; or(j - 1) + (j - 1 + 16) is greater than or equal to the length of strKunci
NB: The second parameter to Substring is the length of the substring you want, not the index of the end of the substring. I suspect you probably meant:
strKunci.Substring(j - 1, 16) This will still throw an exception if (j - 1) + 16 is greater than or equal to the length of the string.
String.Substring Method (System) | Microsoft Docs[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi,
I use for some years this code that worked.
Public Class HotKey
' Windows API functions and constants
Private Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As Integer
Private Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer) As Integer
Private Declare Function GlobalAddAtom Lib "kernel32" Alias "GlobalAddAtomA" (ByVal lpString As String) As Short
Private Declare Function GlobalDeleteAtom Lib "kernel32" (ByVal nAtom As Short) As Short
<pre>
Public Const MOD_ALT As Integer = 1
Public Const MOD_CONTROL As Integer = 2
Public Const MOD_SHIFT As Integer = 4
Public Const MOD_WIN As Integer = 8
Private f As Form
Public Enum EKeyModifier
Neutral
Alt
Control
Control_Alt
Shift
Shift_Alt
Shift_Control
Shift_Alt_Control
Windows
Win_Alt
Win_Control
Win_Control_Alt
Win_Shift
Win_Shift_Alt
Win_Shift_Control
Win_Shift_Alt_Control
Norepeat = &H4000
End Enum
Private _Modifier As EKeyModifier
Private _Key As System.Windows.Forms.Keys ' the id for the hotkey
Dim hotkeyID As Short
Sub SDummy()
MsgBox("Dummy routine ShortKey")
End Sub
Public Property Key() As System.Windows.Forms.Keys
Get
Return _Key
End Get
Set(ByVal value As System.Windows.Forms.Keys)
UnregisterGlobalHotKey()
RegisterGlobalHotKey(value, _Modifier, f)
_Key = value
End Set
End Property
Public Property Modifier() As EKeyModifier
Get
Return _Modifier
End Get
Set(ByVal value As EKeyModifier)
UnregisterGlobalHotKey()
RegisterGlobalHotKey(_Key, value, f)
_Modifier = value
End Set
End Property
' register a global hot key
Function RegisterGlobalHotKey(ByVal hotkey As EKeyModifier, ByVal modifiers As Integer, ByVal frm As Form) As Boolean
Try
f = frm
' use the GlobalAddAtom API to get a unique ID (as suggested by MSDN docs)
'adapted code(obsolete)
'Dim atomName As String = AppDomain.GetCurrentThreadId.ToString("X8") & Me.Name
Dim th As New Threading.Thread(AddressOf SDummy)
Dim value As Integer
value = th.ManagedThreadId
'end adapted
Dim atomName As String = CStr(value) & "_HotKey"
hotkeyID = GlobalAddAtom(atomName)
If hotkeyID = 0 Then
Throw New Exception("Unable to generate unique hotkey ID. Error code: " &
System.Runtime.InteropServices.Marshal.GetLastWin32Error().ToString)
End If
_Key = hotkey
_Modifier = modifiers
' register the hotkey, throw if any error
If RegisterHotKey(frm.Handle, hotkeyID, modifiers, hotkey) = 0 Then
Dim LastError As String = System.Runtime.InteropServices.Marshal.GetLastWin32Error.ToString
Throw New Exception("Unable to register hotkey. Error code: " & LastError)
End If
Return True
Catch ex As Exception
' clean up if hotkey registration failed
UnregisterGlobalHotKey()
Return False
End Try
End Function
' unregister a global hotkey
Sub UnregisterGlobalHotKey()
If hotkeyID <> 0 Then
Try
UnregisterHotKey(f.Handle, hotkeyID)
Catch ex As Exception
End Try
' clean up the atom list
GlobalDeleteAtom(hotkeyID)
hotkeyID = 0
End If
End Sub
Protected Overrides Sub Finalize()
UnregisterGlobalHotKey()
MyBase.Finalize()
End Sub
End Class</pre>
Now I want to use it again but my hotkey isn't working
I define the hotkey in the startup and check it in wndProc routine
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Hotkey = New Classes.HotKey
If Not Hotkey.RegisterGlobalHotKey(Keys.F5, Classes.HotKey.EKeyModifier.Win_Alt, Me) Then
MsgBox("Close key not registered")
End If
End Sub
<pre>
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
' let the base class process the message
MyBase.WndProc(m)
' if this is a WM_HOTKEY message, notify the parent object
Const WM_HOTKEY As Integer = &H312
If m.Msg = WM_HOTKEY Then
' do whatever you wish to do when the hotkey is pressed
Dim key As Keys = ((m.LParam.ToInt64 >> 16) And &HFFFF) 'the charKey
Dim modifier As Classes.HotKey.eKeyModifier = (m.LParam.ToInt64 And &HFFFF) 'Shift Alt Control and/or Windows key
Select Case key.ToString
Case "F5"
EndToolStripMenuItem.PerformClick()
End Select
End If
End Sub</pre>
The WndProc function starts on other messages but not on the hotkey
Where am I wrong?
Thanks
Jan
|
|
|
|
|
Really nobody that can help?
|
|
|
|
|
I'm connecting to an EPP server via TCPclient and it's protected by SSL certificate authentication. The company that provides the EPP server said I needed to get a certificate (not self-signed), upload the CRT file into my user account, and then combine the CRT and KEY files into one (I choose PFX) that I will need to pass with each message in order to invoke an authorization / certificate combination. I've upload the CRT and created the PFX files fine.
I've tested this several ways (SSLstream, NetworkStream, StreamWriter/StreamReader) and none of them have been able to produce me with a readable response. At the same time, neither method produces any exceptions. From my testing it appears that I connect to the server okay, convert my string and write okay, but then either get a blank response or a jibberish response that looks like it might be encrypted? Below are two ways I've tried to do this (SSLstream & NetworkStream). You can see my code, plus a screenshot of the response.
SSLstream
Dim collection = New X509Certificate2Collection()
collection.Import("C:\\Users\\Drew\\Desktop\\testcert\\FIcert.pfx", "password", X509KeyStorageFlags.DefaultKeySet)
Dim store = New X509Store(StoreName.My)
store.Open(OpenFlags.ReadWrite)
Try
For Each certificate As X509Certificate2 In collection
store.Add(certificate)
Next
Finally
store.Close()
End Try
Dim hello As XElement = <epp xmlns="urn:ietf:params:xml:ns:epp-1.0">
<hello/>
</epp>
Try
Dim client As New TcpClient(DRShost, 700)
Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(hello.ToString)
Dim callback As New RemoteCertificateValidationCallback(AddressOf CertificateValidationCallback)
Dim sslStream As New Security.SslStream(client.GetStream(), False, callback)
sslStream.AuthenticateAsClient(DRShost, collection, SslProtocols.[Default], False)
If sslStream.IsAuthenticated Then
MsgBox("IsAuthenticated: " & sslStream.IsAuthenticated)
MsgBox("IsMutuallyAuthenticated: " & sslStream.IsMutuallyAuthenticated)
MsgBox("IsEncrypted: " & sslStream.IsEncrypted)
MsgBox("IsSigned: " & sslStream.IsSigned)
MsgBox("IsServer: " & sslStream.IsServer)
End If
sslStream.Write(data, 0, data.Length)
MsgBox("Sent: " & hello.ToString)
data = New [Byte](256) {}
Dim bytes As Integer = sslStream.Read(data, 0, data.Length)
Dim responseData As String = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
MsgBox("Received: " & responseData)
Catch ex As AuthenticationException
MsgBox(ex.Message)
Catch ex As SocketException
MsgBox(ex.Message)
Catch ex As IOException
MsgBox(ex.Message)
Finally
If sslStream IsNot Nothing Then
sslStream.Close()
End If
End Try
Response:
Screenshot Here
NetworkStream
Dim collection = New X509Certificate2Collection()
collection.Import("C:\\Users\\Drew\\Desktop\\testcert\\FIcert.pfx", "password", X509KeyStorageFlags.DefaultKeySet)
Dim store = New X509Store(StoreName.My)
store.Open(OpenFlags.ReadWrite)
Try
For Each certificate As X509Certificate2 In collection
store.Add(certificate)
Next
Finally
store.Close()
End Try
Dim hello As XElement = <epp xmlns="urn:ietf:params:xml:ns:epp-1.0">
<hello/>
</epp>
Dim client As TcpClient = New TcpClient(DRShost, 700)
Dim tcpNetStream As NetworkStream = client.GetStream()
Dim sslStream As New Security.SslStream(tcpNetStream, False, New RemoteCertificateValidationCallback(AddressOf CertificateValidationCallback), Nothing)
sslStream.AuthenticateAsClient(DRShost, collection, SslProtocols.[Default], False)
Dim encodedBytes As Byte() = ASCIIEncoding.ASCII.GetBytes(hello.ToString)
tcpNetStream.Write(encodedBytes, 0, encodedBytes.Length)
Dim bytesToReceive As Byte() = New Byte(client.ReceiveBufferSize - 1) {}
Dim bytesRead As Integer = tcpNetStream.Read(bytesToReceive, 0, client.ReceiveBufferSize)
MsgBox("Received : " & Encoding.ASCII.GetString(bytesToReceive, 0, bytesRead))
client.Close()
Response:
Screenshot Here
I thought maybe the response from the second set of code was coming back encrypted, so I changed this line:
Dim bytesRead As Integer = tcpNetStream.Read(bytesToReceive, 0, client.ReceiveBufferSize)
To read from the sslStream instead. However, this just leads to once again receiving a blank response:
Dim bytesRead As Integer = sslStream.Read(bytesToReceive, 0, client.ReceiveBufferSize)
Any help on this would be greatly appreciated. I've scoured the web and tried everything I could think of, but to no avail. I do notice that when I view the string I'm sending it does not contain "". However, I believe that is added automatically by XElement?
|
|
|
|
|
Sorry, I thought my problem was a bad path problem but it turns out to be something else.
Although the information below is correct, the code recognizes the returned
path as a valid path.
Can someone tell me why this code does not return the complete path?
It seems like this 'old code' worked at one time.
Thank You
Const CSIDL_MYMUSIC as integer = 13 ' My Music
Const CSIDL_PERSONAL As Integer = 5 ' My Documents
<DllImport("Shell32", CharSet:=CharSet.Auto, SetLastError:=True)> _
Public Shared Function SHGetFolderPath(ByVal hWnd As IntPtr, ByVal csidl As Integer, _
ByVal handle As IntPtr, ByVal dwFlags As Integer, _
<MarshalAs(UnmanagedType.LPTStr)> ByVal lpBuffer As System.Text.StringBuilder) As Integer
End Function
For CSIDL_MYMUSIC the return value is "C:\users\myname.Home\Music" instead of "...\My Music"
For CSIDL_PERSONAL the return value is "C:\users\myname.Home\Documents" instead of "...\My Documents"
sub MyFunction()
Dim sb As New StringBuilder(200)
Dim sPath as string
SHGetFolderPath(IntPtr.Zero, CSIDL_MYMUSIC, IntPtr.Zero, 0, sb)
sPath = sb.ToString()
end sub
-- modified 28-Sep-18 18:04pm.
|
|
|
|
|
The names "My Documents", "My Music" etc. are shorthand links used by Explorer. If you open a command window and enter the command dir C:\ you will notice that they do not exist. And when you use the SHGetFolderPath function, it will always return the real full path for the relevant item.
|
|
|
|
|
Thank you for the information. Always good to learn something new.
|
|
|
|
|
|
Alright!!, something else new (to me). Thanks for the info.
|
|
|
|
|
Private Sub Combo1_Click()
Dim drs As New ADODB.Recordset
drs.Open "select * from room where roomno=" & CInt(Combo1.Text), con, 3, 2
If (Not drs.EOF Or drs.BOF) Then
Do Until drs.EOF
Me.Text2.Text = drs!price
drs.MoveNext
Loop
End If
End Sub
|
|
|
|
|
And? What is the question?
|
|
|
|
|
"VB6 lingua mortua est."
* You do not select *, just the rows columns you need
* You do not concatenate strings with unchecked user-input
* You do not load an entire recordset and loop, just to get a single attribute
* You do not code in VB6, not even if a Klingon demands it
* You do not have any question with your code-dump, and as such, should not expect much answers
Aw, and if you are going to be using this forum as your PA, at least be so kind to fill in your name.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
Hello !
I'm writing a program in vb.net 2017 .
I need to have some forms in a specific size , for example 1180 x 700. The program run very well on users that have new monitors.
But the problem is that some users of this program , have old monitors that support up to 1024 x 768.
Do someone have an opinion how can I resolve this situation ?
Thank you !
|
|
|
|
|
I am assuming you are working on Windows Forms. If not, there is nothing that follows which will help.
Is there any specific reason why the form needs to be of that size? You might want to see TableLayoutPanel control to do control layout. In the panel, you can set column and row sizes to auto, fixed or percentage as per needs. Each cell can have only one control in the panel. The controls must have Dock set to Fill . The panel itself will also have Dock set to Fill (covering entire form).
This will ensure controls resize as the screen does.
"It is easy to decipher extraterrestrial signals after deciphering Javascript and VB6 themselves.", ISanti[ ^]
|
|
|
|
|
You should never do that. It is the user's choice what size the window should be. You just need to ensure that your form is clearly viewable on any screen size.
|
|
|
|
|
But this is my question , how can I do that my form will be clearly viewable on any screen size !!!
|
|
|
|
|
Take a look at the first reply from iw@. Build for the SMALLEST screen size and have the main control (usually a grid) able to resize (dockable, size="auto"). When viewed on a larger screen the main control then expands to use the available space leaving your peripheral controls (header or footer controls) the original size.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Can anyone tell me if c++ is worth learning or is it outdated?
|
|
|
|
|
Definitely not in the Visual Basic forum. Please don't crosspost it just duplicates effort and gets you a bad reputation.
|
|
|
|
|
Hello
I am using VBA from within the Rockwell (SCADA,PLC,...) Factory Talk View SCADA system.
I need to run a external (to the Factory talk view app) internet explorer process with the url (I use directly the IP) of a Bosch IPCamera. The windows security pop up for authentication comes to input user and password.
This is working fine. To do that from the VBA code i do this
Shell("C:\Program Files\Internet Explorer\iexplore.exe" & " http://192.168.50.11/",vbNormalFocus)
But I need to access directly without popup passing the credential from the VBA.
How can thi be done?
Thank you and regards
|
|
|
|
|
I am looking for a copy of vb.net
|
|
|
|
|
|
I tried Google. I couldn't find a download for Visual Basic.net
|
|
|
|
|
You need to look for Visual Studio.
|
|
|
|
|