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

  • Home
  • SEARCH
  • 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 8900237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:03:31+00:00 2026-06-15T01:03:31+00:00

I’m trying to convert an Integer to a String, and then encrypt the String

  • 0

I’m trying to convert an Integer to a String, and then encrypt the String with a XOR encryption. But when i’m decrypting my Strin again, i get a different answer, that the String i typed before the encryption, and i don’t know what i’m doing wrong?

public class Krypte {
public static void main (String [] args) {
    int i = 12345;

    String k = Integer.toString(i);
    String G = secure(k.getBytes());
    System.out.println("Encrypted: " + G);

    String U = secure(G.getBytes());
    System.out.println("Decrypted: " + U);
    int X = Integer.parseInt(U);
    System.out.println("As an int: " + X);

}

public static String secure(byte[] msg) {
    // Variables
    int outLength = msg.length;
    byte secret = (byte) 0xAC; // same as 10101100b (Key)
    // XOR kryptering
    for (int i = 0; i < outLength; i++) {
        // encrypting each byte with XOR (^)
        msg[i] = (byte) (msg[i] ^ secret);
    }
    return new String(msg);
}
}
  • 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-15T01:03:32+00:00Added an answer on June 15, 2026 at 1:03 am

    There’s a subtle (yet very important) difference between char and byte types. Consider this:

    class Krypte {
        public static void main (String [] args) {
            int i = 12345;
            String k = Integer.toString(i);
            System.out.println("Before: " + k);    
            String G = secure(k.toCharArray());
            System.out.println("Encrypted: " + G);
            String U = secure(G.toCharArray());
            System.out.println("Decrypted: " + U);
            int X = Integer.parseInt(U);
            System.out.println("As an int: " + X);
        }
    
        public static String secure(char[] msg) {
            // Variables
            int outLength = msg.length;
            byte secret = (byte) 0xAC; // same as 10101100b (Key)
            // XOR kryptering
            for (int i = 0; i < outLength; i++) {
                // encrypting each byte with XOR (^)
                System.out.println("Byte before: " + msg[i]);
                msg[i] = (char) (msg[i] ^ secret);
                System.out.println("Byte after: " + msg[i]);
            }
            return new String(msg);
        }
    }
    

    This works (proof), because XORing some character value with a byte will (most probably) give you a valid character.

    Not let’s see what happens in the original snippet – by adding this debugging output into the main loop of secure method:

     System.out.println("Byte before: " + msg[i]);
     msg[i] = (byte) (msg[i] ^ secret);
     System.out.println("Byte after: " + msg[i]);
    

    And the output would be:

    Byte before: 49
    Byte after: -99
    Byte before: 50
    Byte after: -98
    Byte before: 51
    Byte after: -97
    Byte before: 52
    Byte after: -104
    Byte before: 53
    Byte after: -103
    

    It’s quite ok: first getBytes function encoded the string given into an array of bytes using the platform’s default charset. Character '1' gets encoded into 49 byte value; '2' becomes 50, etc.

    Then we’re XORing these values with our key – and get this sequence of bytes:

    -99 -98 -97 -104 -103
    

    The final step seems easy: we just make (and return) a new String from this sequence of bytes, what can go wrong here? But in fact it’s the very step where, well, the fan get hit. )

    See, String constructor tries to process this sequence of bytes using the platform’s default charset. Indeed, for some charsets these bytes represent a sequence of valid characters just fine – but not for UTF-8!

    …You probably already guessed what happens next. For each ‘undecodable’ sequence of bytes, as described here, the first byte is transformed into so-called Replacement character, and others are retried. In this particular example there would be five of these signs of failure in the string returned by the first secure invokation.

    Decoding this string is, well, quite meaningless – as it doesn’t store any information (except length) about the target string. That’s why the original code ultimately failed.

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

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to count how many characters a certain string has in PHP, but
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
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
Seemingly simple, but I cannot find anything relevant on the web. What is the
I've got a string that has curly quotes in it. I'd like to replace

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.