Jasypt is very simple and easy library for encrypt and decrypt in Java. You can encrypt your data with your key. Then you can decrypt it with the same key.
Before using this library you have to add the following dependency to your maven project:
<dependency> <groupId>org.jasypt</groupId> <artifactId>jasypt</artifactId> <version>1.9.2</version> </dependency>
Simple code example:
package az.mm.jasypt; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import org.jasypt.util.text.BasicTextEncryptor; /** * * @author MM <[email protected]> */ public class TestJasypt { private static char[] password; static { try (InputStream in = TestJasypt.class.getClassLoader().getResourceAsStream("app.properties");) { Properties props = new Properties(); props.load(in); password = props.getProperty("password").toCharArray(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { String text = "This is my private data"; String encrypt = encrypt(text); String decrypt = decrypt(encrypt); System.out.println("\noriginal: " + text); System.out.println("\nencyrpt: " + encrypt); System.out.println("\ndecyrpt: " + decrypt); } private static String encrypt(String text) { BasicTextEncryptor textEncryptor = new BasicTextEncryptor(); textEncryptor.setPasswordCharArray(password); String encryptedText = textEncryptor.encrypt(text); return encryptedText; } private static String decrypt(String text) { BasicTextEncryptor textEncryptor = new BasicTextEncryptor(); textEncryptor.setPasswordCharArray(password); String decryptedText = textEncryptor.decrypt(text); return decryptedText; } public static char[] getPassword() { return password; } }
Output:
original: This is my private data encyrpt: rS+kUNDW0zvJCzWkKlaVgDdHuOfYawKMH+/JvDQQmQY= decyrpt: This is my private data
You can use this library in RESTful Web Service to encrypt json data. I developed very simple code example regarding it.
RESTful Web Service example:
Controller.java
@RestController public class Controller { @GetMapping("/currency/rates") public String getRates() { List<Currency> rates = new ArrayList(); Rate rateUSD = new Rate(); rateUSD.setEUR("0.81307"); rateUSD.setGBP("0.71502"); rateUSD.setJPY("106.74"); rateUSD.setRUB("56.466"); rateUSD.setTRY("3.7869"); Currency currency1 = new Currency(); currency1.setBase("USD"); currency1.setDate(LocalDate.now().toString()); currency1.setRate(rateUSD); rates.add(currency1); Rate rateRUB = new Rate(); rateRUB.setEUR("0.014399"); rateRUB.setGBP("0.012663"); rateRUB.setJPY("1.8903"); rateRUB.setUSD("0.01771"); rateRUB.setTRY("0.067065"); Currency currency2 = new Currency(); currency2.setBase("RUB"); currency2.setDate(LocalDate.now().toString()); currency2.setRate(rateRUB); rates.add(currency2); String json = new Gson().toJson(rates); System.out.println("gson output: " + json); BasicTextEncryptor textEncryptor = new BasicTextEncryptor(); textEncryptor.setPasswordCharArray("mySecretKey".toCharArray()); String encryptedJson = textEncryptor.encrypt(json); return encryptedJson; } }
Currency.java
public class Currency { private String base; private String date; private Rate rate; public Currency() { } public Currency(String base, String date, Rate rate) { this.base = base; this.date = date; this.rate = rate; } public String getBase() { return base; } public void setBase(String base) { this.base = base; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public Rate getRate() { return rate; } public void setRate(Rate rate) { this.rate = rate; } }
Rate.java
public class Rate { private String USD, EUR, GBP, JPY, RUB, TRY; public Rate() { } public String getUSD() { return USD; } public void setUSD(String USD) { this.USD = USD; } public String getEUR() { return EUR; } public void setEUR(String EUR) { this.EUR = EUR; } public String getGBP() { return GBP; } public void setGBP(String GBP) { this.GBP = GBP; } public String getJPY() { return JPY; } public void setJPY(String JPY) { this.JPY = JPY; } public String getRUB() { return RUB; } public void setRUB(String RUB) { this.RUB = RUB; } public String getTRY() { return TRY; } public void setTRY(String TRY) { this.TRY = TRY; } }
Client example:
public class DecryptJson { public static void main(String[] args) { String encryptedJson = readJsonFromUrl("http://localhost:1717/currency/rates"); String json = decrypt(encryptedJson); System.out.println("encryptedJson: " + encryptedJson); System.out.println("decryptedJson: " + json); } private static String decrypt(String text) { BasicTextEncryptor textEncryptor = new BasicTextEncryptor(); textEncryptor.setPasswordCharArray(TestJasypt.getPassword()); String decryptedText = textEncryptor.decrypt(text); return decryptedText; } private static String readJsonFromUrl(String url) { StringBuilder sb = new StringBuilder(); try (InputStream is = new URL(url).openStream(); BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));) { int cp; while ((cp = rd.read()) != -1) sb.append((char) cp); } catch (Exception ex) { ex.printStackTrace(); } return sb.toString(); } }
Output:
encryptedJson: WMCWw+JeRmlcSXVodBTufNxGgaX4s80ZNkFDnuaYRExINOLoVbflrAKhVH97/aTsmq57PYZArUG+ioxvCZvpSU6kOlL52rktveBy6ozJP9Wm9h4zQAeldpcu3TYgYdKw+ctUyIR8kKjYudB4oieXP7UWUGo+NX3UWDIJ78fx8m7P4RwkC1Llxbem5hynAV+8FeppYl/5gX0L+o4up1f09DBDzvz1TYfdj7gwuIX3WmWkOuh5d92SLG5kgS1jE8Xn+tEz1Lc/y1LngxT9XGUM+B7zYEic+ZXpd204Yx13o3mwgue3weSolY/glbR9iZ0bZu5q4JkJ7WBnmE0N2W52qSle+M9yJ+FJ decryptedJson: [{"base":"USD","date":"2018-02-25","rate":{"EUR":"0.81307","GBP":"0.71502","JPY":"106.74","RUB":"56.466","TRY":"3.7869"}},{"base":"RUB","date":"2018-02-25","rate":{"USD":"0.01771","EUR":"0.014399","GBP":"0.012663","JPY":"1.8903","TRY":"0.067065"}}]
Github links of the above codes:
Other links about this topic: