GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2008-01-07 23:28:40

atarian
Member
Registered: 2008-01-07
Posts: 9

Ceaser_Cipher

hi everyone i'm new and due to the first topic i read i deciede to make a ceaser_cipher script so here it is




Expand #define caeser_cipher
/*caeser_cipher(text,num)
**
**  Arguments:
**  text           the text to be encrypted
**  num            the amount to move the letters by
**  
**
**  Returns:
**  Val            the encrypted text/decypt 
**
**  Notes:
**  To Decipher the text use the reverse of your enciphering number eg; 5 to cipher -5 to decipher
**  Will always return letters in lower case 
**
**  Credit:
**  To XOT for helping me comeup with ways to fix it
*/

var _str,_num,_en,_strlen,_currchar,val,_exnum;

    _str = argument0
    _num = argument1
    val = ""
    _strlen = string_length(_str)
    for (i = 1;i<=_strlen;i+=1)
    {
        _currchar = string_char_at(_str,i)
        if (ord(_currchar)+_num<=90)&&(ord(_currchar)+_num>=65)
        {
            _currchar = chr(ord(_currchar)+32)
        }
        if (ord(_currchar)+_num<=122)&&(ord(_currchar)+_num>=97)
        {  
        
            val += chr(ord(_currchar)+_num)
        }
        else
        {
            if (ord(_currchar)+_num>122)&&(ord(_currchar)+_num<=122-max(0,_num))
            {
                val+= chr(ord(_currchar)+_num-26)
            }
            else
            {
                if (ord(_currchar)+_num<97)&&(ord(_currchar)+_num>=97-min(0,_num))
                {
                    val+= chr(ord(_currchar)+_num+26)
                }
                else
                {
                    val += _currchar
                }
            }
        }
    }
    return val
Expand#define caeser_cipher_asci
/*caeser_cipher_asci(text,num)
**
**  Arguments:
**  text           the text to be encrypted
**  num            the amount to move the letters by
**  
**
**  Returns:
**  Val            the encrypted text/decypt 
**
**  Notes:
**  will return anscii encipher text so letters maybe reutned as symbols more usefull for encyption than the othe cipher
*/

var _str,_num,_en,_strlen,_currchar,val,_exnum;

    _str = argument0
    _num = argument1
    val = ""
    _strlen = string_length(_str)
    for (i = 1;i<=_strlen;i+=1)
    {
        _currchar = ord(string_char_at(_str,i))
        if (_currchar+_num>255)
        {
            _currchar-=255
        }
        if (_currchar+_num<0)
        {
            _currchar+=255
        }
            val += chr(_currchar+_num)
    }
    return val

any way here are the new scripts i think they work perfectly

edit : fixed a mistake

-atarian

Last edited by atarian (2008-02-13 02:14:50)

Offline

#2 2008-01-08 01:37:41

xot
Administrator
Registered: 2007-08-18
Posts: 1,239

Re: Ceaser_Cipher

Your Allman-style indentation looks fine to me (it exists and the code is readable, that's point). GMLscripts.com favors K&R-style (my personal style, naturally enough) but Allman/BSD is fine. I've been thinking about switching over to Allman because that is the style used in the Game Maker documentation. I see some places where it could be optimized. There are so many ways to write this kind of cipher.

These are the changes I would make:
1. Fix the spelling of Caesar.
2. Maybe pass characters that are outside of the alphabetic range unchanged.
3. Maybe make two versions like I did with my Vigenere cipher, an alphabetic version and a full 8-bit binary version.
4. I might get rid of the encipher/decipher argument. If a key of 5 enciphers, then the negative value of that key, -5, deciphers.
5. Both enciphering and deciphering could be accomplished with the same block of code, cutting the total amount of code in half.
6. Take out the bugs. For me, plaintext != ceasar_cipher(ceaser_cipher(plaintext, key, true), key, false). In other words, I can't seem to correctly decipher what I encipher.


Abusing forum power since 1986.

Offline

#3 2008-01-08 02:40:36

atarian
Member
Registered: 2008-01-07
Posts: 9

Re: Ceaser_Cipher

ok all of those ideas are done except for the 8 bit version
and i will replace the code soon, also xot i myself also made a Vigenere cipher before i check for other encryption scripts that have been made and i noticed in your example that you had made a mistake in a Vigenere cipher "a" counts as a 0 and b counts as a 1 have a look at the Vigenere cipher page on wikipedia correct me if i'm wrong

Offline

#4 2008-01-08 05:28:39

xot
Administrator
Registered: 2007-08-18
Posts: 1,239

Re: Ceaser_Cipher

Nice work, I wasn't really expecting you to make all those changes. The code looks a lot tighter, and it's basically working for me now, but there is one other bug fix that needs to be made. The returned ciphertext should be in the same range of characters, the same alphabet, as the accepted input. What I mean is, if this was a traditional Caesar, and the only letters you could use are the 26 letters of the alphabet, A-Z, the result would also only use the same 26 letters of the alphabet. As it stands now, the letters can be shifted beyond 'Z' so to speak. When that happens it should wrap around back to beginning of the alphabet. In a traditional 26 letter Caesar cipher Z+1 = A. Because it doesn't do this, letters that do get shifted beyond 'Z', or the last letter in the plaintext alphabet, aren't deciphered correctly. caesar_cipher(caesar_cipher("This is a test",20),-20) returns "hhis is a test".

Now, onto what you said in your post. Although my printable ASCII Vigenere cipher differs from a traditional Vigenere in a significant way (namely using a character set beyond the 26 letters of the alphabet, A-Z), you are absolutely correct. The first entry in the character set should always have a value of zero. Vigenere is such a simple cipher I don't think I ever did any research into it beyond what I thought I already knew about it. Thanks for pointing out the error, when I make the change I'll credit you as a contributor.


Abusing forum power since 1986.

Offline

#5 2008-01-10 18:35:06

atarian
Member
Registered: 2008-01-07
Posts: 9

Re: Ceaser_Cipher

ok i've fixed all that but now it returns the text in lowercase everytime i've also made a full asci version but i can't post it yet because the computer which i'm post on dosen't have gm i will hopefully post it soon

Offline

#6 2008-02-13 02:09:23

atarian
Member
Registered: 2008-01-07
Posts: 9

Re: Ceaser_Cipher

hi i'm back, soz i've been so long well, holiday then forgot then school started but any way here the new scripts (in the top post)

Offline

#7 2008-02-13 06:22:31

xot
Administrator
Registered: 2007-08-18
Posts: 1,239

Re: Ceaser_Cipher

OK, I'll look over the changes sometime tomorrow. It's late, I'm not really up to anything technical right now.


Abusing forum power since 1986.

Offline

Board footer

Powered by FluxBB