Hello everyone. I was getting creative and started to make a simulated "Encrypter" that simply takes some text and rolls it while solving for each letter. Movie style! I plan on incorporating this into any console app I make just for fun. I'm having an issue with a nested for loop and could use another eye. The issue is that the nested for loop is responsible for randomizing the remaining characters that haven't been printed to console yet and I'm off by one. With the for loop commented out it works fine. It is the rolling effect that is off by one char.
seudo:
Parse input text.
For each letter in text, generate a random char and insert into new string(strEncrypted).
For each VbCr, record the index into a List(Of Integer).
Print strEncrypted while inserting vbCrLf's to match input string character positions.
Starting from left to right, roll each char random(20, 40) times then print non-encrypted letter.
Here is the sub:
I had to use "intLooper" due to the way I handled the vbCrLf's. Originally I was using the outter loop variable "i" for the inner for loop but "i" would skip a number where there was a vbCrLf and so would the inner for loop. For each vbCrLf in strOut, strEncrypted will be reduced by 2 characters...
If there is a better way I'm all ears! Thank you.
seudo:
Parse input text.
For each letter in text, generate a random char and insert into new string(strEncrypted).
For each VbCr, record the index into a List(Of Integer).
Print strEncrypted while inserting vbCrLf's to match input string character positions.
Starting from left to right, roll each char random(20, 40) times then print non-encrypted letter.
Here is the sub:
VB.NET:
Private Sub Decryptor(ByVal strOut As String)
Dim strEncrypted As String = ""
Dim intVbCrLFIndex As List(Of Integer) = New List(Of Integer)
Dim intCurrentPositionLeft As Integer = Console.CursorLeft
Dim intDefaultPositionLeft As Integer = Console.CursorLeft
Dim intCurrentPositionTop As Integer = Console.CursorTop
Dim intDefaultPositionTop As Integer = Console.CursorTop
'Encrypt string and record the vbCrLf indexes.
For i As Integer = 0 To strOut.Length - 1
If strOut.ElementAt(i).ToString = vbCr Then
intVbCrLFIndex.Add(i)
Continue For
ElseIf strOut.ElementAt(i).ToString = vbLf Then
Continue For
End If
strEncrypted &= R()
Next
'Print Encrypted string while inserting vbCrLf.
For i As Integer = 0 To strEncrypted.Length - 1
If intVbCrLFIndex.Contains(i) Then
Console.Write(vbCrLf)
End If
Sleep(5)
Console.Write(strEncrypted(i))
Next
'Decode Encrypted string in-place while rolling.
Dim intLooper As Integer = 0
For i As Integer = 0 To (strEncrypted.Length - 1) + (intVbCrLFIndex.Count)
If intVbCrLFIndex.Contains(i) Then
intVbCrLFIndex.Remove(i)
intCurrentPositionTop = intDefaultPositionTop + 1
intCurrentPositionLeft = intDefaultPositionLeft
'intLooper += 1
Continue For
End If
Console.SetCursorPosition(intCurrentPositionLeft, intCurrentPositionTop)
Dim intTimes As Integer = m_r.Next(10, 20)
While intTimes >= 0
Console.SetCursorPosition(intCurrentPositionLeft, intCurrentPositionTop)
Console.Write(R())
Sleep(1)
intTimes -= 1
Console.SetCursorPosition(intCurrentPositionLeft, intCurrentPositionTop)
'For j As Integer = intLooper To (strEncrypted.Length - 1) '+ (intVbCrLFIndex.Count)
' If intVbCrLFIndex.Contains(j) Then
' Console.Write(vbCrLf)
' Continue For
' End If
' Console.Write(R())
'Next
End While
Console.SetCursorPosition(intCurrentPositionLeft, intCurrentPositionTop)
If strOut(i) = vbCr Or strOut(i) = vbLf Then
Console.Write(vbCrLf)
'intLooper += 1
Continue For
End If
Console.Write(strOut(i))
intCurrentPositionLeft += 1
intLooper += 1
Next
End Sub
Dim m_r As New Random()
Private ReadOnly Property R() As Char
Get
Dim t As Integer = m_r.[Next](10)
If t <= 2 Then
Return ChrW(CInt(AscW("0"c)) + m_r.[Next](10))
ElseIf t <= 4 Then
Return ChrW(CInt(AscW("a"c)) + m_r.[Next](27))
ElseIf t <= 6 Then
Return ChrW(CInt(AscW("A"c)) + m_r.[Next](27))
Else
Return ChrW(m_r.[Next](32, 255))
End If
End Get
End Property
I had to use "intLooper" due to the way I handled the vbCrLf's. Originally I was using the outter loop variable "i" for the inner for loop but "i" would skip a number where there was a vbCrLf and so would the inner for loop. For each vbCrLf in strOut, strEncrypted will be reduced by 2 characters...
If there is a better way I'm all ears! Thank you.