Xml encryption and decryption

I am creating an application in which I have to encrypt xml from my side and send it to the server, and in return I will get xml, and I have to decrypt it. I am not going to encrypt and decrypt. My code is as follows

<?xml version='1.0' encoding='utf-8'?><adm_auth_req><user_name>user.s7</user_name><password>gspcsmo</password></adm_auth_req> 

I use this code to encrypt and decrypt it

 public string encryptData(string key, string data) { int keyLen = key.Length; int dataLen = Convert.ToInt16(data.Length); char chData; char chKey; char[] data1 = data.ToCharArray(); char[] key1 = key.ToCharArray(); StringBuilder encryptedData = new StringBuilder(); for (int i = 0; i < dataLen; i++) { chData = data1[i]; for (int j = 0; j < keyLen; j++) { chKey = key1[j]; chData = (char)(chData ^ chKey); } encryptedData.Append(chData); } return (encryptedData.ToString()); } 

But still, all in vain. Can someone tell me how to encrypt it and decrypt the result?

+4
source share
5 answers

I used the DES algorithm for encryption and decryption.

For encryption: here after encryption I write a file to save. You can save it with a different name (temp) and send it to the server. After sending successfully, you can delete this encrypted file.

  FileOutputStream fos = null ; CipherInputStream cis; byte key[] = "abcdEFGH".getBytes(); SecretKeySpec secretKey = new SecretKeySpec(key,"DES"); Cipher encrypt = Cipher.getInstance("DES/ECB/PKCS5Padding"); encrypt.init(Cipher.ENCRYPT_MODE, secretKey); InputStream fis = new ByteArrayInputStream(fileData);//Here I am getting file data as byte array. You can convert your file data to InputStream by other way too. File dataFile = new File(dataDir,fileName); //dataDir is location where my file is stored if(!dataFile.exists()){ cis = new CipherInputStream(fis,encrypt); try { fos = new FileOutputStream(dataFile); byte[] b = new byte[8]; int i; while ((i=cis.read(b)) != -1) { fos.write(b, 0, i); } return fileName; } finally{ try { if(fos != null) { fos.flush(); fos.close(); } cis.close(); fis.close(); } catch (IOException e) { //IOException } } } return ""; 

To decrypt:

  CipherInputStream cis; FileOutputStream fos = null; FileInputStream fis = null; File dataFile = new File(dataDir,fileName); // here I am getting encrypted file from server File newDataFile = new File(dataDir,fileName+"_TEMP"); // I am creating temporary decrypted file byte key[] = "abcdEFGH".getBytes(); SecretKeySpec secretKey = new SecretKeySpec(key,"DES"); Cipher decrypt = Cipher.getInstance("DES/ECB/PKCS5Padding"); decrypt.init(Cipher.DECRYPT_MODE, secretKey); try { fis = new FileInputStream(dataFile); } catch(Exception e) { //Exception } if(dataFile.exists()){ cis = new CipherInputStream(fis,decrypt); try { fos = new FileOutputStream(newDataFile); byte[] b = new byte[8]; int i; while ((i=cis.read(b)) != -1) { fos.write(b, 0, i); } return newDataFile; } finally{ try { if(fos != null) { fos.flush(); fos.close(); } cis.close(); fis.close(); } catch (IOException e) { //IOException } } } 
+1
source

What is the problem you are solving?

Maybe SSL is right for you? Out of the box encryption, standard solution.

You can also watch JCA . But I think it will be too difficult a solution for your problem.

+3
source

Why don't you use Twofish for this, XML is text, and all you need to use is an algorithm, and you can find many examples for this.

+1
source

In my opinion, you should not try to implement your own algorithm, you first invent the wheel, and secondly, it probably will not be next to security, like other more standard encryption procedures. If I were you, I would consider some good Java encryption libraries. I found here, http://www.bouncycastle.org/latest_releases.html

+1
source

There are already answers to SO that can answer you.

Encrypt and decrypt string in java

How to encrypt String in Java

+1
source

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


All Articles