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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:19:34+00:00 2026-06-05T22:19:34+00:00

I wrote the following code for text file encryption and decryption. The encryption process

  • 0

I wrote the following code for text file encryption and decryption.

The encryption process works fine but I cannot decrypt the created file. For some reason, even though I use the same password, with the same length and same paddings, the decryption output is an encoded file different from the original one…

In other attempts I failed while running encryption and decryption in separated runs. But now they are both running one after another (even though they are using different key-spec, is that the reason? if so – how can I bypass it?)

public static void main(String[] args) throws Exception {
    encrypt("samplePassword", new FileInputStream("file.txt"), new FileOutputStream("enc-file.txt"));
    decrypt("samplePassword", new FileInputStream("enc-file.txt"), new FileOutputStream("file-from-enc.txt"));
}

public static void encrypt(String password, InputStream is, OutputStream os) throws Exception {

    SecretKeySpec keySpec = new SecretKeySpec(password(password), "TripleDES");
    Cipher cipher = Cipher.getInstance("TripleDES");
    cipher.init(Cipher.ENCRYPT_MODE, keySpec);
    byte[] buf = new byte[8096];
    os = new CipherOutputStream(os, cipher);
    int numRead = 0;
    while ((numRead = is.read(buf)) >= 0) {
        os.write(buf, 0, numRead);
    }
    os.close();
}

public static void decrypt(String password, InputStream is, OutputStream os) throws Exception {

    SecretKeySpec keySpec = new SecretKeySpec(password(password), "TripleDES");
    Cipher cipher = Cipher.getInstance("TripleDES");
    cipher.init(Cipher.ENCRYPT_MODE, keySpec);

    byte[] buf = new byte[8096];
    CipherInputStream cis = new CipherInputStream(is, cipher);
    int numRead = 0;
    while ((numRead = cis.read(buf)) >= 0) {
        os.write(buf, 0, numRead);
    }
    cis.close();
    is.close();
    os.close();
}

private static byte[] password(String password) {

    byte[] baseBytes = { (byte) 0x38, (byte) 0x5C, (byte) 0x8, (byte) 0x4C, (byte) 0x75, (byte) 0x77, (byte) 0x5B, (byte) 0x43,
            (byte) 0x1C, (byte) 0x1B, (byte) 0x38, (byte) 0x6A, (byte) 0x5, (byte) 0x0E, (byte) 0x47, (byte) 0x3F, (byte) 0x31,
            (byte) 0xF, (byte) 0xC, (byte) 0x76, (byte) 0x53, (byte) 0x67, (byte) 0x32, (byte) 0x42 };
    byte[] bytes = password.getBytes();
    int i = bytes.length;
    bytes = Arrays.copyOf(bytes, 24);
    if(i < 24){
        for(;i<24; i++){
            bytes[i] = baseBytes[i];
        }
    }
    return bytes;
}

can any one give me a hint?

  • 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-05T22:19:37+00:00Added an answer on June 5, 2026 at 10:19 pm

    On a first look,

    cipher.init(Cipher.ENCRYPT_MODE, keySpec);
    

    should probably be

    cipher.init(Cipher.DECRYPT_MODE, keySpec);
    

    in the decrypt method.

    Have a look at the documentation.

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

Sidebar

Related Questions

I wrote the following code to select text from database,but when i echo the
I wrote the following html file <code> <html> <head> <script text =text/javascript> function createCookie(name,value,days)
I wrote the following code to copy the text file to another: FILE *fpr,
I wrote the following piece of code to read a text file inside of
i wrote following code to create a linkbutton programmatically, but its showing like lable
I wrote following code...but i am getting Error like: Error 1 'LoginDLL.Class1.Login(string, string, string)':
I wrote the following code to count the node of an XML file: private
I wrote the following method to see whether particular file contains ASCII text characters
(VB.NET, .NET 3.5) I wrote the following function to read some text from txt
if wrote the following code and do not understand why the trace returns false:

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.