How do you convert from the ASCII value to string - java

I'm stuck with some code here and what I'm trying to do is convert a string into its ASCII value, subtract 30 from it and then convert back to a string.
E.g. Enter - hello
Convert to - 104 101 108 108 111
Subtract - 74 71 78 78 81
display - JGNNQ
Code:
import javax.swing.*;
public class practice {
public static void main (String[] args) {
String enc = "";
String encmsg = "";
String msg = JOptionPane.showInputDialog("Enter your message");
int len = msg.length();
for (int i = 0; i< len ; i++) {
char cur = msg.charAt(i);
int val = (int) cur;
val = val -32;
enc = "" + val;
encmsg = encmsg + enc;
}
JOptionPane.showMessageDialog(null, encmsg);
}
}
Thanks in advance

Couple things:
Change val = val -32; to val = val -30; to get the proper subtraction you want in the original problem statement.
Next, change
enc = "" + val; to enc = (char)val;
so that you can convert the value to a proper character. Before, you were just concatenating it to a string, which won't do any conversion. You also need to declare enc as a char at the top of your file.
The full working code should be as follows:
char enc;
String encmsg = "";
String msg = JOptionPane.showInputDialog("Enter your message");
int len = msg.length();
for (int i = 0; i < len; i++) {
char cur = msg.charAt(i);
int val = (int) cur;
val = val - 30;
enc = (char) val;
encmsg = encmsg + enc;
}
JOptionPane.showMessageDialog(null, encmsg);

Related

Convert Mac Address to Integer

Im trying convert mac adress to integer:
Result is: 2321591092112814
It should be: 255771439995918
Im trying:
String[] macAddressParts = device.getAddress().split(":");
Byte[] macAddressBytes = new Byte[6];
String macAddressString = "";
for(int i=0; i<6; i++){
Integer hex = Integer.parseInt(macAddressParts[i], 16);
macAddressString += hex.toString();
}
System.out.println(macAddressString);
// 2321591092112814
You cannot make an integer by catenating strings.
Try that :
String[] macAddressParts = "e8:9f:6d:d3:1c:0e".split(":");
Byte[] macAddressBytes = new Byte[6];
long addressAsInteger = 0;
for (int i = 0; i < 6; i++) {
Integer hex = Integer.parseInt(macAddressParts[i], 16);
addressAsInteger = addressAsInteger * 256 + hex;
}
System.out.println("Addresse as an integer : " + addressAsInteger);
It gives the right answer : 255771439995918.

How to remove letter in space for One Time Pad cipher?

I have found this code somewhere on the web for reference yet this bothers me. The result is correct but somehow there is something wrong with the process.
import java.io.*;
import java.util.*;
import java.util.Scanner;
public class onetime{
public static void main(String[] args){
System.out.println("Type Message: " );
Scanner sc = new Scanner(System.in);
String text = sc.nextLine();
String key = RandomAlpha(text.length());
String enc = OTPEncryption(text,key);
System.out.println("Plaintext : " +text);
System.out.println("Key: "+key);
System.out.println("Encrypted : "+enc);
System.out.println("Decrypted : "+OTPDecryption(enc,key));
}
public static String RandomAlpha(int len){
Random r = new Random();
String key = "";
for(int x=0;x<len;x++)
key = key + (char) (r.nextInt(26) + 'A');
return key;
}
public static String OTPEncryption(String text,String key){
String alphaU = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String alphaL = "abcdefghijklmnopqrstuvwxyz";
int len = text.length();
String sb = "";
for(int x=0;x<len;x++){
char get = text.charAt(x);
char keyget = key.charAt(x);
if(Character.isUpperCase(get)){
int index = alphaU.indexOf(get);
int keydex = alphaU.indexOf(Character.toUpperCase(keyget));
int total = (index + keydex) % 26;
sb = sb+ alphaU.charAt(total);
}
else if(Character.isLowerCase(get)){
int index = alphaL.indexOf(get);
int keydex = alphaU.indexOf(Character.toLowerCase(keyget));
int total = (index + keydex) % 26;
sb = sb+ alphaL.charAt(total);
}
else{
sb = sb + get;
}
}
return sb;
}
public static String OTPDecryption(String text,String key){
String alphaU = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String alphaL = "abcdefghijklmnopqrstuvwxyz";
int len = text.length();
String sb = "";
for(int x=0;x<len;x++){
char get = text.charAt(x);
char keyget = key.charAt(x);
if(Character.isUpperCase(get)){
int index = alphaU.indexOf(get);
int keydex = alphaU.indexOf(Character.toUpperCase(keyget));
int total = (index - keydex) % 26;
total = (total<0)? total + 26 : total;
sb = sb+ alphaU.charAt(total);
}
else if(Character.isLowerCase(get)){
int index = alphaL.indexOf(get);
int keydex = alphaU.indexOf(Character.toLowerCase(keyget));
int total = (index - keydex) % 26;
total = (total<0)? total + 26 : total;
sb = sb+ alphaL.charAt(total);
}
else{
sb = sb + get;
}
}
return sb;
}
}
Output
Plain Text: help me
Key: QMMNQAB
Encrypted: gdko ld
Decrypt: help me
so as you can here in the output the second 'Q' of the key isn't supposed to be there because it's supposed to be a space. How can I remove it?
Your answer is highly appreciated thank you :)

Jaunt Java getText() returning correct text but with lots of "?"

The title explains all, also, I have tried removing them
(because the text is there, but instead of "aldo" there is "al?do", also it seems to have a random pattern)
with (String).replace("?", ""), but with no success.
I have also used this, with a combination of UTF_8,UTF_16 and ISO-8859, with no success.
byte[] ptext = tempName.getBytes(UTF_8);
String tempName1 = new String(ptext, UTF_16);
An example of what I am getting:
Studded Regular Sweatshirt // Instead of this
S?tudde?d R?eg?ular? Sw?eats?h?irt // I get this
Could it be the website that notices the headless browser and tries to "spoof" its content? How can I overcome this?
It looks very likely that site you scrapping intent mix up the 3f and 64 characters into your result.
so you have to mask your self as a normal browser to scrapping or filter it out by replacing.
text simple
Sca???rfa???ce??? E???mbr???oi�d???ered L�e???athe
after filteration
Scarface Embroidered Leather
//Sca???rfa???ce??? E???mbr???oi�d???ered L�e???athe
//Scarface Embroidered Leathe
String hex="5363613f3f3f7266613f3f3f63653f3f3f20453f3f3f6d62723f3f3f6f69‌​643f3f3f65726564204c‌​653f3f3f61746865";
byte[] bytes= hexStringToBytes(hex);
//the only line you need
String res = new String(bytes,"UTF-8").replaceAll("\\\u003f","").replaceAll('�',"").replaceAll("�","");
private static byte charToByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(new String(c));
}
public static byte[] hexStringToBytes(String hexString) {
if (hexString == null || hexString.equals("")) {
return null;
}
hexString = hexString.toUpperCase();
int length = hexString.length() / 2;
char[] hexChars = hexString.toCharArray();
byte[] d = new byte[length];
for (int i = 0; i < length; i++) {
int pos = i * 2;
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
}
return d;
}
public static String bytesToHexString(byte[] src){
StringBuilder stringBuilder = new StringBuilder("");
if (src == null || src.length <= 0) {
return null;
}
for (int i = 0; i < src.length; i++) {
int v = src[i] & 0xFF;
String hv = Integer.toHexString(v);
if (hv.length() < 2) {
stringBuilder.append(0);
}
stringBuilder.append(hv);
}
return stringBuilder.toString();
}
public String printHexString( byte[] b) {
String a = "";
for (int i = 0; i < b.length; i++) {
String hex = Integer.toHexString(b[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
a = a+hex;
}
return a;
}

base64 decoding to UTF-8, one character not displaying correctly

I am trying to decode a string from base64 to UTF-8 for an assignment.
Not having programmed Java for a while I am probably not using the most efficient method, however I managed to implement a function working 99% correctly.
Decoding the example string in Base64: VGhpcyBpcyBhbiBBcnhhbiBzYW1wbGUgc3RyaW5nIHRoYXQgc2hvdWxkIGJlIGVhc2lseSBkZWNvZGVkIGZyb20gYmFzZTY0LiAgSXQgaW5jbHVkZXMgYSBudW1iZXIgb2YgVVRGOCBjaGFyYWN0ZXJzIHN1Y2ggYXMgdGhlIPEsIOksIOgsIOcgYW5kICYjOTYwOyBjaGFyYWN0ZXJzLg==
Results in:
This is an Arxan sample string that should be easily decoded from base64. It includes a number of UTF8 characters such as the ñ, é, è, ç and &#960 characters.
However, in the place of the &#960 should be the π symbol being outputted.
Note that I removed the ; after &#960 in here as it seems Stackoverflow automatically corrected it to π
I have tried many things such as creating a byte array and printing that, but still not working.
I am using Eclipse, can it be that just the output there displays incorrectly?
Does somebody have a suggestion to get this to work?
Thanks,
Vincent
Here is my code:
package base64;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
public class Base64 {
public static void main(String[] args) {
//Input strings
String base64 = "VGhpcyBpcyBhbiBBcnhhbiBzYW1wbGUgc3RyaW5nIHRoYXQgc2hvdWxkIGJlIGVhc2lseSBkZWNvZGVkIGZyb20gYmFzZTY0LiAgSXQgaW5jbHVkZXMgYSBudW1iZXIgb2YgVVRGOCBjaGFyYWN0ZXJzIHN1Y2ggYXMgdGhlIPEsIOksIOgsIOcgYW5kICYjOTYwOyBjaGFyYWN0ZXJzLg==";
//String base64 = "YW55IGNhcm5hbCBwbGVhc3U=";
String utf8 = "any carnal pleas";
//Base64 to UTF8
System.out.println("Base64 conversion to UTF8");
System.out.println("-------------------------");
System.out.println("Input base64-string: " + base64);
System.out.println("Output UTF8-string: " + stringFromBase64(base64));
System.out.println();
//UTF8 to Base64
System.out.println("UTF8 conversion to base64");
System.out.println("-------------------------");
System.out.println("Input UTF8-string: " + utf8);
System.out.println("Output base64-string: " + stringToBase64(utf8));
System.out.println();
System.out.println("Pi is π");
}
public static String stringFromBase64(String base64) {
StringBuilder binary = new StringBuilder();
int countPadding = countPadding(base64); //count number of padding symbols in source string
//System.out.println("No of *=* in the input is : " + countPadding);
//System.out.println(base64);
for(int i=0; i<(base64.length()-countPadding); i++)
{
int base64Value = fromBase64(String.valueOf(base64.charAt(i))); //convert Base64 character to Int
String base64Binary = Integer.toBinaryString(base64Value); //convert Int to Binary string
StringBuilder base64BinaryCopy = new StringBuilder(); //debugging
if (base64Binary.length()<6) //adds required zeros to make 6 bit string
{
for (int j=base64Binary.length();j<6;j++){
binary.append("0");
base64BinaryCopy.append("0"); //debugging
}
base64BinaryCopy.append(base64Binary); // debugging
} else // debugging
{
base64BinaryCopy.append(base64Binary); //debugging
} // debugging
//System.out.println(base64.charAt(i) + " = " + base64Value + " = " + base64BinaryCopy); //debugging
binary.append(base64Binary);
}
//System.out.println(binary);
//System.out.println(binary.length());
StringBuilder utf8String = new StringBuilder();
for (int bytenum=0;bytenum<(binary.length()/8);bytenum++) //parse string Byte-by-Byte
{
StringBuilder utf8Bit = new StringBuilder();
for (int bitnum=0;bitnum<8;bitnum++){
utf8Bit.append(binary.charAt(bitnum+(bytenum*8)));
}
char utf8Char = (char) Integer.parseInt(utf8Bit.toString(), 2); //Byte to utf8 char
utf8String.append(String.valueOf(utf8Char)); //utf8 char to string and append to final utf8-string
//System.out.println(utf8Bit + " = " + Integer.parseInt(utf8Bit.toString(), 2) + " = " + utf8Char + " = " + utf8String); //debugging
}
return utf8String.toString();
}
public static String stringToBase64(String utf8) {
StringBuilder binary = new StringBuilder();
String paddingString = "";
String paddingSymbols = "";
for(int i=0; i<(utf8.length()); i++)
{
int utf8Value = utf8.charAt(i); //convert utf8 character to Int
String utf8Binary = Integer.toBinaryString(utf8Value); //convert Int to Binary string
StringBuilder utf8BinaryCopy = new StringBuilder(); //debugging
if (utf8Binary.length()<8) //adds required zeros to make 8 bit string
{
for (int j=utf8Binary.length();j<8;j++){
binary.append("0");
utf8BinaryCopy.append("0"); //debugging
}
utf8BinaryCopy.append(utf8Binary); // debugging
} else // debugging
{
utf8BinaryCopy.append(utf8Binary); //debugging
} // debugging
//System.out.println(utf8.charAt(i) + " = " + utf8Value + " = " + utf8BinaryCopy);
binary.append(utf8Binary);
}
if ((binary.length() % 6) == 2) {
paddingString = "0000"; //add 4 padding zeroes
paddingSymbols = "==";
} else if ((binary.length() % 6) == 4) {
paddingString = "00"; //add 2 padding zeroes
paddingSymbols = "=";
}
binary.append(paddingString); //add padding zeroes
//System.out.println(binary);
//System.out.println(binary.length());
StringBuilder base64String = new StringBuilder();
for (int bytenum=0;bytenum<(binary.length()/6);bytenum++) //parse string Byte-by-Byte per 6 bits
{
StringBuilder base64Bit = new StringBuilder();
for (int bitnum=0;bitnum<6;bitnum++){
base64Bit.append(binary.charAt(bitnum+(bytenum*6)));
}
int base64Int = Integer.parseInt(base64Bit.toString(), 2); //Byte to Int
char base64Char = toBase64(base64Int); //Int to Base64 char
base64String.append(String.valueOf(base64Char)); //base64 char to string and append to final Base64-string
//System.out.println(base64Bit + " = " + base64Int + " = " + base64Char + " = " + base64String); //debugging
}
base64String.append(paddingSymbols); //add padding ==
return base64String.toString();
}
public static char toBase64(int a) { //converts integer to corresponding base64 char
String charBase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
//charBase64 = new char[]{'A','B','C','D','E','F','G','H','I','J','K','L','M','N'};
return charBase64.charAt(a);
}
public static int fromBase64(String x) { //converts base64 string to corresponding integer
String charBase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
return charBase64.indexOf(x);
}
public static int countPadding(String countPadding) { //counts the number of padding symbols in base64 input string
int index = countPadding.indexOf("=");
int count = 0;
while (index != -1) {
count++;
countPadding = countPadding.substring(index + 1);
index = countPadding.indexOf("=");
}
return count;
}
}
UTF8 is a character encoding that transforms a given char to 1, 2 or more bytes. Your code assumes that each byte should be transformed to a character. That works fine for ASCII characters such as a, b, c that are indeed transformed to a single byte by UTF8, but it doesn't work for characters like PI, which are transformed to a multi-byte sequence.
Your algorithm is awfully inefficient, and I would just ditch it and use a ready-to-use ecnoder/decoder. The JDK 8 comes with one. Guava and commons-codec also do. Your code should be as simple as
String base64EncodedByteArray = "....";
byte[] decodedByteArray = decoder.decode(base64EncodedByteArray);
String asString = new String(decodedByteArray, StandardCharSets.UTF_8);
or, for the other direction:
String someString = "VGhpcyBpcyBhb...";
byte[] asByteArray = someString.getBytes(StandardCharSets.UTF_8);
String base64EncodedByteArray = encoder.encode(asBytArray);

JAVA to Perl - port XOR encryptor class

I have the folowing JAVA class of a XOR "encryption" class:
import java.io.PrintStream;
public class Encryptor
{
private static final String m_strPrivateKey = "4p0L#r1$";
public Encryptor()
{
}
public static String encrypt(String pass)
{
String strTarget = XORString(pass);
strTarget = StringToHex(strTarget);
return strTarget;
}
public static String decrypt(String pass)
{
String strTarget = HexToString(pass);
strTarget = XORString(strTarget);
return strTarget;
}
private static String GetKeyForLength(int nLength)
{
int nKeyLen = "4p0L#r1$".length();
int nRepeats = nLength / nKeyLen + 1;
String strResult = "";
for(int i = 0; i < nRepeats; i++)
{
strResult = strResult + "4p0L#r1$";
}
return strResult.substring(0, nLength);
}
private static String HexToString(String str)
{
StringBuffer sb = new StringBuffer();
char buffDigit[] = new char[4];
buffDigit[0] = '0';
buffDigit[1] = 'x';
int length = str.length() / 2;
byte bytes[] = new byte[length];
for(int i = 0; i < length; i++)
{
buffDigit[2] = str.charAt(i * 2);
buffDigit[3] = str.charAt(i * 2 + 1);
Integer b = Integer.decode(new String(buffDigit));
bytes[i] = (byte)b.intValue();
}
return new String(bytes);
}
private static String XORString(String strTarget)
{
int nTargetLen = strTarget.length();
String strPaddedKey = GetKeyForLength(nTargetLen);
String strResult = "";
byte bytes[] = new byte[nTargetLen];
for(int i = 0; i < nTargetLen; i++)
{
int b = strTarget.charAt(i) ^ strPaddedKey.charAt(i);
bytes[i] = (byte)b;
}
String result = new String(bytes);
return result;
}
private static String StringToHex(String strInput)
{
StringBuffer hex = new StringBuffer();
int nLen = strInput.length();
for(int i = 0; i < nLen; i++)
{
char ch = strInput.charAt(i);
int b = ch;
String hexStr = Integer.toHexString(b);
if(hexStr.length() == 1)
{
hex.append("0");
}
hex.append(Integer.toHexString(b));
}
return hex.toString();
}
public static void main(String args[])
{
if(args.length < 1)
{
System.err.println("Missing password!");
System.exit(-1);
}
String pass = args[0];
String pass2 = encrypt(pass);
System.out.println("Encrypted: " + pass2);
pass2 = decrypt(pass2);
System.out.println("Decrypted: " + pass2);
if(!pass.equals(pass2))
{
System.out.println("Test Failed!");
System.exit(-1);
}
}
}
I tried to port it to Perl like this:
#!/usr/bin/perl
use strict;
use warnings;
my $pass = shift || die "Missing password!\n";
my $pass2 = encrypt($pass);
print "Encrypted: $pass2\n";
$pass2 = decrypt($pass2);
print "Decrypted: $pass2\n";
if ($pass ne $pass2) {
print "Test Failed!\n";
exit(-1);
}
sub encrypt {
my $pass = shift;
my $strTarget = XORString($pass);
$strTarget = StringToHex($strTarget);
return $strTarget;
}
sub decrypt {
my $pass = shift;
my $strTarget = HexToString($pass);
$strTarget = XORString($strTarget);
return $strTarget;
}
sub GetKeyForLength {
my $nLength = shift;
my $nKeyLen = length '4p0L#r1$';
my $nRepeats = $nLength / $nKeyLen + 1;
my $strResult = '4p0L#r1$' x $nRepeats;
return substr $strResult, 0, $nLength;
}
sub HexToString {
my $str = shift;
my #bytes;
while ($str =~ s/^(..)//) {
my $b = eval("0x$1");
push #bytes, chr sprintf("%d", $b);
}
return join "", #bytes;
}
sub XORString {
my $strTarget = shift;
my $nTargetLen = length $strTarget;
my $strPaddedKey = GetKeyForLength($nTargetLen);
my #bytes;
while ($strTarget) {
my $b = (chop $strTarget) ^ (chop $strPaddedKey);
unshift #bytes, $b;
}
return join "", #bytes;
}
sub StringToHex {
my $strInput = shift;
my $hex = "";
for my $ch (split //, $strInput) {
$hex .= sprintf("%02x", ord $ch);
}
return $hex;
}
Code seems ok but the problem is the JAVA class outputs different results than the Perl code.
In JAVA I have the plain-text passsword
mentos
and it is encoded as
&4\=80CHB'
What should I do to my Perl script to get the same result? Where I do wrong?
Another two examples: plain-text
07ch4ssw3bby
is encoded as:
,#(0\=DM.'# '8WQ2T
(note the space after #)
Last example, plain-text:
conf75
encoded as:
&7]P0G-#!
Thanks for help!
Ended up with this, thanks to Joni Salonen:
#!/usr/bin/perl
# XOR password decoder
# Greets: Joni Salonen # stackoverflow.com
$key = pack("H*","3cb37efae7f4f376ebbd76cd");
print "Enter string to decode: ";
$str=<STDIN>;chomp $str; $str =~ s/\\//g;
$dec = decode($str);
print "Decoded string value: $dec\n";
sub decode{ #Sub to decode
#subvar=#_;
my $sqlstr = $subvar[0];
$cipher = unpack("u", $sqlstr);
$plain = $cipher^$key;
return substr($plain, 0, length($cipher));
}
My only and last problem is that when a "\" is found (actually "\\" as one escaped the real character) the decryption goes wrong :-\
Example encoded string:
"(4\\4XB\:7"G#, "
(I escaped it with double-quotes, last characters of the string is a space, it should decode to
"ovFsB6mu"
Update: thanks to Joni Salonen, I have 100% working final version:
#!/usr/bin/perl
# XOR password decoder
# Greets: Joni Salonen # stackoverflow.com
$key = pack("H*","3cb37efae7f4f376ebbd76cd");
print "Enter string to decode: ";
$str=<STDIN>;chomp $str; $str =~s/\\(.)/$1/g;
$dec = decode($str);
print "Decoded string value: $dec\n";
sub decode{ #Sub to decode
#subvar=#_;
my $sqlstr = $subvar[0];
$cipher = unpack("u", $sqlstr);
$plain = $cipher^$key;
return substr($plain, 0, length($cipher));
}
Your encryption loop skips the first character of $strTarget if it happens to be '0'. You could compare it against an empty string instead of checking if it's "true":
while ($strTarget ne '') {
my $b = (chop $strTarget) ^ (chop $strPaddedKey);
unshift #bytes, $b;
}
Update: This program decrypts your strings:
use feature ':5.10';
$key = pack("H*","3cb37efae7f4f376ebbd76cd");
say decrypt("&4\=80CHB'"); # mentos
say decrypt(",#(0\=DM.'# '8WQ2T"); # 07ch4ssw3bby
say decrypt("&7]P0G-#!"); # conf75
sub decrypt {
$in = shift;
$cipher = unpack("u", $in);
$plain = $cipher^$key;
return substr($plain, 0, length($cipher));
}

Categories