有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java加密和解密xml

我正在制作一个应用程序,在这个应用程序中,我必须从我这边加密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) 个答案

  1. # 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
                }
            }
        }
    
  2. # 3 楼答案

    为什么不使用Twofish呢,XML是文本,只需要使用算法,就可以找到很多examples用于此

  3. # 4 楼答案

    你在解决什么问题

    也许SSL和你匹配? 即时加密,标准解决方案

    你也可以看看JCA。但我认为,对于你的问题来说,这将是一个沉重的解决方案

  4. # 5 楼答案

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