Results 1 to 6 of 6
  1. #1
    ssk2659 is offline Member
    Join Date
    Jan 2015
    Posts
    48
    Rep Power
    0

    Default Encryption methods

    My class includes these two methods which encrypt a String according to Caesar Cipher method (where a letter moves according to the given SHIFT KEY). Here in my code, there are two methods: encrypt(String s) and encrypt1(char c). A message is passed to encrypt(String s) method and it extracts every character, passes it and at the same time calls encrypt1(char c) which encrypts character by character. encrypt() method then returns an encrypted message (encrypt1() has some methods such as rotate(index, shift), indexOf() - they all work as they passed every test cases). Could somebody help me please why am I getting wrong output (I'm passing test cases)?

    For example in the following test cases:

    Java Code:
    @Test (timeout=2000)
    public void test_Symmetric_16() {
        assertEquals("hello rowld", swap.encrypt("hello world"));
    }
    
    @Test (timeout=2000)
    public void test_Symmetric_17() {
        assertEquals("rwRW", swap.encrypt("wrWR"));
    }
    In the first test case the encrypt method is returning only 'd' while the second returns only 'R'`.

    Java Code:
    public String encrypt(String s) {
        String string = "";
        char c = 0;
        char encrypted = 0;
        for (int i = 0; i < s.length(); i++) {
            c = s.charAt(i);
            encrypted = encrypt1(c);
            string += "" + encrypted;
        }
        return string;
    }
    
    public char encrypt1(char c) {
        int index = 0;
        int rotateIndex = 0;
        for (int i = 0; i < alphabet.length(); i++) {
            if (c == alphabet.get(i)) {
                index = alphabet.indexOf(c) + shift;
                if((index > alphabet.length()) || (index < alphabet.length())) {
                    rotateIndex = rotate(index, shift);
                    //c = alphabet.get(rotateIndex);
                }
                c = alphabet.get(rotateIndex);
            } else {
                throw new NotInAlphabetException(c, alphabet);  
            }
        }
        return c;
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Encryption methods

    why am I getting wrong output
    Can you post the program's output and add some comments saying what it should be?

    Please post a complete program that will compile, execute and show the problem.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Juan2114 is offline Member
    Join Date
    Feb 2015
    Posts
    5
    Rep Power
    0

    Default Re: Encryption methods

    #9990018 - Pastie
    Something like that should be good enough.

    Java Code:
    /**
     * Created with eclipse 28/02/2015 1:57:10 p. m.
     * @Author Juan Sebastian Quiceno <Juan.2114@hotmail.com>
     */
    public class CesarCipher {
    
    	/**
    	 * Represents an empty, non existing character
    	 */
    	private static final char EMPTY_CHAR = '\u0000';
    	
    	/**
    	 * Represents the alphabet
    	 */
    	private static final char [] ALPHABET = "_ ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
    	
    	public static void main(String[] args) {
    		final char [] encrypted = encrypt("hello world", 2);
    		final char [] decrypted = decrypt("JGNNQBYQTNF", 2);
    		System.out.println("Encrypted: " + String.copyValueOf(encrypted) + "; Decrypted: " + String.copyValueOf(decrypted));
    	}
    	
    	/**
    	 * Attempts to encrypt a piece of word 
    	 * @param word	the word to encrypt
    	 * @param offset	the offset, that is used for word separation
    	 * @return	the char array
    	 */
    	public static char [] decrypt(String word, final int offset) {
    		return perfom(word, offset, false);
    	}
    	
    	/**
    	 * Attempts to decrypts a piece of word 
    	 * @param word	the word to encrypt
    	 * @param offset	the offset, that is used for word separation
    	 * @return	the char array
    	 */
    	public static char[] encrypt(String word, final int offset) {
    		return perfom(word, offset, true);
    	}
    	
    	/**
    	 * Performs an encryption / decryption operation
    	 * @param word	the word to modify
    	 * @param offset	the offset for the operation
    	 * @param encrypt	are we encrypting or decrypting
    	 * @return
    	 */
    	public static char [] perfom(String word, final int offset, final boolean encrypt) {
    		word = word.toUpperCase();
    		final char [] array = word.toCharArray();
    		final char [] result = new char[array.length];
    		int count = 0;
    		for (char c : array) {
    			result[count ++] = switchLetter(c, offset, encrypt);
    		}
    		return result;
    	}
    	
    	/**
    	 * Switch the character by a new character
    	 * @param key	the default character as a reference
    	 * @param offset	the offset to switch it by
    	 * @param add	should we increment the key by the offset, or decrement
    	 * @return
    	 */
    	private static char switchLetter(final char key, int offset, final boolean add) {
    		final int index = indexOf(key) + (add ? offset : -offset);
    		if (index >= ALPHABET.length) 
    			return EMPTY_CHAR;
    		
    		return ALPHABET[index];
    	}
    	
    	/**
    	 * Attempts to grab the index of the key by the developer designed alphabet
    	 * @param key	the key
    	 * @return	the index of, in the alphabet
    	 */
    	private static final int indexOf(final char key) {
    		int count = 0;
    		for (char c : ALPHABET) {
    			if (c == key) {
    				break;
    			}
    			count ++;
    		}
    		return count;
    	}
    }
    Edit: You can add support for non caps easily.
    Last edited by Juan2114; 03-01-2015 at 12:47 AM.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,716
    Rep Power
    18

    Default Re: Encryption methods

    @Juan2114: The OP's question was "Could somebody help me please why am I getting wrong output (I'm passing test cases)?" and the code you posted doesn't even begin to address that.

  5. #5
    Juan2114 is offline Member
    Join Date
    Feb 2015
    Posts
    5
    Rep Power
    0

    Default Re: Encryption methods

    Im sorry but if he haven't supplied his code that's the best that i can do.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Encryption methods

    Quote Originally Posted by Juan2114 View Post
    Im sorry but if he haven't supplied his code that's the best that i can do.
    First, don't include links to unknown sites. They will probably be removed by a moderator. Second, if you want to show code, then paste it between [CODE][/CODE] tags. And only do it to demonstrate a concept, not spoonfeed solutions. You should also review the Forum Rules before posting.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. Encryption
    By stegano in forum New To Java
    Replies: 0
    Last Post: 03-01-2011, 04:13 PM
  3. How to encryption?
    By JavaCy in forum Advanced Java
    Replies: 1
    Last Post: 09-26-2010, 08:49 PM
  4. Aes Encryption
    By rohitpatidar3 in forum Advanced Java
    Replies: 7
    Last Post: 04-12-2010, 04:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •