473,408 Members | 1,840 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,408 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 1302
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.