加密和解密xml

时间:2011-12-06 05:46:55

标签: java android xml encryption

我正在创建一个应用程序,我必须从我身边加密xml并将其发送到服务器,作为响应,我将收到xml,我必须解密它。我不知道加密和解密。我的代码如下

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

我正在使用此代码对其进行加密和解密

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());
}

但仍然徒劳无功。谁能告诉我如何加密它并解密结果?

5 个答案:

答案 0 :(得分:3)

你要解决的问题是什么?

也许SSL与您匹配? 开箱即用的加密,标准解决方案。

您也可以查看JCA。但我想,对你的问题来说,这将是一个太重的解决方案。

答案 1 :(得分:1)

为什么不使用Twofish,XML就是Text,你需要使用的只是算法,你可以找到很多examples

答案 2 :(得分:1)

在我看来,你不应该尝试首先实现自定义算法,你要重新发明轮子,其次可能不会像其他更标准的加密例程那样安全。如果我是你,我会去看看一些优秀的Java加密库。我找到的就是http://www.bouncycastle.org/latest_releases.html

答案 3 :(得分:1)

在SO上已经有一些答案可能适合您。

Encrypt and decrypt a String in java

How to encrypt String in Java

答案 4 :(得分:1)

我使用DES算法进行加密和解密。

加密:加密后,我正在写文件保存。您可以使用其他(临时)名称保存它并将其发送到服务器。成功发送后,您可以删除此加密文件

    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 "";

解密:

    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
            }
        }
    }