How To Encode Too Decode A Json Encrypted String Inwards Java

This tutorial requires the operate of apache park as well as jackson libraries. For instance I convey a highly confidential string as well as I desire to shipping it over the net, of class unremarkably I desire to encrypt it. And 1 of the method nosotros tin shipping this encrypted string over the cyberspace is past times JSON format.

Here's how I did it (to brand things easier I posted my code):


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  <modelVersion>4.0.0</modelVersion>  <groupId>encryptionDemo</groupId>  <artifactId>encryptionDemo</artifactId>  <version>0.0.1-SNAPSHOT</version>  <build>   <sourceDirectory>src</sourceDirectory>   <plugins>    <plugin>     <artifactId>maven-compiler-plugin</artifactId>     <version>2.3.2</version>     <configuration>      <source>1.6</source>      <target>1.6</target>     </configuration>    </plugin>   </plugins>  </build>   <dependencies>   <dependency>    <groupId>commons-codec</groupId>    <artifactId>commons-codec</artifactId>    <version>1.4</version>   </dependency>   <dependency>    <groupId>com.fasterxml.jackson.core</groupId>    <artifactId>jackson-core</artifactId>    <version>2.0.4</version>   </dependency>   <dependency>    <groupId>com.fasterxml.jackson.core</groupId>    <artifactId>jackson-databind</artifactId>    <version>2.0.4</version>   </dependency>   <dependency>    <groupId>com.fasterxml.jackson.core</groupId>    <artifactId>jackson-annotations</artifactId>    <version>2.0.4</version>   </dependency>  </dependencies> </project> 

JSONUtils, operate to encode as well as decode a string:

package com.ipiel.utils;  import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException;  import com.fasterxml.jackson.core.JsonGenerationException; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper;  world class JSONUtils {  static ObjectMapper mapper = novel ObjectMapper();   world static String toJSON(Object o) {   String outcome = "";   if (o != null) {    ByteArrayOutputStream out = novel ByteArrayOutputStream();    endeavor {     mapper.writeValue(out, o);    } grab (JsonGenerationException e) {     e.printStackTrace();    } grab (JsonMappingException e) {     e.printStackTrace();    } grab (IOException e) {     e.printStackTrace();    }    endeavor {     outcome = out.toString("UTF-8");    } grab (UnsupportedEncodingException e) {     e.printStackTrace();    }   }   supply result;  }   world static  T parseJSON(String jsonString, Class beanClass)    throws JsonParseException, JsonMappingException, IOException {   supply mapper.readValue(jsonString, beanClass);  } } 

And finally, the class that encrypts as well as decrypts the string:

package com.ipiel.utils;  import java.beans.IntrospectionException; import java.io.IOException; import java.security.InvalidKeyException;  import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec;  import org.apache.commons.codec.binary.Base64;  import com.ipiel.models.Address; import com.ipiel.models.Student;  world class IpielContextParser {  Cipher cipher;  SecretKey myDesKey;   world IpielContextParser(String secretkey) {   endeavor {    DESKeySpec dks = novel DESKeySpec(secretkey.getBytes());    SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");    myDesKey = skf.generateSecret(dks);    cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");   } grab (Exception e) {    e.printStackTrace();   }  }   world String encodeBeanToString(Student student)    throws IllegalBlockSizeException, BadPaddingException,    InvalidKeyException {   String outcome = null;   String jsonString = JSONUtils.toJSON(student);   byte[] text = jsonString.getBytes();   cipher.init(Cipher.ENCRYPT_MODE, myDesKey);   byte[] textEncrypted = cipher.doFinal(text);   outcome = Base64.encodeBase64String(textEncrypted);    supply result;  }   world Student decodeStringToBean(String param) throws IOException,    InvalidKeyException, IllegalBlockSizeException,    BadPaddingException, IntrospectionException {   byte[] decodedBytes = Base64.decodeBase64(param);   cipher.init(Cipher.DECRYPT_MODE, myDesKey);   byte[] textDecrypted = cipher.doFinal(decodedBytes);    supply JSONUtils.parseJSON(new String(textDecrypted), Student.class);  }   someone void test() {   Address address = novel Address();   address.setCountry("PH");   address.setZip("4030");    Student pupil = novel Student();   student.setAddress(address);   student.setName("Ipiel");   student.setAge(27);    endeavor {    String encoded = encodeBeanToString(student);    System.out.println("encoded: " + encoded);    Student decoded = decodeStringToBean(encoded);    System.out.println("decoded: " + decoded);   } grab (Exception e) {    }  }   world static void main(String args[]) {   novel IpielContextParser("yxvuicidunccxnxzquidxstraitsdxsunmaitrxadxtruityharmunix").test();  } } 
Next
Previous
Click here for Comments

0 komentar:

Please comment if there are any that need to be asked.