Game Maker Games, Articles, Tutorials & More

Game Maker Network


Howdy, Guest! Please sign in or register an account.

Print

string_encrypt

By Daniel · December 24, 2008

A standard encryption function

// string_encrypt(str, password)
// str: string to be encrypted
// password: password to encrypt string with
// Note: for optimal security, password should be ~6-8 characters or more, and not a common word/phrase/etc.

var str, pas, str_len, pas_len, str_pos, pas_pos, str_char, pas_char;

str = argument0;
pas = argument1;

str_len = string_length(str);
pas_len = string_length(pas);

pas_pos = 1;
for (str_pos = 1; str_pos <= str_len; str_pos += 1) { // For each character in the string to be encrypted
 // Read the character to be encrypted & the password character to encrypt with
 str_char = string_char_at(str,str_pos);
 pas_char = string_char_at(pas,pas_pos);
 // Convert to ASCII equivalents so we can manipulate them
 str_char = ord(str_char);
 pas_char = ord(pas_char);
 // Encrypt this character
 str_char += pas_char
 if str_char >= 256
  str_char -= 256;
 str_char = chr(str_char);
 str = string_delete(str,str_pos,1); // Delete current character
 str = string_insert(str_char,str,str_pos); // Replace with new encrypted character
 pas_pos += 1; // Go on to the next character in the password
 if pas_pos > pas_len // If end of password has been reached
  pas_pos = 1; // Repeat password from start
}

return str;

Categories: Encryption

Comments

There are no comments to display.

Post a Comment

You must be signed in to post comments.

Advertisement