Java encryption

I have a text configuration file. This file must be encrypted and sent along with the product so that the end user cannot change the values.

I looked through AES for this and came across this simple example.


import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.io.*;

/**
 * This program generates a AES key, retrieves its raw bytes, and
 * then reinstantiates a AES key from the key bytes.
 * The reinstantiated key is used to initialize a AES cipher for
 * encryption and decryption.
 */
public class AES
{

    /**
     * Turns array of bytes into string
     *
     * @param buf   Array of bytes to convert to hex string
     * @return  Generated hex string
     */
    public static String asHex(byte buf[])
    {
        StringBuilder strbuf = new StringBuilder(buf.length * 2);
        int i;

        for (i = 0; i &lt buf.length; i++)
        {
            if (((int) buf[i] & 0xff) &lt 0x10)
            {
                strbuf.append("0");
            }

            strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
        }

        return strbuf.toString();
    }

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

        String message = "This is just an example";

        // Get the KeyGenerator

        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        kgen.init(128); // 192 and 256 bits may not be available


        // Generate the secret key specs.
        SecretKey skey = kgen.generateKey();
        byte[] raw = skey.getEncoded();

        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");


        // Instantiate the cipher

        Cipher cipher = Cipher.getInstance("AES");

        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

        byte[] encrypted = cipher.doFinal(message.getBytes());
        System.out.println("encrypted string: " + asHex(encrypted));

        cipher.init(Cipher.DECRYPT_MODE, skeySpec);
        byte[] original = cipher.doFinal(encrypted);
        String originalString = new String(original);
        System.out.println("Original string: " + originalString + " " + asHex(original));
    }
}


Is this a good approach? I can get the configuration file as a string, encode it, write encoded bytes to the file. Then I could distribute the encoded file.

On the other hand, how would I decode it? How to distribute a key specification so that it can be decrypted?

thank

+3
source share
4 answers

(Cancel the slightly abridged version of my comment as an answer, as it received a few votes, and I am pretty sure this is the answer.)

, , : , ; . - ( , ), . , . , , , - (Edit: , -).

@limc, - , . , @Mr Jacques - , , 100% ( , ), ( - , ...)

+5

jar . , .

+2

- - , , .

- "" , .

, . , .

+2

. , - (Adobe, Microsoft, Symantec) n-way-handshake, , . , ... , , ..

In one of my projects that I worked on, I saved the encryption key in the registry of the user's computer. Of course, my project is not a top-secret project, and it does not prevent a more experienced user from looking for a key in the registry. This is still better than hard-coding the encryption key in the source code, especially if I need to manage project versions.

+2
source

Source: https://habr.com/ru/post/1795271/


All Articles