-2

Hello I need AES algorithm for data files not string and don't know how to modify this code..... I want public static String encrypt get pass and file route and replace the encrypted file with original file

public class AES {
    private static SecretKeySpec secretKey;
    private static byte[] key;

    public static void setKey(String myKey)
    {
        MessageDigest sha = null;
        try {
            key = myKey.getBytes("UTF-8");
            sha = MessageDigest.getInstance("SHA-1");
            key = sha.digest(key);
            key = Arrays.copyOf(key, 16);
            secretKey = new SecretKeySpec(key, "AES");
        }
        catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    public static String encrypt(String strToEncrypt, String secret)
    {
        try
        {
            setKey(secret);
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            return Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8")));
        }
        catch (Exception e)
        {
            System.out.println("Error while encrypting: " + e.toString());
        }
        return null;
    }

    public static String decrypt(String strToDecrypt, String secret)
    {
        try
        {
            setKey(secret);
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt)));
        }
        catch (Exception e)
        {
            System.out.println("Error while decrypting: " + e.toString());
        }
        return null;
    }
}
Neo
  • 3,534
  • 2
  • 20
  • 32
jack jo
  • 11
  • 3

1 Answers1

0

A few observations:

  1. setKey is public, but you use it as private. Don't set they key in encrypt, leaving it to the caller (my preferred option) or make it private, rebreanding it as some kind of helper function.
  2. encrypt gets a String only to turn it into a byte[] (using getBytes()), and anyway you want to encrypt files, which are generally referred to as bytes, not strings (not all files are textual). It should get and return byte[], IMO. If you want to treat the file as textual, ignore this one.
  3. You want encrypt to do something it shouldn't. As named, it should only encrypt. Don't make it use files, etc.

So my suggestion is, create a new method encryptFile.
Use Files.readAllBytes (or read this about reading text files).
Use encrypt to encrypt the data.
Use Files.write to write back to file.

Neo
  • 3,534
  • 2
  • 20
  • 32
  • first a lot thanks for your help you mean "public static byte[] encrypt(byte[] strToEncrypt.getBytes(), String secret)" ? so what about this line : "return Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8")));" ? – jack jo Oct 07 '17 at 21:57
  • No. `public static byte[] encrypt(byte[] toEncrypt, String secret)`. As for the return statement, you should just `.encode` (not `encodeToString`) and you shouldn't use `getBytes` as you have them already. – Neo Oct 07 '17 at 22:06