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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:44:33+00:00 2026-06-09T18:44:33+00:00

I want to encrypt a text file using AES encryption. However,I am not so

  • 0

I want to encrypt a text file using AES encryption.
However,I am not so sure how to combine the Aes codes and the file reading code. I am new to this kind of encryption. Any help is appreciated.

I have tried to do this. And there is error under the encrypt and it state not applicable for the arguments. Or I should do it in another way?

public static void main(String[] args) throws Exception {

    FileReader file = new FileReader ("original.txt");
    BufferedReader reader = new BufferedReader(file);

    String text = "";
    String line = reader.readLine();
    while(line !=null)
    {
        text +=line; 
        line = reader.readLine();
    }


    String test = Testing.encrypt(text);
    System.out.println("Encrypted : " + test);
    reader.close();
  } 

The full codes is below. Thank you very much.

(AES encryption)

 package encypt.com;

import java.io.BufferedReader;
import java.io.FileReader;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.*;

public class Testing {

private static final String ALGORITHM = "AES";
private static final int ITERATIONS = 2;
private static final byte[] keyValue = 
    new byte[] { 'T', 'h', 'i', 's', 'I', 's', 'A', 'S', 'e', 'c', 'r', 'e', 't', 'K', 'e', 'y'};

public static String encrypt(String value, String salt) throws Exception {
    Key key = generateKey();
    Cipher c = Cipher.getInstance(ALGORITHM);  
    c.init(Cipher.ENCRYPT_MODE, key);

    String valueToEnc = null;
    String eValue = value;
    for (int i = 0; i < ITERATIONS; i++) {
        valueToEnc = salt + eValue;
        byte[] encValue = c.doFinal(valueToEnc.getBytes());
        eValue = new BASE64Encoder().encode(encValue);
    }
    return eValue;
}

public static String decrypt(String value, String salt) throws Exception {
    Key key = generateKey();
    Cipher c = Cipher.getInstance(ALGORITHM);
    c.init(Cipher.DECRYPT_MODE, key);

    String dValue = null;
    String valueToDecrypt = value;
    for (int i = 0; i < ITERATIONS; i++) {
        byte[] decordedValue = new BASE64Decoder().decodeBuffer(valueToDecrypt);
        byte[] decValue = c.doFinal(decordedValue);
        dValue = new String(decValue).substring(salt.length());
        valueToDecrypt = dValue;
    }
    return dValue;
}

private static Key generateKey() throws Exception {
    Key key = new SecretKeySpec(keyValue, ALGORITHM);
    // SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
    // key = keyFactory.generateSecret(new DESKeySpec(keyValue));
    return key;
}


public static void main(String[] args) throws Exception {


    String password = "mypassword";
    String salt = "this is a simple clear salt";
    String passwordEnc = Testing.encrypt(password, salt);
    String passwordDec = Testing.decrypt(passwordEnc, salt);

   System.out.println("Salt Text : " + salt);
   System.out.println("Plain Text : " + password);
   System.out.println("Encrypted : " + passwordEnc);
   System.out.println("Decrypted : " + passwordDec);
}
}

(File reading code)

package encypt.com;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Scanner;

public class readfile {
public static void main(String[] args) throws Exception {

    FileReader file = new FileReader ("key.txt");
    BufferedReader reader = new BufferedReader(file);

    String text = "";
    String line = reader.readLine();
    while(line !=null)
    {
        text +=line; 
        line = reader.readLine();
    }

    reader.close();
    System.out.println(text); 
  } 
} 
  • 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-09T18:44:34+00:00Added an answer on June 9, 2026 at 6:44 pm

    I guess your error occurs because you try to call

    public static String encrypt(String value, String salt)
    

    with

    String test = Testing.encrypt(text);
    

    so the salt parameter is missing.

    Try calling this function with i.e.

    String test = Testing.encrypt(text,"mySalt");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want the code of Rijndael algorithm to encrypt any given text to store
I have some text that is in a file. I want to encrypt this
I've some clear text which I want to encrypt using RSA_PKCS_V21 (using PolarSSL library).
so my problem is simple. I want to Encrypt some text and write it
I have some code to encrypt some strings in Python. Encrypted text is used
I want to read a the content of a PDF file and encrypt the
I have some C code to encrypt text: ngx_int_t ngx_http_encrypted_session_3des_mac_encrypt (ngx_pool_t * pool, ngx_log_t
I want to encrypt an integer with DES, the resultant cipher text should also
What is a simple and effective way to encrypt a plain text file? I'm
I am using visual studio 2010 my project is that, i want to 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.