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 6886985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:49:42+00:00 2026-05-27T05:49:42+00:00

I am implementing a Cipher Block Chaining for school work and the question asks

  • 0

I am implementing a Cipher Block Chaining for school work and the question asks for a method taking String and returning another String. At first, I thought it was odd and that byte[] variables would be much more adequate, but implemented a method still. Basically, here’s the code :

static public String encode(String message) {
   byte[] dataMessage = message.getBytes();
   ByteArrayOutputStream out = new ByteArrayOutputStream();

   byte last = (byte) (Math.random() * 256);
   byte cur;
   out.write(last);

   for (byte b : data) {
      cur = (byte) (b^last);
      System.out.println("Encode '" + (char) b + "' = " + b + "^" + last + " > " + cur );
      out.write( cur );
      last = cur;
   }

   System.out.println("**ENCODED BYTES = " + Arrays.toString(out.toByteArray()));
   System.out.println("**ENCODED STR   = " + Arrays.toString(out.toString().getBytes()));

   return out.toString();
}

The decode method works similarly. Some times, the method will spit results like

Encode 'H' = 72^109 > 37
Encode 'e' = 101^37 > 64
Encode 'l' = 108^64 > 44
Encode 'l' = 108^44 > 64
Encode 'o' = 111^64 > 47
**ENCODED BYTES = [109, 37, 64, 44, 64, 47]
**ENCODED STR   = [109, 37, 64, 44, 64, 47]

But sometimes will also spit things like

Encode 'H' = 72^-63 > -119
Encode 'e' = 101^-119 > -20
Encode 'l' = 108^-20 > -128
Encode 'l' = 108^-128 > -20
Encode 'o' = 111^-20 > -125
**ENCODED BYTES = [-63, -119, -20, -128, -20, -125]
**ENCODED STR   = [-17, -65, -67, -17, -65, -67, -17, -65, -67, -17, -65, -67]

I presume that this has something to do with UTF-8 (the system’s default encoding), but I’m not familiar enough to figure out exactly why such a string would be returned with the given bytes.

  • 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-05-27T05:49:43+00:00Added an answer on May 27, 2026 at 5:49 am

    You can’t take an arbitrary sequence of bytes and assume it’s a valid UTF-8 encoded string. So, I suspect that the toString method, as documented, replaces malformed-input and unmappable-character sequences with the default replacement string for the platform’s default character set.

    You should thus not transform purely binary data into a String like this. Use some encoding like Hex or Base64 to transform your bytes to a printable string, and vice-versa.

    Apache commons-codec has a Base64 utility class.

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

Sidebar

Related Questions

I'm implementing a c# program that should automatize a Mono-alphabetic substitution cipher. The functionality
Implementing the very first scala example I ran into some problems probably tipical for
Whilst implementing my first MVVM application in WPF, I've been wondering about the pros
I'm implementing encryption / decryption using Java Cipher and AES. Everything is working well
Implementing Interface just provide the skeleton of the method. If we know the exact
While implementing the code from this question on my project I realized there's 3
When implementing iterator using yield return , is there any difference between returning IEnumerator
when implementing/using methods that return or work with instances of objects, what is the
Implementing Equals() for reference types is harder than it seems. My current canonical implementation
Implementing a 'sandbox' environment in Python used to be done with the rexec module

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.