Simple one line, native encryption / decryption method in Java

Is there a dead simple native method for encrypting / decrypting a string with a key in Java? I don’t care what type of encryption (AES, DES, etc.), I just do not care that it is connected using a key and does not break easily.

Ideally, I would like this to be a one line solution:

String encryptedString = NativeEncryptionClass.encrypt("this is the data", "key123"); 

thanks

+6
source share
5 answers

Not sure if you can do this in a [plausible] single-line space, but you can easily achieve simple symmetric encryption - look at the following example:

Private example using DES

I used the Bouncy Castle library for a good effect in the past.

+3
source

If you mean a native platform-independent library, maybe jasypt might be of interest to you.

+2
source

Well, maybe this is not a "one line", but it looks easy enough:

use java cipher class. look here ... (there are other examples on this page ...)

http://download.oracle.com/javase/6/docs/technotes/guides/security/crypto/CryptoSpec.html#BlowKeyEx

+1
source

Maybe you should try using simple XOR encryption.

0
source
 import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class SimpleMD5Example { public static void main(String[] args) { String passwordToHash = "0"; String generatedPassword = null; try { // Create MessageDigest instance for MD5 MessageDigest md = MessageDigest.getInstance("MD5"); // Add password bytes to digest md.update(passwordToHash.getBytes()); // Get the hash bytes byte[] bytes = md.digest(); System.out.println(bytes); // This bytes[] has bytes in decimal format; // Convert it to hexadecimal format StringBuilder sb = new StringBuilder(); for (int i = 0; i < bytes.length; i++) { sb.append(Integer.toString((bytes[i] & 0xff), 16).substring(1)); } // Get complete hashed password in hex format generatedPassword = sb.toString(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } System.out.println(generatedPassword); } } 
-1
source

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


All Articles