Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Encrypting A String

morphiosmorphios Posts: 16Member
[b][red]This message was edited by morphios at 2004-8-20 9:10:57[/red][/b][hr]
Hello Everyone

I am trying to write a function, which would encrypt a given string using a simple substitution cipher, but I am unsure how to go about it. I understand the concept of substitution (I could decrypt it in moments by hand, but writing code is something else) and have an array with the key e.g.:

[CODE]
function Encrypt(aString)
{
var cipherArray =
['s','c','h','n','i','t','z','e','l','m','o','p','q','r','u',
'v','w','x','y','a','b','d','f','g','j','k'];

// schnitzel is the key
[/CODE]

I thought maybe I also needed another array to substitute the cipherArray with original characters such as:

[CODE]
var characterArray =
['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o',
'p','q','r','s','t','u','v','w','x','y','z'];
[/CODE]

This is where I become stuckwhat to do next? How do I now get from user inputting string to encrypting the given string?

Does anyone know of any websites, which would help me go onto the next stage, or is anyone will to help (please).

Any help appreciated, Thanks

Morphios


Comments

  • zibadianzibadian Posts: 6,349Member
    : Hello Everyone
    :
    : I am trying to write a function, which would encrypt a given string using a simple substitution cipher, but I am unsure how to go about it. I understand the concept of substitution (I could decrypt it in moments by hand, but writing code is something else) and have an array with the key e.g.:
    :
    : [CODE]
    : function Encrypt(aString)
    : {
    : var cipherArray =
    : ['s','c','h','n','i','t','z','e','l','m','o','p','q','r','u',
    : 'v','w','x','y','a','b','d','f','g','j','k'];
    :
    : // schnitzel is the key
    : [/CODE]
    :
    : I thought maybe I also needed another array to substitute the cipherArray with original characters such as:
    :
    : [CODE]
    : var characterArray =
    : ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o',
    : 'p','q','r','s','t','u','v','w','x','y','z'];
    : [/CODE]
    :
    : This is where I become stuckwhat to do next? How do I now get from user inputting string to encrypting the given string?
    :
    : Does anyone know of any websites, which would help me go onto the next stage, or is anyone will to help (please).
    :
    : Any help appreciated, Thanks
    :
    : Morphios
    :
    Getting the user input is described here: http://developer.irt.org/script/162.htm
    The encryption itself starts with an empty encryptedString. The encryptedString is filled using a for-loop (http://www.pageresource.com/jscript/jifelse.htm), in which you find the letter index in the characterArray, and then add the character with the same index from the cipherArray to it. Finding the character in the characterArray involves another loop. In pseudocode this should look something like this:
    [code]
    var encryptedString = '';
    var charIndex = 0;
    for (i = loop over user-input string)
    {
    for (j = loop over characterArray)
    {
    if (user-input[i] == characterArray[j])
    {
    charIndex = j;
    }
    }
    encryptedString == encryptedString + cipherArray[charIndex];
    }
    [/code]
    The decryption process is very similar. Remember that this is a very poor encryption and therefor shouldn't be used for any serious encryption applications.
  • morphiosmorphios Posts: 16Member
    [italic][red]The decryption process is very similar. Remember that this is a very poor encryption and therefor shouldn't be used for any serious encryption applications.[/red][/italic]

    zibadian, thank you for the links, I will take a look at them. Thanks for the fragment of code also, I think it is just the guide I have been looking for.

    Thanks again. I may be back. ;-)

  • morphiosmorphios Posts: 16Member
    Hello Again Everyone

    This is what I have so far (with help from zibadian). As I have mentioned before, I am trying to write something simple, that takes input from a user and using a simple substitution cipher encrypts the users input.

    Can anyone see (because I have now lost my marbles) the problem, when I enter a string I cannot get an encrypted output. Im hoping its something fairly simple but knowing me Ive missed a whole chunk of something. I can get output but it keeps showing 'undefined'.

    Can anyone help?

    [code]
    function doEncrypt()
    {

    var givenString,encodedString;
    document.encipher.outputString.value = '';
    givenString = document.encipher.inputString.value;
    givenString = givenString.toLowerCase();// note that toLowerCase leaves non-alpha characters unchanged
    encodedString = subEncrypt(givenString);
    document.encipher.outputString.value = encodedString;
    document.encipher.inputString.focus();// reset focus

    }


    function subEncrypt(aString)
    {
    var cipherArray =
    ['s','c','h','n','i','t','z','e','l','m','o','p','q','r','u',
    'v','w','x','y','a','b','d','f','g','j','k']; // schnitzel is the key


    var characterArray =
    ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o',
    'p','q','r','s','t','u','v','w','x','y','z'];


    var encryptedString = '';
    var charIndex = 0;

    for (var i = 0; i < aString.length; i++)
    {
    for (var j = 0; j < characterArray.length; j++)
    {
    if (aString.length[i] == characterArray[j])
    {
    charIndex = j;
    }
    }
    };
    encryptedString = encryptedString + cipherArray[charIndex]
    }
    [/code]

    Any help appreciated.

  • zibadianzibadian Posts: 6,349Member
    : Hello Again Everyone
    :
    : This is what I have so far (with help from zibadian). As I have mentioned before, I am trying to write something simple, that takes input from a user and using a simple substitution cipher encrypts the users input.
    :
    : Can anyone see (because I have now lost my marbles) the problem, when I enter a string I cannot get an encrypted output. Im hoping its something fairly simple but knowing me Ive missed a whole chunk of something. I can get output but it keeps showing 'undefined'.
    :
    : Can anyone help?
    :
    : [code]
    : function doEncrypt()
    : {
    :
    : var givenString,encodedString;
    : document.encipher.outputString.value = '';
    : givenString = document.encipher.inputString.value;
    : givenString = givenString.toLowerCase();// note that toLowerCase leaves non-alpha characters unchanged
    : encodedString = subEncrypt(givenString);
    : document.encipher.outputString.value = encodedString;
    : document.encipher.inputString.focus();// reset focus
    :
    : }
    :
    :
    : function subEncrypt(aString)
    : {
    : var cipherArray =
    : ['s','c','h','n','i','t','z','e','l','m','o','p','q','r','u',
    : 'v','w','x','y','a','b','d','f','g','j','k']; // schnitzel is the key
    :
    :
    : var characterArray =
    : ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o',
    : 'p','q','r','s','t','u','v','w','x','y','z'];
    :
    :
    : var encryptedString = '';
    : var charIndex = 0;
    :
    : for (var i = 0; i < aString.length; i++)
    : {
    : for (var j = 0; j < characterArray.length; j++)
    : {
    : if (aString.length[i] == characterArray[j])
    : {
    : charIndex = j;
    : }
    : }
    : };
    : encryptedString = encryptedString + cipherArray[charIndex]
    : }
    : [/code]
    :
    : Any help appreciated.
    :
    :
    All you need to add is the return statement to the subEncrypt() function. Currently it encode the string, but doesn't return the resulting value. The code for that is quite simple:
    [code]
    return encryptedString;
    [/code]
    Also there are several mistakes in the encryption routine. First, the statement
    [code]
    encryptedString = encryptedString + cipherArray[charIndex]
    [/code]
    must be inside the outer loop, otherwise it will only add the last character and not the rest.
    Secondly, the line
    [code]
    if (aString.length[i] == characterArray[j])
    [/code]
    is invalid, since the length property is an integer and not an array. To get a single character from a string, you need to say:
    [code]
    if (aString[i] == characterArray[j])
    [/code]
    After you made thse changes and additions it should work.
  • morphiosmorphios Posts: 16Member
    [red]Zibadian, thank you once again for taking the time to help me.[/red]

    I have one last question [red](if you don't mind)[/red]

    How do you stop it encrypting the spaces in a given string?
  • zibadianzibadian Posts: 6,349Member
    : [red]Zibadian, thank you once again for taking the time to help me.[/red]
    :
    : I have one last question [red](if you don't mind)[/red]
    :
    : How do you stop it encrypting the spaces in a given string?
    :
    Add the space character to both the cipher array and the character array. If they share the same index, the space will not be encrypted.
    An alternative is to set charIndex to -1 (indicating not found) before searching for the correct index. If the charIndex is still -1 after search, the character isn't listed, and you should copy the character from the plaintext string into the encrypted string. This is a more general way of handling other characters than letters.
  • morphiosmorphios Posts: 16Member
    Thank you very much for all the help.

    Morphios
  • morphiosmorphios Posts: 16Member
    [red][italic]Thank you very much for all the help.
    [/italic][/red] A bit premature here.

    Okay Zibadian, I've been at this all day (got the bumps on my head to prove it) and still cannot get it this stupid code to stop encrypting the spaces.

    Have you got a bigger clue, by any chance? PLEASE!!:-o
  • zibadianzibadian Posts: 6,349Member
    : [red][italic]Thank you very much for all the help.
    : [/italic][/red] A bit premature here.
    :
    : Okay Zibadian, I've been at this all day (got the bumps on my head to prove it) and still cannot get it this stupid code to stop encrypting the spaces.
    :
    : Have you got a bigger clue, by any chance? PLEASE!!:-o
    :
    The code itself is pretty simple. Here is the encryption routine, which will only encrypt the characters in the array:
    [code]
    var encryptedString = '';
    var charIndex = 0;

    for (var i = 0; i < aString.length; i++)
    {
    charIndex = -1; // Start out with a not-found value
    for (var j = 0; j < characterArray.length; j++)
    {
    if (aString[i] == characterArray[j])
    {
    charIndex = j;
    }
    }
    if (charIndex > -1) // Found a character in the characterArray
    {
    // Perform the encryption
    encryptedString = encryptedString + cipherArray[charIndex];
    } else {
    // Simply copy the character from the user input into the encrypted string
    encryptedString = encryptedString + aString[i];
    }
    };
    }
    [/code]

  • morphiosmorphios Posts: 16Member
    [b][red]This message was edited by morphios at 2004-8-19 3:17:21[/red][/b][hr]
    [red][italic]Thank you very much for [b]all[/b] the help.[/red][/italic]

    Its all working as it should now, I am eternally grateful to you.

    Morphios



Sign In or Register to comment.