Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8605419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:49:34+00:00 2026-06-12T02:49:34+00:00

I’m facing this weird issue. I use the Character.reverseBytes(char ch) method for the purpose

  • 0

I’m facing this weird issue. I use the "Character.reverseBytes(char ch)" method for the purpose of encrypting. It works fine, when running inside NetBeans. But Whenever I try to run it outside, it gives weird outputs.

I think the issue is that in two occasions, it use two different encoding methods (or something like that). The following code demonstrate the issue.

import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;

public class Encryptor {

    public static String encode(String in) {
        ArrayList<Character> list = new ArrayList<Character>();
        for (int i = 0; i < in.length(); i++) {
            list.add(Character.reverseBytes(in.charAt(i)));
        }
        return arrayToString(list);
    }

    public static String decode(String in) {
        ArrayList<Character> list = new ArrayList<Character>();
        for (int i = 0; i < in.length(); i++) {
            list.add(Character.reverseBytes(in.charAt(i)));
        }
        return arrayToString(list);
    }

    private static String arrayToString(ArrayList<Character> list) {
        char[] ch = new char[list.size()];
        for (int i = 0; i < list.size(); i++) {
            ch[i] = list.get(i);
        }
        return String.copyValueOf(ch);
    }

    public static void main(String[] args) throws java.io.FileNotFoundException, java.io.IOException {
        String pass = "Password";
        String passEn = encode(pass);
        File file = new File(System.getProperty("user.dir") + "/pass.txt");
        file.createNewFile();
        PrintWriter pr = new PrintWriter(file);
        pr.write(passEn);
        pr.flush();
        passEn = new java.util.Scanner(file).next();
        String passDe = decode(passEn);
        String msg;
        msg = "Initial : " + pass
                + "\nEncrypted : " + passEn
                + "\nDecrypted : " + passDe;
        javax.swing.JOptionPane.showMessageDialog(null, msg);
    }
}

First I save the encrypted word to a file, then try to decode the saved word. This gives two different outputs in the above mentioned two occasions.

Is there any way to fix this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T02:49:36+00:00Added an answer on June 12, 2026 at 2:49 am

    Fundamentally, Character.reverseBytes is giving you a somewhat arbitrary UTF-16 code unit. There’s no guarantee whatsoever that the characters you get afterwards will form a valid string – there could be “half” surrogate pairs, for example, or characters which don’t have any specific meaning in Unicode.

    More importantly, there’s every chance that when you write the “text” to a file and read it back, you’ll get different values – particularly as you’re not specifying an encoding. There’s a very good chance that the default encoding you’re using can’t support the characters you’ve ended up with.

    Fundamentally, you don’t want to do any of this. Encryption of strings should basically take the form of:

    • Encode the string to bytes in a fixed encoding (e.g. UTF-8)
    • Encrypt the binary data (please don’t roll your own; Java includes plenty of crypto facilities)
    • Pass the encrypted binary data wherever you need it to. If you must represent the binary data as a string, use something like base64 to make sure you can recover the exact binary data later.

    Then to decrypt, you reverse each operation:

    • Retrieve the encrypted binary data, which may involve converting back from base64
    • Perform the appropriate decryption step (binary to binary) based on whatever encryption algorithm you used earlier
    • Convert the result (still bytes) back to a string, using the same encoding you used at the very start

    Treating arbitrary binary data as text – which is effectively what Character.reverseBytes is doing, in a very crude way – is a really bad idea.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.