Thread: Vigenere Decipher/Encipher

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    3

    Angry Vigenere Decipher/Encipher

    I am new here and a very novice. I want to learn something from you. Here my codes won't work . any idea or suggestion? any help would be appreciated.

    void vigenere(operation){
    var keyText = trimString(document.forms[0].keyfield.value);
    if (keyText == ""){
    return;
    }
    for (i = 0; i < keyText.length; i++){
    letter = keyText.charAt(i).toLowerCase();
    if ("abcdefghijklmnopqrstuvwxyz".indexOf(letter) == -1){
    alert("Your key may contain only letters.");
    return;
    }
    }
    if (operation == "encipher"){
    var inputText = document.forms[0].plainfield.value;
    outputText = vencipher(inputText,keyText);
    document.forms[0].cipherfield.value = outputText;
    }
    else if (operation == "decipher"){
    var inputText = document.forms[0].cipherfield.value;
    outputText = vdecipher(inputText,keyText);
    document.forms[0].plainfield.value = outputText;
    }
    else{
    alert("Error");
    }
    }

    void vencipher(inputText,keyText){
    var letterOffset;
    var inputNumber;
    var outputNumber;
    var changeNumber;
    var iKey = 0;
    var outputChar = "";
    var outputText = "";
    var lowerAlpha = "abcdefghijklmnopqrstuvwxyz";
    var upperAlpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    keyText = keyText.toLowerCase();
    for (i = 0; i < inputText.length; i++){
    inputChar = inputText.charAt(i);
    if (upperAlpha.indexOf(inputChar) != -1){
    var alphabet = upperAlpha;
    }
    else if (lowerAlpha.indexOf(inputChar) != -1){
    var alphabet = lowerAlpha;
    }
    else {
    outputText += inputChar;
    continue;
    }
    inputNumber = alphabet.indexOf(inputChar);
    changeNumber = lowerAlpha.indexOf(keyText.charAt(iKey));
    outputNumber = inputNumber + changeNumber;
    outputNumber -= (outputNumber >= 26) ? 26 : 0;
    outputChar = alphabet.charAt(outputNumber);
    outputText += outputChar;
    iKey++;
    iKey = (iKey >= keyText.length) ? 0 : iKey;
    }
    return outputText;
    }

    void vdecipher(inputText,keyText){
    var letterOffset;
    var inputNumber;
    var outputNumber;
    var changeNumber;
    var iKey = 0;
    var outputChar = "";
    var outputText = "";
    var lowerAlpha = "abcdefghijklmnopqrstuvwxyz";
    var upperAlpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    keyText = keyText.toLowerCase();
    for (i = 0; i < inputText.length; i++){
    inputChar = inputText.charAt(i);
    if (upperAlpha.indexOf(inputChar) != -1){
    var alphabet = upperAlpha;
    }
    else if (lowerAlpha.indexOf(inputChar) != -1){
    var alphabet = lowerAlpha;
    }
    else {
    outputText += inputChar;
    continue;
    }
    inputNumber = alphabet.indexOf(inputChar);
    changeNumber = lowerAlpha.indexOf(keyText.charAt(iKey));
    outputNumber = inputNumber - changeNumber;
    outputNumber += (outputNumber < 0) ? 26 : 0;
    outputChar = alphabet.charAt(outputNumber);
    outputText += outputChar;
    iKey++;
    iKey = (iKey >= keyText.length) ? 0 : iKey;
    }
    return outputText;
    }

    void clearForm(){
    document.forms[0].plainfield.value = "";
    document.forms[0].cipherfield.value = "";
    }

    void trimString(inputString) {
    var outputString = "";
    var front = 0;
    while ((front < inputString.length) && (inputString.charAt(front) == ' ')){
    ++front;
    }
    var back = inputString.length;
    while ((back > 0) && (inputString.charAt(back - 1) == ' ')){
    --back;
    }
    outputString = inputString.substring(front, back);
    return outputString;
    }

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    Let me guess... You used to program in Java, right? Pretty much the whole program is messed up. You don't have a main function, you declare variables wrong, you use Java (charAt is Java, not C), and you appear to make up your own language in places. My advice: Put this program away for awhile (Maybe a month or so), grab a book, and relearn C++ from the ground up (hmm... that'd make a good book title).

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    3
    how can i change to C? not Java. thanks in advance.

  4. #4
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    You're going to have to rewrite all of the code. Well, to be fair, not all of it. About 90% of it. Look on the web for tutorials on C, and you'll see what I'm talking about.

  5. #5
    Fingerstyle Guitarist taylorguitarman's Avatar
    Join Date
    Aug 2001
    Posts
    564
    Looks like JavaScript to me, not Java. There is a large difference for those that don't know. Anyways, as Govtcheez said, you're going to have to rewrite it. Get a good book on C++ or Java and write it according to the way that language does it. I think in this case Java would be an easier translation but you're going to have to learn a new language either way. You get to choose
    If a tree falls in the forest, and no one is around to see it, do the other trees make fun of it?

  6. #6
    5|-|1+|-|34|) ober's Avatar
    Join Date
    Aug 2001
    Posts
    4,429
    I think that's a combination of Java and Javascript... see above for advice...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vigenere Cipher decryption help needed
    By magic101 in forum C Programming
    Replies: 2
    Last Post: 02-07-2009, 07:25 PM
  2. Crashing Vigenere Cipher program
    By ultrabot90 in forum C++ Programming
    Replies: 15
    Last Post: 09-21-2007, 05:41 PM
  3. Vigenere Encryption
    By vasanth in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2002, 02:53 PM
  4. Vigenere
    By Xander in forum C++ Programming
    Replies: 1
    Last Post: 02-15-2002, 01:34 AM