Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Find and Replace (Encryption)  (Read 8128 times)

0 Members and 1 Guest are viewing this topic.

Jacob

    Topic Starter


    Hopeful

    Thanked: 1
    • Experience: Expert
    • OS: Windows XP
    Find and Replace (Encryption)
    « on: November 27, 2008, 09:56:43 AM »
    I am in the process of making some kind of encrypter and decrypter, for this I need to find and replace letters, for instance "a" will be replaced with "$".

    I have already searched around but the topics I have seen are inconclusive, thanks to anybody who offers any kind of support.

    I am looking for help on how to find and replace strings in a text file.
    « Last Edit: November 27, 2008, 10:37:47 AM by Jacob »

    Prince_



      Beginner

      Thanked: 5
      Re: Find and Replace (Encryption)
      « Reply #1 on: November 27, 2008, 10:03:00 AM »
      Quote
      for instance "a" will be replaced with "$".
      what if there is $ in the original codes?

      Jacob

        Topic Starter


        Hopeful

        Thanked: 1
        • Experience: Expert
        • OS: Windows XP
        Re: Find and Replace (Encryption)
        « Reply #2 on: November 27, 2008, 10:08:14 AM »
        Quote
        for instance "a" will be replaced with "$".
        what if there is $ in the original codes?
        It's a text file. It is only meant to contain text. Then if that happens I will replace "a" with "\$1" it was simply an example, to help the person who answers my question.

        Besides, then "$" will be replaced with "^&".
        « Last Edit: November 27, 2008, 10:22:24 AM by Jacob »

        Prince_



          Beginner

          Thanked: 5
          Re: Find and Replace (Encryption)
          « Reply #3 on: November 27, 2008, 10:29:26 AM »
          i think you may replace each other in the 26 chars.
          e.g.

          a->b
          b->c
          c->d
          ...
          z->a

          Jacob

            Topic Starter


            Hopeful

            Thanked: 1
            • Experience: Expert
            • OS: Windows XP
            Re: Find and Replace (Encryption)
            « Reply #4 on: November 27, 2008, 10:31:46 AM »
            You don't get it do you.
            I will be replacing only text but I can encrypt that into any symbol.
            Code: [Select]
            a -> !!
            b -> *!
            c -> (!
            d -> ^!
            e -> :!
            as an example, and to decrypt:
            Code: [Select]
            !! -> a
            *! -> b
            (! -> c
            ^! -> d
            :! -> e
            I would prefer it if you no longer posted if you can provide no help on my topic.

            Prince_



              Beginner

              Thanked: 5
              Re: Find and Replace (Encryption)
              « Reply #5 on: November 27, 2008, 10:35:10 AM »
              srr, but what help do you need? i dun follow you...

              Dias de verano

              • Guest
              Re: Find and Replace (Encryption)
              « Reply #6 on: November 27, 2008, 10:37:09 AM »
              To replace text in a string, you can use the set command thus

              set string=%string:a=b%

              where a and b are strings

              Code: [Select]
              C:\>set string=bab

              C:\>set string=%string:a=o%

              C:\>echo %string%
              bob

              C:\>set string=%string:o=eee%

              C:\>echo %string%
              beeeb

              C:\>set string=%string:e=^!%

              C:\>echo %string%
              b!!!b

              Jacob

                Topic Starter


                Hopeful

                Thanked: 1
                • Experience: Expert
                • OS: Windows XP
                Re: Find and Replace (Encryption)
                « Reply #7 on: November 27, 2008, 10:38:18 AM »
                To replace text in a string, you can use the set command thus

                set string=%string:a=b%

                where a and b are strings

                Code: [Select]
                C:\>set string=bab

                C:\>set string=%string:a=o%

                C:\>echo %string%
                bob

                C:\>set string=%string:o=eee%

                C:\>echo %string%
                beeeb

                C:\>set string=%string:e=^!%

                C:\>echo %string%
                b!!!b


                Thanks you very much, problem solved.
                But how would i get this to run through everything in a text file rather than just a variable.

                Dias de verano

                • Guest
                Re: Find and Replace (Encryption)
                « Reply #8 on: November 27, 2008, 10:42:25 AM »
                But how would i get this to run through everything in a text file rather than just a variable.

                An interesting exercise for you, Jacob.

                You can read the text file line by line into a variable using FOR, as I am sure you must know by now. Inside the loop you can do all the string replacements in the line of text, and then write it out to a new file.

                Prince_



                  Beginner

                  Thanked: 5
                  Re: Find and Replace (Encryption)
                  « Reply #9 on: November 27, 2008, 10:43:31 AM »
                  you dun know how to replace a char in a string? ...
                  then i have to tell you that there are lots of limitations when the cmd.exe dealing with the replacement.
                  i advise that you give up your complex idea about the encryption, juz use my method.

                  Jacob

                    Topic Starter


                    Hopeful

                    Thanked: 1
                    • Experience: Expert
                    • OS: Windows XP
                    Re: Find and Replace (Encryption)
                    « Reply #10 on: November 27, 2008, 10:45:49 AM »
                    But how would i get this to run through everything in a text file rather than just a variable.

                    An interesting exercise for you, Jacob.

                    You can read the text file line by line into a variable using FOR, as I am sure you must know by now. Inside the loop you can do all the string replacements in the line of text, and then write it out to a new file.


                    Sorry, I do not know how to set a line as a variable.

                    Dias de verano

                    • Guest
                    Re: Find and Replace (Encryption)
                    « Reply #11 on: November 27, 2008, 10:46:32 AM »
                    you dun know how to replace a char in a string? ...
                    then i have to tell you that there are lots of limitations when the cmd.exe dealing with the replacement.
                    i advise that you give up your complex idea about the encryption, juz use my method.

                    What method? I don't see any method. Just an idea. Both your idea and Jacob's are equivalent. The classic simple substitution cypher.

                    Dias de verano

                    • Guest
                    Re: Find and Replace (Encryption)
                    « Reply #12 on: November 27, 2008, 10:49:23 AM »
                    But how would i get this to run through everything in a text file rather than just a variable.

                    An interesting exercise for you, Jacob.

                    You can read the text file line by line into a variable using FOR, as I am sure you must know by now. Inside the loop you can do all the string replacements in the line of text, and then write it out to a new file.


                    Sorry, I do not know how to set a line as a variable.

                    You did on Oct 24th

                    http://www.computerhope.com/forum/index.php/topic,69075.msg451057.html#msg451057

                    Jacob

                      Topic Starter


                      Hopeful

                      Thanked: 1
                      • Experience: Expert
                      • OS: Windows XP
                      Re: Find and Replace (Encryption)
                      « Reply #13 on: November 27, 2008, 10:51:41 AM »
                      Code: [Select]
                      If in doubt, delims== will always grab the whole line (which is what you want.) Note I used set /a because the 2nd line of the output file
                      from wmic contains a TAB character after the percentage number, which I wanted to remove

                      So, I did, thanks, and sorry.  :)

                      Prince_



                        Beginner

                        Thanked: 5
                        Re: Find and Replace (Encryption)
                        « Reply #14 on: November 27, 2008, 10:52:33 AM »
                        Quote
                        Sorry, I do not know how to set a line as a variable.

                        ...

                        Code: [Select]
                        C:\>type test.txt
                        hello
                        world
                        C:\>for /f %a in (test.txt) do @set line=%a

                        C:\>echo %line%
                        world

                        Prince_



                          Beginner

                          Thanked: 5
                          Re: Find and Replace (Encryption)
                          « Reply #15 on: November 27, 2008, 10:56:57 AM »
                          you dun know how to replace a char in a string? ...
                          then i have to tell you that there are lots of limitations when the cmd.exe dealing with the replacement.
                          i advise that you give up your complex idea about the encryption, juz use my method.

                          What method? I don't see any method. Just an idea. Both your idea and Jacob's are equivalent. The classic simple substitution cypher.


                          what i wanna say is that the Jacob's idea of replacement is too complex to realize, for him especially

                          Jacob

                            Topic Starter


                            Hopeful

                            Thanked: 1
                            • Experience: Expert
                            • OS: Windows XP
                            Re: Find and Replace (Encryption)
                            « Reply #16 on: November 27, 2008, 11:04:38 AM »
                            you dun know how to replace a char in a string? ...
                            then i have to tell you that there are lots of limitations when the cmd.exe dealing with the replacement.
                            i advise that you give up your complex idea about the encryption, juz use my method.

                            What method? I don't see any method. Just an idea. Both your idea and Jacob's are equivalent. The classic simple substitution cypher.


                            what i wanna say is that the Jacob's idea of replacement is too complex to realize, for him especially

                            Sorry, suddenly because I do not know how to replace strings in a text file, I do not understand the idea of encryption? One, cannot know everything.

                            Dias de verano

                            • Guest
                            Re: Find and Replace (Encryption)
                            « Reply #17 on: November 27, 2008, 11:06:27 AM »
                            what i wanna say is that the Jacob's idea of replacement is too complex to realize, for him especially

                            What I want to say is that your unhelpful and snooty attitude is not appropriate for this forum.

                            Jacob, you seem to want to replace each letter of the alphabet with a pair of characters. This is all very well, but you should realise that certain character pairs have special meaning in cmd scripts, particularly if the first character of the pair is a caret (^). For example ^! is replaced by cmd with !

                            Jacob

                              Topic Starter


                              Hopeful

                              Thanked: 1
                              • Experience: Expert
                              • OS: Windows XP
                              Re: Find and Replace (Encryption)
                              « Reply #18 on: November 27, 2008, 11:08:44 AM »
                              what i wanna say is that the Jacob's idea of replacement is too complex to realize, for him especially

                              What I want to say is that your unhelpful and snooty attitude is not appropriate for this forum.

                              Jacob, you seem to want to replace each letter of the alphabet with a pair of characters. This is all very well, but you should realise that certain character pairs have special meaning in cmd scripts, particularly if the first character of the pair is a caret (^). For example ^! is replaced by cmd with !

                              Yes, I am currently stuck with the following, I have dinner now, so is it okay if I leave you to ponder around with this?
                              Code: [Select]
                              @echo off
                              set FileName=text.txt
                              for /f "skip=0 delims==" %%A in ('type %FileName%') do (
                              set string=%%A
                              set string=%string:a=o%
                              echo %string%>!%FileName%
                              )
                              pause >nul
                              « Last Edit: November 27, 2008, 11:19:02 AM by Jacob »

                              Prince_



                                Beginner

                                Thanked: 5
                                Re: Find and Replace (Encryption)
                                « Reply #19 on: November 27, 2008, 11:09:33 AM »
                                In actual fact, i've done the work about one year before ,which you're doing now. I know how hard it is while replacing some chars.

                                Jacob

                                  Topic Starter


                                  Hopeful

                                  Thanked: 1
                                  • Experience: Expert
                                  • OS: Windows XP
                                  Re: Find and Replace (Encryption)
                                  « Reply #20 on: November 27, 2008, 11:17:56 AM »
                                  In actual fact, i've done the work about one year before ,which you're doing now. I know how hard it is while replacing some chars.
                                  I very much doubt that, or you would of been able to help me, like I said before, I'd prefer it if you do not post on my thread anymore.

                                  Prince_



                                    Beginner

                                    Thanked: 5
                                    Re: Find and Replace (Encryption)
                                    « Reply #21 on: November 27, 2008, 11:20:45 AM »
                                    Quote
                                    What I want to say is that your unhelpful and snooty attitude is not appropriate for this forum.

                                    unhelpful? snooty?
                                    im trying my best to help him, and if i said something wrong when you think im snooty, i make an apology

                                    Dias de verano

                                    • Guest
                                    Re: Find and Replace (Encryption)
                                    « Reply #22 on: November 27, 2008, 11:32:04 AM »

                                    Yes, I am currently stuck with the following, I have dinner now, so is it okay if I leave you to ponder around with this?
                                    Code: [Select]
                                    @echo off
                                    set FileName=text.txt
                                    for /f "skip=0 delims==" %%A in ('type %FileName%') do (
                                    set string=%%A
                                    set string=%string:a=o%
                                    echo %string%>!%FileName%
                                                      ^
                                                 
                                    )
                                    pause >nul

                                    Code: [Select]
                                    echo %string%>!%FileName%
                                                  ^
                                                  |
                                                  |
                                         Why is this here?

                                    You need to learn about delayed expansion. Variables in a parenthetical expression such as a FOR loop or a block IF structure are not updated because of the way cmd expands variables at runtime. You need to use a special technique...

                                    1. Place this line before the code

                                    setlocal enabledelayedexpansion

                                    2. Use ! signs instead of % signs inside the loop for the variables you wish to change as the loop runs.

                                    Code: [Select]
                                    @echo off
                                    setlocal enabledelayedexpansion
                                    set FileName=text.txt
                                    for /f "skip=0 delims==" %%A in ('type %FileName%') do (
                                    set string=%%A
                                    set string=!string:a=o!
                                    echo !string!>%FileName%
                                    )
                                    pause >nul

                                    You do know that your code will only work if the file %FileName% contains exactly one line?

                                    Jacob

                                      Topic Starter


                                      Hopeful

                                      Thanked: 1
                                      • Experience: Expert
                                      • OS: Windows XP
                                      Re: Find and Replace (Encryption)
                                      « Reply #23 on: November 27, 2008, 11:45:11 AM »
                                      Thank you, but I get:
                                      Code: [Select]
                                      delims==" was unexpected at this timeand the
                                      Code: [Select]
                                      echo %string%>!%FileName%was there to differentiate a crypted file and a non crypted file.

                                      And i am aware that this will only work for one line, but i am planning on looping it much like this:
                                      Code: [Select]
                                      :loop
                                      find /c " " data\accounts\%AccName%\bank.txt>data\temp\linecount\%AccName%.txt
                                      for /f "tokens=1,2,3 delims= " %%A in ('type data\temp\linecount\%AccName%.txt ^| find "BANK.TXT:"') do (
                                          set count=%%C
                                          )
                                      set /a id=%id%+1
                                      for /f "tokens=1,2,3 delims=:" %%A in ('type data\accounts\%AccName%\bank.txt ^| find "=%id%="') do (
                                          set itemidno=%%B
                                      set itemq=%%C
                                          )
                                      for /f "tokens=1,2,3 delims=:" %%A in ('type data\item\items.txt ^| find "ID=%itemidno%="') do (
                                          set itemn=%%B
                                      set itemp=%%C
                                          )
                                      set /a itemw=%itemp%*%itemq%
                                      echo     %itemq% x %itemn% [%itemw%]
                                      if %ID% NEQ %count% (
                                      goto loop
                                      )

                                      Dias de verano

                                      • Guest
                                      Re: Find and Replace (Encryption)
                                      « Reply #24 on: November 27, 2008, 11:53:36 AM »
                                      Thank you, but I get:
                                      Code: [Select]
                                      delims==" was unexpected at this time

                                      This is because skip=0 is not allowed. You must use a value of 1 or more. If you are skipping 0 lines, omit the skip=N part altogether.

                                      diablo416



                                        Hopeful
                                        • Experience: Beginner
                                        • OS: Unknown
                                        Re: Find and Replace (Encryption)
                                        « Reply #25 on: November 27, 2008, 12:42:37 PM »

                                        ive tryed something similar to this, i gave up on it because there was already third party software that would do this from cmd.exe,  the problem i came across in the end is the unknown amount of charachters in the current line,

                                        i thought of another way to do this, but i could never make it work as i dident understand setlocal enabledelayedexpansion well enough, something like this
                                        just as an example

                                        @echo off
                                        set cn=0
                                        setlocal enabledelayedexpansion
                                        for /f "Tokens=1*" %%a in (file) do (
                                               call set line=%%a%%b
                                               if "!%line:~!%cn%!,1%!"=="w" set file.%!cn!%=u
                                               call set /a cn=%!cn!%+1
                                        )


                                        that wont work

                                        diablo416



                                          Hopeful
                                          • Experience: Beginner
                                          • OS: Unknown
                                          Re: Find and Replace (Encryption)
                                          « Reply #26 on: November 27, 2008, 12:49:46 PM »
                                          you might also consider, having every charachter on a new line.. for example


                                          @echo off
                                          :TOP
                                          set /p encr=-
                                          set mes=%mes%%encr:~0,1%
                                          echo.&echo %encr:~0,1%>>FILE
                                          CLS&Echo %mes%&Pause>nul & goto TOP


                                          as a way of writting the message to be encrypted then just using if statements

                                          Dias de verano

                                          • Guest
                                          Re: Find and Replace (Encryption)
                                          « Reply #27 on: November 27, 2008, 12:59:25 PM »
                                          You can get the length of any string by echoing it to a temp file and then using FOR to get the file size, and finally subtracting 2 from that (for the cr and lf at the end)

                                          Code: [Select]
                                          @echo off
                                          set string=123456789
                                          echo %string%>%temp%\getlength.txt
                                          for %%L in (%temp%\getlength.txt) do set /a slen=%%~zL
                                          set /a slen-=2
                                          echo length %slen%

                                          Note: no space between the variable name and the > symbol




                                          devcom



                                            Apprentice

                                            Thanked: 37
                                            Re: Find and Replace (Encryption)
                                            « Reply #28 on: November 27, 2008, 01:10:06 PM »
                                            i've made sth like encrypting program based on random generated key's
                                            ex of usage:

                                            Code: [Select]
                                            Password: computerhope
                                            Key: 6CLgj9e1rtH4pFuNXDoA7mzqVY82WskB0I3
                                            Generated: 402YDtFz0t
                                            Code: [Select]
                                            Password: computerhope
                                            Key: Z19Es6WG2xFa4yrHctmI5D3UqoV7jNB80L
                                            Generated: a07otxy30x

                                            decrypt work well too
                                            Download: Choice.exe

                                            Dias de verano

                                            • Guest
                                            Re: Find and Replace (Encryption)
                                            « Reply #29 on: November 27, 2008, 01:19:31 PM »
                                            Code: [Select]
                                            @echo off
                                            setlocal enabledelayedexpansion
                                            set string=Mary had a little lamb

                                            REM Get string length
                                            echo %string%>%temp%\getlength.txt
                                            for %%L in (%temp%\getlength.txt) do set /a slen=%%~zL
                                            set /a slen-=2
                                            echo String is %string%
                                            echo String length is  %slen%


                                            REM show each character in the line
                                            for /L %%C in (1,1,%slen%) do (
                                            set /a n=%%C-1
                                                    call set char=%%string:~!n!,1%%
                                                    echo Character %%C   is  !char!
                                            )




                                            Jacob

                                              Topic Starter


                                              Hopeful

                                              Thanked: 1
                                              • Experience: Expert
                                              • OS: Windows XP
                                              Re: Find and Replace (Encryption)
                                              « Reply #30 on: November 28, 2008, 09:24:51 AM »
                                              I think i will not continue with this project, but I have learned a few things, thank you everyone.