Computer Hope
Microsoft => Microsoft DOS => Topic started by: zask on March 22, 2016, 08:25:30 PM
-
How to I make a variable that displays a single random letter a-z.
for example
I want to do something like this
echo %RANDOM:~-1%
but instead display something like this
echo a
echo q
echo w
echo y
echo h
by doing this
echo %RandomLetter%
echo %RandomLetter%
echo %RandomLetter%
echo %RandomLetter%
echo %RandomLetter%
-
@echo off
REM Setup random source
REM Put this before you you start using
REM random letters
setlocal enabledelayedexpansion
set alphabet=abcdefghijklmnopqrstuvwxyz
set randsource=
REM Append alphabet as many times as you like
set copies=100
for /l %%N in (1,1,%copies%) do set randsource=!randsource!%alphabet%
set /a maxnum=26*%copies%
REM Get a random character
SET /A rnum=%RANDOM%*%maxnum%/32768+1
set randletter=!randsource:~%rnum%,1!
echo %randletter%
-
Or use a VBScript one-liner:
@echo off
set lowercase=96
set uppercase=64
set casetype=%uppercase%
echo randomize:wscript.echo chr(Int((26)*Rnd+1)+%casetype%) > randletter.vbs
for /f %%A in ('cscript //nologo randletter.vbs') do set randletter=%%A
echo %randletter%
-
Here's a way to do it. Much fun.
@echo off
:: Echos a random letter
set randletter=call set /a r=%%random%% %%%% 26 ^& setlocal enabledelayedexpansion ^& set "alphabet=abcdefghijklmnopqrstuvwxyz" ^& call echo %%alphabet:~!r!,1%%^& endlocal
%randletter%
%randletter%
%randletter%
%randletter%
%randletter%
%randletter%
%randletter%
%randletter%
%randletter%
%randletter%
%randletter%
-
@echo off
REM Setup random source
REM Put this before you you start using
REM random letters
setlocal enabledelayedexpansion
set alphabet=abcdefghijklmnopqrstuvwxyz
set randsource=
REM Append alphabet as many times as you like
set copies=100
for /l %%N in (1,1,%copies%) do set randsource=!randsource!%alphabet%
set /a maxnum=26*%copies%
REM Get a random character
SET /A rnum=%RANDOM%*%maxnum%/32768+1
set randletter=!randsource:~%rnum%,1!
echo %randletter%
Okay this is helpful but is there a way a can change this to make a random 26 character alphabet variable?
-
Randomized alphabet string 26 chars long, (no repeats)
@echo off
REM 65 for upper case, 97 for lower case
set CaseType=97
> Randalphabet.vbs echo randomize timer
>> Randalphabet.vbs echo RandAlphabet=""
>> Randalphabet.vbs echo Do
>> Randalphabet.vbs echo randchar = Chr(Int(26*Rnd+%CaseType%))
>> Randalphabet.vbs echo If Instr(RandAlphabet, randchar) = 0 Then RandAlphabet = RandAlphabet ^& randchar
>> Randalphabet.vbs echo if len(RandAlphabet) = 26 Then exit Do
>> Randalphabet.vbs echo Loop
>> Randalphabet.vbs echo wscript.echo RandAlphabet
REM -----------------------------------------------
REM Here you use it
for /f "delims=" %%A in ('cscript //nologo Randalphabet.vbs') do set Randalphabet=%%A
echo %Randalphabet%
Examples
cjdmtelkufvywozsnbghxairqp
pojqhbyftgweviadlxzrsncumk
ayoublqmvzdhfkxpiwcngsterj
-
Randomized alphabet string 26 chars long, (no repeats)
@echo off
REM 65 for upper case, 97 for lower case
set CaseType=97
> Randalphabet.vbs echo randomize timer
>> Randalphabet.vbs echo RandAlphabet=""
>> Randalphabet.vbs echo Do
>> Randalphabet.vbs echo randchar = Chr(Int(26*Rnd+%CaseType%))
>> Randalphabet.vbs echo If Instr(RandAlphabet, randchar) = 0 Then RandAlphabet = RandAlphabet ^& randchar
>> Randalphabet.vbs echo if len(RandAlphabet) = 26 Then exit Do
>> Randalphabet.vbs echo Loop
>> Randalphabet.vbs echo wscript.echo RandAlphabet
REM -----------------------------------------------
REM Here you use it
for /f "delims=" %%A in ('cscript //nologo Randalphabet.vbs') do set Randalphabet=%%A
echo %Randalphabet%
Examples
cjdmtelkufvywozsnbghxairqp
pojqhbyftgweviadlxzrsncumk
ayoublqmvzdhfkxpiwcngsterj
thank you this is exactly what i need :)
-
Have you noticed what happens if you delete or comment out the randomize timer line in the vbs?
-
Have you noticed what happens if you delete or comment out the randomize timer line in the vbs?
the only thing that i realized is that it goes to an error on second run due to the ">" sign not being a ">>", what exactly is happening?
-
the only thing that i realized is that it goes to an error on second run due to the ">" sign not being a ">>", what exactly is happening?
Comment out the VBS line being written like this (that's a single quote just before the randomize keyword)
(etc)
> Randalphabet.vbs echo 'randomize timer
(etc)
-
Comment out the VBS line being written like this (that's a single quote just before the randomize keyword)
(etc)
> Randalphabet.vbs echo 'randomize timer
(etc)
Oh okay i see now, thats a really cool trick :). Now i came up with this using some of the code that was provided by you guys and some old code i had laying around. this code is supposed encrypt a text file full of batch commands, then make a decrypter to take the encrypted text file, decrypt each command, and execute it within the same batch file. Now i am having a problem, each line in the text file is supposed placed in a separate variable to encrypt/decrpyt them one at a time, but if i wanted to use variable in the encrypted text file (For example~ %Variable%, %%V, !Variable!), they wont work correctly. It would appear "%Variable%" rather than what the substitution of what the variable should be set to.
for example if i typed something like this in a text file and encrypted it;
color 17
echo hello
pause
everything would work completely fine but if i tried to type something like this;
color 17
set a=hello
echo %a%
pause
When the decrypter.bat file would decrypt then execute the commands it would display as this
"%a%"
instead of
"hello"
Is there a way to fix this? here is my code so far.
@ECHO Off
echo encrypt batch file
echo.
Setlocal EnableDelayedExpansion
Set _RNDLength=8
Set _Alphanumeric=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
Set _Str=%_Alphanumeric%987654321
:_LenLoop
IF NOT "%_Str:~18%"=="" SET _Str=%_Str:~9%& SET /A _Len+=9& GOTO :_LenLoop
SET _tmp=%_Str:~9,1%
SET /A _Len=_Len+_tmp
Set _count=0
SET _RndAlphaNum=
:_loop
Set /a _count+=1
SET _RND=%Random%
Set /A _RND=_RND%%%_Len%
SET _RndAlphaNum=!_RndAlphaNum!!_Alphanumeric:~%_RND%,1!
If !_count! lss %_RNDLength% goto _loop
REM 65 for upper case, 97 for lower case
set CaseType=97
> Randalphabet.vbs echo randomize timer
>> Randalphabet.vbs echo RandAlphabet=""
>> Randalphabet.vbs echo Do
>> Randalphabet.vbs echo randchar = Chr(Int(26*Rnd+%CaseType%))
>> Randalphabet.vbs echo If Instr(RandAlphabet, randchar) = 0 Then RandAlphabet = RandAlphabet ^& randchar
>> Randalphabet.vbs echo if len(RandAlphabet) = 26 Then exit Do
>> Randalphabet.vbs echo Loop
>> Randalphabet.vbs echo wscript.echo RandAlphabet
for /f "delims=" %%A in ('cscript //nologo Randalphabet.vbs') do set Randalphabet=%%A
del Randalphabet.vbs
set /A CREATEKEY=%random% %%200 +2
set /P "FILEA= Enter the name of the text file that you wish to encrypt (Example; "Somefile") : "
set /P "FILEB= Enter the new name for the text file that you wish to encrypt (Example; "NewFile") : "
Setlocal EnableExtensions
for /f "delims=" %%A in (%FILEA%.txt) do (
Set /a C+=1
Set X[!C!]=%%A
set CHECKPASSWORD=%%A
set CHECKKEY=%%B
set CHAR=0123456789%Randalphabet%
for /l %%C in (10 1 36) do (
for /f %%D in ("!CHAR:~%%C,1!") do (
set /a MATH=%%C*%CREATEKEY%
for /f %%E in ("!MATH!") do (
set "CHECKPASSWORD=!CHECKPASSWORD:%%D=-%%E!"
)
)
)
echo !CHECKPASSWORD! >> %FILEB%.!_RndAlphaNum!
)
Set X
echo @ECHO Off >> Decrypt.bat
echo. >> Decrypt.bat
echo Setlocal EnableExtensions EnableDelayedExpansion >> Decrypt.bat
echo for /f "delims=" %%%%A in (%FILEB%.!_RndAlphaNum!) do ( >> Decrypt.bat
Setlocal DisableExtensions DisableDelayedExpansion
echo Set /a C+=1 >> Decrypt.bat
echo Set X[!C!]=%%%%A >> Decrypt.bat
echo set CHECKPASSWORD=%%%%A >> Decrypt.bat
echo set CHECKKEY=%%%%B >> Decrypt.bat
echo set CHAR=0123456789%Randalphabet% >> Decrypt.bat
echo. >> Decrypt.bat
echo for /l %%%%C in (10 1 36) do ( >> Decrypt.bat
echo for /f %%%%D in ("!CHAR:~%%%%C,1!") do ( >> Decrypt.bat
echo set /a MATH=%%%%C*%CREATEKEY% >> Decrypt.bat
echo for /f %%%%E in ("!MATH!") do ( >> Decrypt.bat
echo. >> Decrypt.bat
echo set "CHECKPASSWORD=!CHECKPASSWORD:%%%%E=%%%%D!" >> Decrypt.bat
echo ) >> Decrypt.bat
echo ) >> Decrypt.bat
echo ) >> Decrypt.bat
echo for /f %%%%F in ("!CHECKPASSWORD!") do ( >> Decrypt.bat
echo set "CHECKPASSWORD=!CHECKPASSWORD:-=!" >> Decrypt.bat
echo !CHECKPASSWORD! >> Decrypt.bat
echo ) >> Decrypt.bat
echo ) >> Decrypt.bat
echo Set X >> Decrypt.bat
I have a working example of some encrypted commands that do work, they shoud help give example of what
im trying to do, but you have to download it here.
http://www.filedropper.com/batchencrypter_2
-
It would be nice if Zask could tell us why he wants to do this.
If it is just for the fun of doing a task in DOS the hard way, that is OK.
But if Zask needs a quick and effective solution to a piratical problem, he might say so.
Here is why I ask. He is using a Random generator to make a sequence that is not random. He wants a re-ordered sequence of the Alphabet. It is a table where are letters are present, but not in an obvious order. Such tables do not need the random generator. Such are used in some types of encryption methods. And it can be done in batch, but if anybody finds the code your method is no longer a secret.
-
It would be nice if Zask could tell us why he wants to do this.
If it is just for the fun of doing a task in DOS the hard way, that is OK.
But if Zask needs a quick and effective solution to a piratical problem, he might say so.
Here is why I ask. He is using a Random generator to make a sequence that is not random. He wants a re-ordered sequence of the Alphabet. It is a table where are letters are present, but not in an obvious order. Such tables do not need the random generator. Such are used in some types of encryption methods. And it can be done in batch, but if anybody finds the code your method is no longer a secret.
I understand that, and yes you are correct, i do prefer doing things in dos the hard way, that's just the type of person i am. I don't really care if other people find my code because i just program batch for a hobbie and i do this to learn, when i am not programming in dos i am usually learning other programming language at school, and that's where i usually ask questions on my work. But when I program batch the internet is where i come to learn. There really isn't much reason to why i needed the random alphanumeric code, but one reason is because when "a" is assigned to "1", and "b" to "2", "c" to "3", and so forth. it would be to obvious.
I just wanted it assigned to a random alphabet so that "a" could be "5", or "z" could be "12". And also because i wanted the sequence to change each time. It doesn't necessarily make the code anymore harder
to decipher but it does give some people with less knowledge less more time to reverse the code (Not much though). But do acknowledge that a lot of the help i get from you guys is very much appreciated.
So do you know a way that i can make it possible to get the variables to work? I cant currently get my head wrapped around this. They display what appears textualized, but they don't actually work.
-
Simple number sequnces.
https://www.mathsisfun.com/numberpatterns.html
This can be one way of making a unusual sequence. But you can also do it by hand and get good results. The idea is a foundation on which to build. You can add complexity to it later.
Partial example: (only 8 items to show the idea.)
1 2 3 4 5 6 7 8 < order
A B C D E F G H
5 2 7 4 1 6 3 8 < scramble
In this group on 8 items are shown. The index is 5. Only two items are m is not scambled. This does not matter, the set is still hard to guess.
No,this is not MS-DOS code, but ut shows the value of having some kind on idea or conceptin mind. This modloe does not need the random funtion. But requires aknowledge of number sequences. using this, yu can hand code tables of numbers.
What I am sayhing is that you need to do more "top down" thinking before you code. Have some notes and examples of what yu can do by hand and then think about making code for it.
-
Simple number sequnces.
https://www.mathsisfun.com/numberpatterns.html
This can be one way of making a unusual sequence. But you can also do it by hand and get good results. The idea is a foundation on which to build. You can add complexity to it later.
Partial example: (only 8 items to show the idea.)
1 2 3 4 5 6 7 8 < order
A B C D E F G H
5 2 7 4 1 6 3 8 < scramble
Thank you that is helpful, but the problem isnt scrambling the code pattern, although yall did help me fix a problem that i was currently having. the problem now is that when i try to decrpyt the code, when i use variables like "%var%", they display as actual text instead of what the variable is suppose to display and when i encrypt the code, i use the "!" sign in "!var!", but they wont send out to the "!" sign to the encrypted text file because the max setlocal recursion is reached.