Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

A simple alternative method of encryption and decryption using the RND function

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • PBWin/PBCC A simple alternative method of encryption and decryption using the RND function

    ' Here is a simple alternative method of encryption and decryption
    ' using the RND function.

    ' Previously I presented a similar code where encryption was made
    ' by changing the CODE of each character in the text.

    ' The present code below performs the encryption by changing the
    ' POSITION of characters in the text string. The method uses the
    ' powerful STRREVERSE$ function repeatedly to shuffle the charac-
    ' ters. Other methods may also be used.
    '
    ' It would be easy to combine the two methods and thereby obtain
    ' an even stronger encryption.
    '
    Code:
    ' Here is a simple alternative method of encryption and decryption
    ' using the RND function.
    
    ' Previously I presented a similar code where encryption was made
    ' by changing the CODE of each character in the text.
    
    ' The present code below performs the encryption by changing the
    ' POSITION of characters in the text string. The method uses the
    ' powerful STRREVERSE$ function repeatedly to shuffle the charac-
    ' ters. Other methods may also be used.
    '
    ' It would be easy to combine the two methods and thereby obtain
    ' an even stronger encryption.
    '
    #COMPILE EXE
    #DIM ALL
    
    FUNCTION PBMAIN () AS LONG
        LOCAL i, k, le, lh AS LONG
        LOCAL s AS STRING
    
       s = "According to a report released by a pair of Russian opposition leaders, " + _
           "President Vladimir Putin enjoys the use of 20 official villas and residences " + _
           "around the country, a billion-dollar fleet of government jets, and a mini-armada " + _
           "of state-owned luxury yachts."
    
        MSGBOX s
    
        ' encrypt
        RANDOMIZE 6932877               ' should be the same for
        RANDOMIZE RND(9997, 3785265)    ' encryption and decryption
        le = LEN(s) : lh = le \ 2
        k = RND(405, 687)
        DIM a(1 TO k) AS LOCAL LONG
        DIM b(1 TO k) AS LOCAL LONG
        FOR i = 1 TO k
            a(i) = RND(0, lh)           ' make arrays of
            b(i) = RND(lh + 1, le - 1)  ' random numbers
        NEXT
    
        FOR i = 1 TO k                  ' "forward" loop
            s = LEFT$(s, a(i)) + STRREVERSE$(MID$(s, a(i)+1, b(i)-a(i)+1))+ RIGHT$(s, le-b(i)-1)
        NEXT
    
        MSGBOX s
    
        ' decrypt
        RANDOMIZE 6932877               ' should be the same for
        RANDOMIZE RND(9997, 3785265)    ' encryption and decryption
        le = LEN(s) : lh = le \ 2
        k = RND(405, 687)
        DIM a(1 TO k) AS LOCAL LONG
        DIM b(1 TO k) AS LOCAL LONG
    
        FOR i = 1 TO k
            a(i) = RND(0, lh)           ' make arrays of
            b(i) = RND(lh + 1, le-1)    ' random numbers
        NEXT
    
        FOR i = k TO 1 STEP -1          ' "backward" loop producing the REVERSE sequence of events
            s = LEFT$(s, a(i)) + STRREVERSE$(MID$(s, a(i)+1, b(i)-a(i)+1))+ RIGHT$(s, le-b(i)-1)
        NEXT
    
        MSGBOX s
    
    END FUNCTION
    Last edited by Erik Christensen; 6 Sep 2012, 10:21 AM. Reason: changed to a(i) = RND(0, lh) to cover also the beginning of the string

  • #2
    Instead of using RND the following uses something that I am currently working on. It is George Marsaglia's CMWC with a lag of 256. So, instead of RND's Dword seed we use 256 Dwords with a period of roughly (2^32)^256. This would make an attacker contemplating a seed attack very miserable.

    The random range is about three times faster than RND(a,b).

    Your example, Erik, was very useful. The library has been stable after a few days crunching but GPF'd on your code. It now encrypts/decrypts as the RND version. I hope to be putting it into the Third_Party Addons in a week or so. Attached is a zip but it should be treated as suspect for the time being.

    Added: The AES CSP is used for the Seed method so the minimum desktop OS is XP SP3. I need to introduce some code to 'pull out' gracefully.

    Code:
    #Compile Exe
    #Dim All
    
    #Link "F:\PBWin\CMWC\Objects\ObjectRand.pblib" ' Edit To your location Of ObjectRand.pblib
    
    Function PBMain () As Long
        Local i, k, le, lh As Long
        Local s As String
        
       Local RandUsr As IUserRandom
       RandUsr = Class "UserRandom" 
    
       s = "According to a report released by a pair of Russian opposition leaders, " + _
           "President Vladimir Putin enjoys the use of 20 official villas and residences " + _
           "around the country, a billion-dollar fleet of government jets, and a mini-armada " + _
           "of state-owned luxury yachts."
    
       MsgBox s
    
        ' encrypt
        'Randomize 6932877               ' should be the same For
        'Randomize Rnd(9997, 3785265)    ' encryption And decryption
        RandUsr.Seed("An easy to remember phrase and should be the same for encryption and decryption")
        le = Len(s) : lh = le \ 2
        k = RandUsr.R(405, 687)
        Dim a(1 To k) As Local Long
        Dim b(1 To k) As Local Long
        For i = 1 To k
            a(i) = RandUsr.R(2, lh)           ' make arrays Of
            b(i) = RandUsr.R(lh + 1, le - 1)  ' Random numbers
        Next
    
        For i = 1 To k                  ' "forward" Loop
            s = Left$(s, a(i)) + StrReverse$(Mid$(s, a(i)+1, b(i)-a(i)+1))+ Right$(s, le-b(i)-1)
        Next
    
        MsgBox s
    
        ' decrypt
        'Randomize 6932877               ' should be the same For
        'Randomize Rnd(9997, 3785265)    ' encryption And decryption
        RandUsr.Seed("An easy to remember phrase and should be the same for encryption and decryption")
        
        le = Len(s) : lh = le \ 2
        k = RandUsr.R(405, 687)
        Dim a(1 To k) As Local Long
        Dim b(1 To k) As Local Long
    
        For i = 1 To k
            a(i) = RandUsr.R(2, lh)           ' make arrays Of
            b(i) = RandUsr.R(lh + 1, le-1)    ' Random numbers
        Next
    
        For i = k To 1 Step -1          ' "backward" Loop producing the REVERSE sequence Of Events
            s = Left$(s, a(i)) + StrReverse$(Mid$(s, a(i)+1, b(i)-a(i)+1))+ Right$(s, le-b(i)-1)
        Next
    
        MsgBox s
    
    End Function
    Attached Files
    Last edited by David Roberts; 6 Sep 2012, 10:06 AM.

    Comment


    • #3
      David, that sounds fine!

      Comment


      • #4
        ... and if the seed was an input and we have access to a password manager we could use something like this:

        RandUsr.Seed("y@Z71ykO(.|eCQ%0B+vEN2T6}bt<LF@3$U^/C:q.^]{u3p2&'~F@j3PN#XfN$UD[")

        No one will guess that in a hurry with nearly 420 bits of entropy; 2^340 times stronger than the minimum password entropy recommended by the NIST. We don't lose any of that as the underlying algorithm uses SHA512.

        Comment


        • #5
          I just found this: http://www.powerbasic.com/support/pb...ad.php?t=24531
          You may find these random number generators of interest.

          Comment


          • #6
            LCGs are old hat nowadays, along with DES. Check this out - especially 'Advantages and disadvantages of LCGs'.

            Comment


            • #7
              ' Here is a another simple method of encryption
              ' and decryption using the RND function.

              ' Like the previous this code performs encryption by changing the
              ' POSITION of characters in the text string.
              '
              ' Please note that even if two or more of the random numbers turn out
              ' to be identical, the method is still valid, because the sorting procedure
              ' is exactly the same in encryption and decryption.
              '
              ' This method can also be combined with the character CODE changing
              ' method to produce a stronger encryption.
              Code:
              ' Here is a another simple method of encryption
              ' and decryption using the RND function.
              
              ' Like the previous this code performs encryption by changing the
              ' POSITION of characters in the text string.
              '
              ' Please note that even if two or more of the random numbers turn out
              ' to be identical, the method is still valid, because the sorting procedure
              ' is exactly the same in encryption and decryption.
              '
              ' This method can also be combined with the character CODE changing
              ' method to produce a stronger encryption.
              
              #COMPILE EXE
              #DIM ALL
              
              FUNCTION PBMAIN () AS LONG
                  LOCAL i, le AS LONG
                  LOCAL s, s2 AS STRING
              
                  s = "France has unveiled its budget " + _
                      "for 2013, avoiding big austerity spending cuts in favour of higher taxes on the " + _
                      "wealthy and big businesses. French Prime Minister Jean-Marc Ayrault confirmed that " + _
                      "there is to be a new 75% tax rate for people earning more than 1m euros a year."
              
                  MSGBOX s
              
                  ' encrypt
                  RANDOMIZE 1469328                 ' should be the same for
                  RANDOMIZE RND(159997, 4378526)    ' encryption and decryption
                  le = LEN(s)
                  DIM a(1 TO le) AS LOCAL LONG
                  DIM b(1 TO le) AS LOCAL LONG
                  FOR i = 1 TO le
                      a(i) = i                 ' make index array
                      b(i) = RND(1, 20000000)  ' make array of random numbers
                  NEXT
              
                  ARRAY SORT b(), TAGARRAY a()
              
                  s2 = SPACE$(le)
                  FOR i = 1 TO le
                      ASC(s2, a(i)) = ASC(s, i)
                  NEXT
              
                  MSGBOX s2
              
                  ' decrypt
                  RANDOMIZE 1469328                 ' should be the same for
                  RANDOMIZE RND(159997, 4378526)    ' encryption and decryption
              
                  le = LEN(s2)
                  DIM a(1 TO le) AS LOCAL LONG
                  DIM b(1 TO le) AS LOCAL LONG
                  FOR i = 1 TO le
                      a(i) = i                 ' make index array
                      b(i) = RND(1, 20000000)  ' make array of random numbers
                  NEXT
              
                  ARRAY SORT b(), TAGARRAY a()
              
                  s = SPACE$(le)
                  FOR i = 1 TO le
                      ASC(s, i) = ASC(s2, a(i))
                  NEXT
              
                  MSGBOX s
              
              END FUNCTION
              Last edited by Erik Christensen; 28 Sep 2012, 03:25 PM.

              Comment

              Working...
              X