This class is an implementation of the ROT-N algorithm. : ROT « Security « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Security » ROT 




This class is an implementation of the ROT-N algorithm.
   
/*
 * Copyright WizTools.org
 * Licensed under the Apache License, Version 2.0:
 * http://www.apache.org/licenses/LICENSE-2.0
 */

//package org.wiztools.commons;

import java.util.HashMap;
import java.util.Map;

/**
 * This class is an implementation of the ROT-N algorithm. The algorithm
 * implemented in this class dynamically creates the lookup table.
 @author subhash
 */
public final class RotN {
    private RotN() {}

    private static final char[] lookup = "abcdefghijklmnopqrstuvwxyz".toCharArray();

    private static interface Emitter {
        void emit(char c1, char c2);
    }

    private static void map(final int n, final Emitter emitter) {
        if(n < || n > 25)
            throw new IllegalArgumentException("Value of N should be between 1 and 26");

        for(int i=0, j=n; i<lookup.length; i++,j++) {
            if(j == lookup.length)
                j = 0;
            emitter.emit(lookup[i], lookup[j]);
        }
    }

    private static Map<Character, Character> getCipherMap(final int n) {
        final Map<Character, Character> out = new HashMap<Character, Character>();
        Emitter emitter = new Emitter() {
            @Override
            public void emit(char c1, char c2) {
                out.put(c1, c2);
            }
        };
        map(n, emitter);
        return out;
    }

    private static Map<Character, Character> getDeCipherMap(final int n) {
        final Map<Character, Character> out = new HashMap<Character, Character>();
        Emitter emitter = new Emitter() {
            @Override
            public void emit(char c1, char c2) {
                out.put(c2, c1);
            }
        };
        map(n, emitter);
        return out;
    }

    private static String process(final String inString,
            final Map<Character, Character> mapthrows IllegalArgumentException {
        char[] arr = inString.toCharArray();
        StringBuilder sb = new StringBuilder(arr.length);
        for(char c: arr) {
            Character out = map.get(c);
            if(out == null) {
                sb.append(c);
            }
            else {
                if(Character.isUpperCase(c))
                    sb.append(Character.toUpperCase(out));
                else
                    sb.append(out);
            }
        }
        return sb.toString();
    }

    /**
     * To cipher a String using ROT-N algorithm.
     @param n The value of N in ROT-N.
     @param inString The input string.
     @return The ciphered value.
     @throws IllegalArgumentException When n is not within range.
     */
    public static String cipher(final int n, final String inStringthrows IllegalArgumentException {
        return process(inString, getCipherMap(n));
    }

    /**
     * To decipher a String using ROT-N algorithm.
     @param n The value of N in ROT-N.
     @param inString The input string.
     @return The deciphered value.
     @throws IllegalArgumentException When n is not within range.
     */
    public static String deCipher(final int n, final String inStringthrows IllegalArgumentException {
        return process(inString, getDeCipherMap(n));
    }
}
---------------
/*
 * Copyright WizTools.org
 * Licensed under the Apache License, Version 2.0:
 * http://www.apache.org/licenses/LICENSE-2.0
 */

package org.wiztools.commons;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 @author subhash
 */
public class RotNTest {

    public RotNTest() {
    }

    @BeforeClass
    public static void setUpClass() throws Exception {
    }

    @AfterClass
    public static void tearDownClass() throws Exception {
    }

    @Before
    public void setUp() {
    }

    @After
    public void tearDown() {
    }

    /**
     * Test of cipher method, of class RotN.
     */
    @Test
    public void testCipher() {
        System.out.println("cipher");
        int n = 13;
        String inString = "subhash-101-aarthi";
        String expResult = Rot13.cipher(inString);
        String result = RotN.cipher(n, inString);
        assertEquals(expResult, result);
     }

    /**
     * Test of deCipher method, of class RotN.
     */
    @Test
    public void testDeCipher() {
        System.out.println("deCipher");
        int n = 13;
        String inString = Rot13.cipher("subhash-101-aarthi");
        String expResult = "subhash-101-aarthi";
        String result = RotN.deCipher(n, inString);
        assertEquals(expResult, result);
     }
}

   
    
    
  














Related examples in the same category
1.An implementation of the ROT-13 algorithm.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.