user1644257
user1644257

Reputation: 11

How can i change a string into the ascii values and back into a string?

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.");
     }
  }
}

Upvotes: 0

Views: 4398

Answers (2)

Maarten Bodewes
Maarten Bodewes

Reputation: 93948

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);
}

Upvotes: 1

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382092

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).

Upvotes: 2

Related Questions