Character Substitution and Permutation

Recently I came across the challenge to generate a list of all possible passwords for an overheard password with the permutation of character substitution.

The substitutions can be anything the user finds easy to remember, such as: ‘a’=’@’ or ‘A’ or ‘a’, ‘b’=’8’ or ‘B’ or ‘b’ , ‘k’=’|<‘ or ‘k’ or ‘K’ , ‘o’=’0’ or ‘O’ or ‘o’

With that the password “abook” can have following combinations.


Here is the solution in java.
github repository –

package password_character_substitution;

import java.util.ArrayList;
import java.util.HashMap;

public class Password_Character_Substitution {

    static HashMap<Character, String[]> mappings = new HashMap<Character, String[]>();

    public static void main(String[] args) {

        ArrayList passwords = generate_password_possibilities("abook");

        for(int i=0;i<passwords.size();i++) {
            System.out.print(passwords.get(i) + "\t\t");
            if(i%10==0) {
        System.out.println("Number of Password Combinations:" + passwords.size());

    static ArrayList generate_password_possibilities(String overheardpassword) {
        ArrayList password_list = new ArrayList();
        if (overheardpassword != null && overheardpassword.length()!=0) {
            generate_password_helper(password_list, "", overheardpassword);
        return password_list;

    static void generate_password_helper(ArrayList password_list, String password_prefix, String password_remaining) {
        char password_char = password_remaining.substring(0, 1).charAt(0);
        String[] replacements = mappings.get(Character.valueOf(password_char));

        if (password_remaining.length() == 1) {
            if (replacements != null) {
                for (int i = 0; i < replacements.length; i++) {
                    password_list.add(password_prefix + replacements[i]);
            } else {
                password_list.add(password_prefix + password_char);
        } else {

            if (replacements != null) {
                for (int i = 0; i < replacements.length; i++) {
                    generate_password_helper(password_list, password_prefix + replacements[i], password_remaining.substring(1));
            } else {
                generate_password_helper(password_list, password_prefix + password_char, password_remaining.substring(1));

    static void init_character_mapping() {
        mappings.put(Character.valueOf('a'), new String[]{"a", "A", "@"});
        mappings.put(Character.valueOf('b'), new String[]{"b", "B", "8","|3"});
        mappings.put(Character.valueOf('c'), new String[]{"c", "C"});
        mappings.put(Character.valueOf('d'), new String[]{"d", "D"});
        mappings.put(Character.valueOf('e'), new String[]{"e", "E", "3"});
        mappings.put(Character.valueOf('f'), new String[]{"f", "F"});
        mappings.put(Character.valueOf('g'), new String[]{"g", "G", "9", "6"});
        mappings.put(Character.valueOf('h'), new String[]{"h", "H"});
        mappings.put(Character.valueOf('i'), new String[]{"i", "I", "!", "1"});
        mappings.put(Character.valueOf('j'), new String[]{"j", "J"});
        mappings.put(Character.valueOf('k'), new String[]{"k", "K", "|<"});
        mappings.put(Character.valueOf('l'), new String[]{"l", "L"});
        mappings.put(Character.valueOf('m'), new String[]{"m", "M"});
        mappings.put(Character.valueOf('n'), new String[]{"n", "N"});
        mappings.put(Character.valueOf('o'), new String[]{"o", "O", "0"});
        mappings.put(Character.valueOf('p'), new String[]{"p", "P", "9"});
        mappings.put(Character.valueOf('q'), new String[]{"Q", "q"});
        mappings.put(Character.valueOf('r'), new String[]{"r", "R"});
        mappings.put(Character.valueOf('s'), new String[]{"s", "S", "$", "5"});
        mappings.put(Character.valueOf('t'), new String[]{"t", "T", "7"});
        mappings.put(Character.valueOf('u'), new String[]{"u", "U", "you"});
        mappings.put(Character.valueOf('v'), new String[]{"v", "V"});
        mappings.put(Character.valueOf('w'), new String[]{"w", "W"});
        mappings.put(Character.valueOf('x'), new String[]{"x", "X"});
        mappings.put(Character.valueOf('y'), new String[]{"y", "Y"});
        mappings.put(Character.valueOf('z'), new String[]{"z", "Z"});
        mappings.put(Character.valueOf(' '), new String[]{"?"});
        mappings.put(Character.valueOf('1'), new String[]{"1", "!"});
        mappings.put(Character.valueOf('2'), new String[]{"2", "Z"});
        mappings.put(Character.valueOf('3'), new String[]{"3", "E"});
        mappings.put(Character.valueOf('4'), new String[]{"4", "?"});
        mappings.put(Character.valueOf('5'), new String[]{"5", "S", "s", "$"});
        mappings.put(Character.valueOf('6'), new String[]{"6"});
        mappings.put(Character.valueOf('7'), new String[]{"7", "T"});
        mappings.put(Character.valueOf('8'), new String[]{"8", "B"});
        mappings.put(Character.valueOf('9'), new String[]{"9"});
        mappings.put(Character.valueOf('0'), new String[]{"0", "0", "O"});


Consuming PHP Zend Rest API in Java


In my last Weblog entry, I described the steps on developing Rest API in PHP using Zend Framework.

Now we have Rest API ready to be consumed by the client. Let consume it using Java Client.

Before beginning the coding part, we need to download following library.

The Jakarta Commons HttpClient 3.1 Library

Apache Commons Logging Library

Apache Commons Codec Library

First of all lets create the new NetBeans project. Select “java” from the project categories and “java Application” from the projects list. Click Next, name your project as “zend_rest_client” and finally click Finish button.



Right click on the Library and Select the Property. Add downloaded Library files using “Add Jar/Folder”.


Click “OK” and then replace the content of Main.Java file with the following code.

package zend_rest_client;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.w3c.dom.*;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
/* @author divyen patel */

public class Main {

    public static void main(String[] args) throws IOException, SAXException {
        String str_REST_SERVER = "http://localhost";
        String str_Port = "80"; //Default Apache Port
        String str_rest_API_page = "weather_rest.php";
        String str_rest_method = "get_temperature";
        String str_rest_method_argument = "zipcode=95035";
        String str_rest_url = str_REST_SERVER + ":" + str_Port + "/" + str_rest_API_page + "?method=" + str_rest_method + "&amp;" + str_rest_method_argument;
        System.out.println("Request URL:" + str_rest_url + "\n");
// Send GET request
        HttpClient client = new HttpClient();
        GetMethod method = new GetMethod(str_rest_url);
        int statusCode = client.executeMethod(method);
        InputStream rstream = null;
        rstream = method.getResponseBodyAsStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(rstream));
        String line;
        String xmlString = "";
        while ((line = br.readLine()) != null) {
            xmlString = xmlString + line;
        System.out.println("Request Response:" + xmlString + "\n");
//Parsing XML Response
        DOMParser parser = new DOMParser();
        parser.parse(new InputSource(new StringReader(xmlString)));
        Document document = parser.getDocument();
        System.out.println("Parsing XML Response");
        if (document.getElementsByTagName("status").item(0).getTextContent().equals("passed")) {
            System.out.println("Status: Passed");
            System.out.println("zipcode:" + document.getElementsByTagName("zipcode").item(0).getTextContent());
            System.out.println("tempHigh:" + document.getElementsByTagName("tempHigh").item(0).getTextContent());
            System.out.println("templow:" + document.getElementsByTagName("templow").item(0).getTextContent());
        } else {
            System.out.println("Status: Failed");

Finally right click on the project and Build it. You may see some warnings stating “DOMParser is Sun proprietary API and may be removed in a future release”. You can ignore these warnings.

Run the project and see the output in the IDE


You can also test it in the terminal console, using “java -jar <location of the jar file>”


If you have any better solution for consuming REST API in Java, please share with me. I am egar to learn it.