Vigenere Cipher (Encrypt & Decrypt)

Scripts e snippets scritti in GameMaker Language (gml)
Rispondi
Avatar utente
Xxshark888xX
Membro d'elite
Messaggi: 1497
Iscritto il: 30/05/2012, 19:17
Specialità: Un poco di tutto
Uso: GM:Studio 1.4 Pro
Località: Earth
Contatta:

Vigenere Cipher (Encrypt & Decrypt)

Messaggio da Xxshark888xX »

Salve :cappa:

Ho inziato di nuovo a programmare din tanto in tanto e dato che non ho mai provato a programmare un algoritmo per creare un sistema di criptaggio, ho deciso di iniziare con qualcosa di semplice. Cosi' ho ricostruito in GML il sistema di Vigenere, anche se un poco modificato. Dato che somma i valori in UNICODE o li sottrae se dobbiamo decriptare.

Lo script e' questo:

Codice: Seleziona tutto

///xs8_vigenereCipher(str, key, method);
 
/*
 
 ~~~ Script version 1.0 made by Xxshark888xX (Adriano Mutu) 6-Jan-2017 ~~~
 
|=========================|
| Arguments | Explanation |
|=========================|
| str       | The string you want to encipher or decipher
| key       | The key to encipher / decipher
| method    | 0 = encipher / 1 = decipher
 
*/
 
var _str, _key, _strVal, _strOut;
 
_str = argument0;
_key = argument1;
_strVal = 0;
_strOut = "";
 
if (string_length(_key) != string_length(_str)) { //if the key length isn't equal to the str length
    if (string_length(_key) < string_length(_str)) { //If the key length is less than str length
        //Repeat key char until key length reaches the str length
        var _i = 1;
        while (string_length(_key) < string_length(_str)) {
            _key += string_char_at(_key, _i);
            _i++;
        }
    } else { //If the key length is higher than str length
        //Cut the key char until reaches the str length
        _key = string_delete(_key, string_length(_str) + 1, string_length(_key) - string_length(_str));
    }
}
 
for (var i = 1; i <= string_length(_str); i++) {
    if (argument2 == 0) { //the method is to encipher
        _strVal = ord(string_char_at(_str, i)) + ord(string_char_at(_key, i));
        _strOut += chr(_strVal);
    } else { //the method is to decipher
        _strVal = ord(string_char_at(_str, i)) - ord(string_char_at(_key, i));
        _strOut += chr(_strVal);
    }
}
 
return _strOut;
Per utilizzarlo invece:

Per esempio se vogliamo criptare " GameMaker Italia " ci bastera' fare

Codice: Seleziona tutto

str_criptata = xs8_vigenereCipher("GameMaker Italia", "KeyA Caso", 0);
Ora, il risultato sara' questo : [’Ææ¦m¤ÌØák®í¢Œ¬Â]

N.B: Assicuratevi di copiare bene il testo criptato, perche' molti caratteri saranno "invisibili".

Invece per decriptare il testo che abbiamo appena criptato ci bastera' fare

Codice: Seleziona tutto

str_decriptata = xs8_vigenereCipher("’Ææ¦m¤ÌØák®í¢Œ¬Â", "KeyA Caso", 1);
Tengo a precisare che ho preferito seguire la regola classica del metodo di Vigenere, ovvero che la key deve avere la stessa lunghezza del testo da criptare / decriptare.
Lo script adatta automaticamente la key alla lunghezza del testo.

L'ho riscritto anche in C#

Codice: Seleziona tutto

private string xs8_vigenereCipher(string _str, string _key, int _method)
{
    /*

    ~~~ Script version 1.0.1 made by Xxshark888xX (Adriano Mutu) 7-Jan-2017 ~~~

    |=========================|
    | Arguments | Explanation | 
    |=========================|
    | str       | The string you want to encipher or decipher
    | key       | The key to encipher / decipher
    | method    | 0 = encipher / 1 = decipher

    */

    int _strVal = 0;
    string _strOut = "";
            
    if (_key.Length != _str.Length) { //if the key length isn't equal to the str length
        if (_key.Length < _str.Length) { //If the key length is less than str length
            //Repeat key char until key length reaches the str length
            int _i = 0;
            while (_key.Length < _str.Length) {
                _key += _key.Substring(_i, 1);
                _i++;
            }
        } else { //If the key length is higher than str length
            //Cut the key char until reaches the str length
            _key = _key.Remove(_str.Length, _key.Length - _str.Length);
        }
    }

    for (int i = 0; i < _str.Length; i++) {
        if (_method == 0) { //the method is to encipher
            foreach (char c in _str.Substring(i)) {
                _strVal = c;
                break;
            }
            foreach (char c in _key.Substring(i)) {
                _strVal += c;
                 break;
            }
            _strOut += Convert.ToChar(_strVal);
        } else { //the method is to decipher
            try {
                foreach (char c in _str.Substring(i)) {
                    _strVal = c;
                    break;
                }
                foreach (char c in _key.Substring(i)) {
                    _strVal -= c;
                    break;
                }
                _strOut += Convert.ToChar(_strVal);
            } catch { }
        }
    }

    return _strOut;
}
Contatti
Steam
Facebook

Gif
Spoiler
Immagine
Immagine
Immagine
Immagine

Rispondi

Chi c’è in linea

Visitano il forum: Nessuno e 4 ospiti