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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:22:10+00:00 2026-06-07T03:22:10+00:00

I have a question regarding encrypting and decrypting a string I have to send

  • 0

I have a question regarding encrypting and decrypting a string

I have to send a encrypted string over the network.(an android app is the client) this is what i did so far

byte[] input = getByteArray(filePath);//get the message stored in a file as a byte array

by going through some tutorial i managed to get the String message to a byte array and
encrypted it using javax.crypto

Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");

cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);

encrypted msg is retrived as a byte array

byte[] encrypted

i even managed to decrypt it using the reverse method and get the message again

but my problem comes when i try to convert this encrypted byte array to strings (to pass it over the network)
and then reconvert it to a byte array

i tryed this

String encryptedStrn = new String(encrypted); // convert to string

when i convert it to the byte array by
byte[] enc = encryptedStrn.getBytes();

and use this enc array to decrypt but the output does not come correctly.

Have i missed some basic stuff regarding converting. Please help me.
thanks in advance

  • 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-07T03:22:13+00:00Added an answer on June 7, 2026 at 3:22 am

    As CodeInChaos wrote in a comment, you shouldn’t use the String(byte[]) constructor to create a string from opaque binary data. The string constructors are intended for text data which has been encoded using an encoding like ASCII, UTF-8 etc. Opaque binary data such as the result of encryption, or an image file, is not encoded text data in the same way, so you end up losing information.

    You should use base64 instead, which encodes any binary data into ASCII. There are various 3rd party libraries for this, including a good public domain one. Alternatively, on Android you can just use the Base64 class.

    Additionally, even when you are encoding or decoding real text, you shouldn’t use String.getBytes() and the String(byte[]) constructor anyway – they use the platform default encoding, which is almost always the wrong choice. Instead, you should use the overloads which explicitly take a CharSet or the name of a character encoding. UTF-8 is typically a good encoding to use if you’re able to control both ends – if you’re only controlling one end, you need to know which encoding the other end is expecting.

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

Sidebar

Related Questions

I have a question regarding parsing data from Wikipedia for my Android app. I
i have a question regarding the AsyncTask class in android, and why it is
I have question regarding polymorphism about its assignment statement, for Example this is The
I have a question regarding inheritance in Java. If I have this base class
I have a question regarding file reading and I am getting frustrated over it
I have a question regarding threading in Java. In my code, ... client.doSth(); //
I would have question regarding web services. Let's say I have webservice client that
I have a question regarding this code. I'm a beginner and enjoy learning C#.
I have a question regarding vectors: If I have a std::vector<MyClass> will this vector
I have a question regarding in-app billing. I have a published app that currently

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.