473,396 Members | 2,140 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

First attempt on a for loop

Hello all,

I've been working on a paper and pen crypto application.
The time has come for the Keyword cipher, but I feel that my loops here are
clumsy and overly complex.
Can someone give som hints, tips or even solutions to how to optimise these
loops.
I'm still learning so I will work on getting better at choosing good
variable names/descriptors and commenting my code.

Here are the loops in question:

[...]
Function KeywordCipher(ByVal strPlainText As String, ByVal strSecretKey As
String) As String
Dim m As Integer
Dim n As Integer
Dim strCipherChar As Char
Dim p As Integer
Dim K As Integer
Dim C As Integer
Dim i As Integer
Dim j As Integer
Dim counter As Integer = 1
Dim strKeywordChar As Char
Dim strCipherText As String
For i = 1 To strPlainText.Length
strCipherChar = Mid(strPlainText, i, 1)

For n = 0 To chrKeySpace.GetUpperBound(0)
If strCipherChar = chrKeySpace(n) Then
C = CInt(n + 1)
Exit For
End If

Next
For j = counter To strSecretKey.Length
strKeywordChar = Mid(strSecretKey, j, 1)

For m = 0 To chrKeySpace.GetUpperBound(0)
If strKeywordChar = chrKeySpace(m) Then
K = CInt(m + 1)
Exit For
End If

Next

Exit For

Next

p = (C + K) Mod 29
strCipherText = strCipherText + chrKeySpace(CInt((p - 1) Mod29))

If counter < strSecretKey.Length Then
counter += 1
Else
counter = 1
End If
Next

Return (strCipherText)

End Function
[...]

Thank you so much for your time

Nazdarovje,

Remi
Nov 20 '05 #1
4 1300
A StringBuilder object will speed things way up.

Change this:
Dim strCipherText As String

To this:
Dim strCipherText As New StringBuilder(64)
Hello all,

I've been working on a paper and pen crypto application.
The time has come for the Keyword cipher, but I feel that my loops here are
clumsy and overly complex.
Can someone give som hints, tips or even solutions to how to optimise these
loops.
I'm still learning so I will work on getting better at choosing good
variable names/descriptors and commenting my code.

Here are the loops in question:

[...]
Function KeywordCipher(ByVal strPlainText As String, ByVal strSecretKey As
String) As String
Dim m As Integer
Dim n As Integer
Dim strCipherChar As Char
Dim p As Integer
Dim K As Integer
Dim C As Integer
Dim i As Integer
Dim j As Integer
Dim counter As Integer = 1
Dim strKeywordChar As Char
Dim strCipherText As String
For i = 1 To strPlainText.Length
strCipherChar = Mid(strPlainText, i, 1)

For n = 0 To chrKeySpace.GetUpperBound(0)
If strCipherChar = chrKeySpace(n) Then
C = CInt(n + 1)
Exit For
End If

Next
For j = counter To strSecretKey.Length
strKeywordChar = Mid(strSecretKey, j, 1)

For m = 0 To chrKeySpace.GetUpperBound(0)
If strKeywordChar = chrKeySpace(m) Then
K = CInt(m + 1)
Exit For
End If

Next

Exit For

Next

p = (C + K) Mod 29
strCipherText = strCipherText + chrKeySpace(CInt((p - 1) Mod29))

If counter < strSecretKey.Length Then
counter += 1
Else
counter = 1
End If
Next

Return (strCipherText)

End Function
[...]

Thank you so much for your time

Nazdarovje,

Remi

Nov 20 '05 #2
A StringBuilder object will speed things way up.

Change this:
Dim strCipherText As String

To this:
Dim strCipherText As New StringBuilder(64)
Hello all,

I've been working on a paper and pen crypto application.
The time has come for the Keyword cipher, but I feel that my loops here are
clumsy and overly complex.
Can someone give som hints, tips or even solutions to how to optimise these
loops.
I'm still learning so I will work on getting better at choosing good
variable names/descriptors and commenting my code.

Here are the loops in question:

[...]
Function KeywordCipher(ByVal strPlainText As String, ByVal strSecretKey As
String) As String
Dim m As Integer
Dim n As Integer
Dim strCipherChar As Char
Dim p As Integer
Dim K As Integer
Dim C As Integer
Dim i As Integer
Dim j As Integer
Dim counter As Integer = 1
Dim strKeywordChar As Char
Dim strCipherText As String
For i = 1 To strPlainText.Length
strCipherChar = Mid(strPlainText, i, 1)

For n = 0 To chrKeySpace.GetUpperBound(0)
If strCipherChar = chrKeySpace(n) Then
C = CInt(n + 1)
Exit For
End If

Next
For j = counter To strSecretKey.Length
strKeywordChar = Mid(strSecretKey, j, 1)

For m = 0 To chrKeySpace.GetUpperBound(0)
If strKeywordChar = chrKeySpace(m) Then
K = CInt(m + 1)
Exit For
End If

Next

Exit For

Next

p = (C + K) Mod 29
strCipherText = strCipherText + chrKeySpace(CInt((p - 1) Mod29))

If counter < strSecretKey.Length Then
counter += 1
Else
counter = 1
End If
Next

Return (strCipherText)

End Function
[...]

Thank you so much for your time

Nazdarovje,

Remi

Nov 20 '05 #3
Cor
Hi Remi,

Making your own encoding classes is always clumsy and complex.
The biggest deal is that to be able to encode but than to decode.

However there are a lot of encoding classes on msdn.
Have a look for that, and start searching using the keyword "rijndael", that
is one of the classes, but it is a nice startpoint for the search

I hope this helps?

Cor
Nov 20 '05 #4
Cor
Hi Remi,

Making your own encoding classes is always clumsy and complex.
The biggest deal is that to be able to encode but than to decode.

However there are a lot of encoding classes on msdn.
Have a look for that, and start searching using the keyword "rijndael", that
is one of the classes, but it is a nice startpoint for the search

I hope this helps?

Cor
Nov 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: aaj | last post by:
SQL SERVER 2000 Hi all This is my first attempt at writing a stored procedure. I have managed to get it working but its unlikely to be the best way of handling the problem. While writing it I...
4
by: Theron NightStar | last post by:
I am trying to teach myself c++. This is the first program I have ever written that might have an practical use to me. I rather proud of it since like implied - I have no real knowledge of c++. ...
32
by: James Curran | last post by:
I'd like to make the following proposal for a new feature for the C# language. I have no connection with the C# team at Microsoft. I'm posting it here to gather input to refine it, in an "open...
7
by: Lae. | last post by:
I can't figure this one out. n00b question no doubt, this is my first ever JS attempt. Here's the snippet, and here's the full deal http://www.ualberta.ca/~koryb/first.js...
3
by: Joe Befumo | last post by:
This is my first attempt at multi-thread programming, and I'm encountering a program-logic problem that I can't quite put my finger on. The program is pretty simple. I'm trying to validate a...
4
by: Larry | last post by:
OK, I'm just learning MySQL, or at least trying to. I have a table with data as follows USER FIELDID VALUE 1 1 Bob 1 2 Smith 2 1 John 2 ...
14
by: Billy | last post by:
Complete n00b here, so please bear with me. I have written the following code and would like to know if it could be optimised. In particular, I'm reading the user input for the operand into...
26
by: tesh.uk | last post by:
Hi Gurus, I have written the following code with the help of Ivor Horton's Beginning C : // Structures, Arrays of Structures. #include "stdafx.h" #include "stdio.h" #define MY_ARRAY 15
8
Motoma
by: Motoma | last post by:
Good evening everyone. I am starting to re-explore C++, and I wanted to build a singleton class. Unfortunately, when I set things up as I do in PHP, it doesn't work out for me. I hope that the...
12
by: Phillip B Oldham | last post by:
I'm keen on learning python, with a heavy lean on doing things the "pythonic" way, so threw the following script together in a few hours as a first-attempt in programming python. I'd like the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.