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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:26:48+00:00 2026-06-15T17:26:48+00:00

I have to encrypt/decrypt plain text in java using DES with a key. I

  • 0

I have to encrypt/decrypt plain text in java using DES with a key. I have got a very good tutorial at IBM which can be found here . The problem with this example is that it is generating the key in the program itself. Now if I encrypt a string(eg password) and store in database then I would not be able to decrypt it because I would not know the key.

Below is the example at IBM

import java.security.*;
import javax.crypto.*;
//
// encrypt and decrypt using the DES private key algorithm
public class PrivateExample {

  public static void main (String[] args) throws Exception {
  //
  // check args and get plaintext
    if (args.length !=1) {
      System.err.println("Usage: java PrivateExample text");
      System.exit(1);
    }
  byte[] plainText = args[0].getBytes("UTF8");
  //
  // get a DES private key
  System.out.println( "\nStart generating DES key" );
  KeyGenerator keyGen = KeyGenerator.getInstance("DES");
  keyGen.init(56);
  Key key = keyGen.generateKey();
  System.out.println( "Finish generating DES key" );
//
// get a DES cipher object and print the provider
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
System.out.println( "\n" + cipher.getProvider().getInfo() );
//
// encrypt using the key and the plaintext
System.out.println( "\nStart encryption" );
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] cipherText = cipher.doFinal(plainText);
System.out.println( "Finish encryption: " );
System.out.println( new String(cipherText, "UTF8") );

//
// decrypt the ciphertext using the same key
System.out.println( "\nStart decryption" );
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] newPlainText = cipher.doFinal(cipherText);
System.out.println( "Finish decryption: " );

System.out.println( new String(newPlainText, "UTF8") );
}
}

Can anyone suggest how can I add my own key in this example?

  • 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-15T17:26:49+00:00Added an answer on June 15, 2026 at 5:26 pm

    Make the key one of the args instead of generateKey if you plan to supply the key.

    Edit: generateKey generates a random key. It might be simpler to save this key to use for decryption than to add code to parse a key arg. Have a look at KeyGenerator and SecretKey.

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

Sidebar

Related Questions

I am trying to encrypt some plain text with a public key using RSA_public_encrypt(),
I am using ColdFusion to encrypt and decrypt. I have variable active with a
I have a requirement to programmatically encrypt portions of a plain text file in
I have the following code to encrypt/decrypt data in node.js, which is working: var
I have an Android application, which uses javax.crypto to encrypt some text data in
I'm running Java in Eclipse, fyi. I have plaintext which is encrypted using Blowfish
I have the following python script to encrypt/decrypt data using AES 256, could you
I have an enCrypt and deCrypt class which I want to use in my
I have to encrypt/decrypt some sensitive information in a Xml file? Yes I can
I need to encrypt and decrypt short strings (Ex. 'product1234'). I have used mcrypt_encrypt

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.