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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:50:54+00:00 2026-05-22T23:50:54+00:00

I have the following problem. I use this code to encrypt a sample text

  • 0

I have the following problem. I use this code to encrypt a sample text in C#, and want to decrypt it in java. I use the following java code.

byte[] IV = { 65, 1, 2, 23, 4, 5, 6, 7, 32, 21, 10, 11, 12, 13, 84, 45 };
byte[] KEY = { 0, 42, 2, 54, 4, 45, 6, 7, 65, 9, 54, 11, 12, 13, 60, 15 };
byte baData[] = new byte[1024];
int iRead = 0;

SecretKeySpec key = new SecretKeySpec(KEY, "AES/CBC/PKCS5Padding");
Cipher cipher = Cipher.getInstance ("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(IV));

File file = new File("/sdcard", "SAMPLE.txt");

FileInputStream in = new FileInputStream(file);
iRead = in.read(baData, 0, baData.length);

String strResult = new String(cipher.doFinal(baData, 0, baData.length));

I obviously use the same IV and KEY values above as in the C# sample. The above java code runs on an android device, and the encrypted binary file is the SAMPLE.txt on it’s sdcard.
The problem is I don’t get the correct data after decrypting it. Can anyone tell me what I’m doing wrong?

Thanks.

  • 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-22T23:50:55+00:00Added an answer on May 22, 2026 at 11:50 pm

    There are a few problems here:

    • What encoding are you using to convert the text data into binary before encrypting it? Your current use of the string constructor will use the platform default encoding – almost never a good idea
    • You’re assuming that a single call to read() will read everything – you’re not actually using the value of iRead later
    • You’re assuming that all the data will fit in a 1024-byte buffer

    It’s hard to know which of those is causing the problem.

    I would strongly suggest that you use a CipherInputStream to wrap your FileInputStream, and then wrap an InputStreamReader around that, using the appropriate encoding. You could then use something like Guava’s CharStreams.toString() method to read everything from the InputStreamReader into a string.

    EDIT: As noted in comments, while all of these are potential problems, there’s also the matter of the code currently creating the Cipher in encryption mode rather than decryption mode.

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

Sidebar

Related Questions

i have following problem To decode the Biquinary code use the number 5043210. At
i've got the following Problem / Question: I have HTML Code like this: <div
I have the following problem when i use the selectManyCheckBox: campaignInformationForm.campaignInformation.googleAnalytics (this are boolean
I have the following sample code #!/usr/bin/perl use strict; use warnings; my $b =
I have the following problem: I need to use XSLFO to generate a 2-column
Trying to use JSTL but have the following problem: Index.xhtml page: <?xml version=1.0 encoding=UTF-8?>
I have following problem, Code: String a=Yeahh, I have no a idea what's happening
I have the following problem: I have an HTML textbox ( <input type=text> )
I have the following problem in my Data Structures and Problem Solving using Java
I have the following problem: I use a script that saves the referer URL.

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.