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 < buf.length; i++) { if (((int) buf[i] & 0xff) < 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
(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% ( , ), ( - , ...)
jar . , .
- - , , .
- "" , .
, . , .
. , - (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.
Source: https://habr.com/ru/post/1795271/More articles:Using a ticket server to generate primary identifiers? - javaSending CTRL-S message to window - c #https://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1795268/strange-behaviour-on-safari-on-ios-too-complicated-to-explain-here&usg=ALkJrhiPkSpVwc9kCDtRTOZkuR81D_cmIwTypical erlang web / enterprise application architecture? - erlangdifference between recursive function and stack usage in terms of memory usage - algorithmHow to write * / in javadoc - javaNeed a "SOAP client with WSSE authentication" ... is Visual Studio suitable for this? - authenticationDoes Cast вызывает IQueryable для оценки в Linq-to-SQL? Каковы ограничения на его использование? - castingNHibernate Cascade Deletes - c #How to unit test heart rate pattern? - c #All Articles