0

I'm trying to make an simple encryption program that converts a string into the ASCII value equivalent and then decrypts it into the string again or char.

import java.io.*; 
import javax.swing.*;

public class SimpleEncryption {
   public static void main (String [] args) throws Exception
  {
     BufferedReader inKb = new BufferedReader (new InputStreamReader (System.in));

     for(int i=0; i<10; i++)
     {
        String ans = JOptionPane.showInputDialog ("Hello User, would you like to encrypt or decrypt?");
        ans = ans.toUpperCase();        
        int a = 0;

        if (ans.contains("EN")||ans.contains("ENCRYPT"))
        {
           String pass = "";
           pass = JOptionPane.showInputDialog ("Please type your phrase into input:");          

           for (int j=0; j<pass.length(); j++)
           {
              char c = pass.charAt(j);
              a = (int) c;
              System.out.print(a);
           }
           break;
        }


        if (ans.contains("DE")||ans.contains("DECRYPT"))
        {
           String pass = "";
           pass = JOptionPane.showInputDialog ("Please type the encrypted code into input:");          

           for (int k=0; k<pass.length(); k++)
           {
              char c = pass.charAt(k);
              a = (int)(c);
              i = (char) a;
              System.out.print(a);
           }
           break;
        }

        System.out.println("Sorry I don't understand, please retry.");
     }
  }
}
6
  • 6
    You've said what you're trying to do, and given some code... but no actual question. (Note that the char to int conversion will give you the Unicode value of a UTF-16 code unit... think beyond ASCII.) Commented Sep 3, 2012 at 16:57
  • How can i change a string given by a user into a ASCII value (encrypt) and then take the values (eg. hello = 104101108108111) and change them into their characters(decrypt)? Commented Sep 3, 2012 at 17:01
  • I don't see how it's called "encryption". Is that a first step before actual encryption ? Commented Sep 3, 2012 at 17:04
  • Yes, all i need is to change the string into the ASCII values (eg. h = 104) and then change them back to a string. Commented Sep 3, 2012 at 17:07
  • I dont want want to make it too complicated as you can see (UTF-16) so cant i just use an Array to store each ASCII value and then convert the number back into a char like: char ch = (char)(65) ? Commented Sep 3, 2012 at 17:12

2 Answers 2

2

If what you're trying to have is in fact an encoding (before encryption ?) in ASCII of any java String (UTF-16 based), you might encode it in base64 : this encoding scheme was created just for that.

It's really easy to do this encoding/decoding in java (as in other languages).

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot man, i was really stressing to get this done. I will try to find out how to use base64 now. Thanks again
The base64 string is just a string with only a reduced set of characters (thus the string is longer by about 30%). You can use as any string, but without the need to escape it for most uses.
If I read your question and code right dystroy, this is not what you want.
1

What you seem to want is to get the byte array representing some kind of character encoding (not encryption) of the input string. Then you seem to want to show the octal value of the encoded characters. If you just need US ASCII then you would get all (printable) characters up to 177 octal. If you want special characters you need to choose a more specific character set (IBM OEM or Western-Latin are common ones). UTF-8 could also be used, but it may encode a single character into multiple bytes.

public static String toOctalString(final byte[] encoding) {
    final StringBuilder sb = new StringBuilder(encoding.length * 4);
    for (int i = 0; i < encoding.length; i++) {
        if (i != 0) {
            sb.append("|");
        }
        sb.append(Integer.toOctalString(encoding[i] & 0xFF));
    }
    return sb.toString();
}

public static byte[] fromOctalString(final String octalString) {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream(octalString.length() / 4 + 1);
    final Matcher m = Pattern.compile("[0-7]{1,3}").matcher(octalString);
    while (m.find()) {
        baos.write(Integer.parseInt(m.group(), 8));
    }
    return baos.toByteArray();
}

public static void main(String[] args) {
    final String userInput = "owlstæd";
    // use the common Latin-1 encoding, standardized in ISO 8859 as character set 1
    // you can replace with ASCII, but the ASCII characters will encode fine for both
    final byte[] userInputEncoded = userInput.getBytes(Charset.forName("ISO8859-1"));
    final String octalString = toOctalString(userInputEncoded); 
    System.out.println(octalString);

    final byte[] userInputEncoded2 = fromOctalString(octalString);
    final String userInput2 = new String(userInputEncoded2, Charset.forName("ISO8859-1"));
    System.out.println(userInput2);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.