Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在xml文件的节点中存储加密值(对于ex-ðçM¸½§J·h°P¥«ahæ8225; i»Uçñ&>9ó?)_Java_Xml_Encryption - Fatal编程技术网

Java 如何在xml文件的节点中存储加密值(对于ex-ðçM¸½§J·h°P¥«ahæ8225; i»Uçñ&>9ó?)

Java 如何在xml文件的节点中存储加密值(对于ex-ðçM¸½§J·h°P¥«ahæ8225; i»Uçñ&>9ó?),java,xml,encryption,Java,Xml,Encryption,我使用以下代码在java中使用AES密钥进行加密和解密- import java.nio.charset.Charset; import java.security.GeneralSecurityException; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; public class EncryptionTest {

我使用以下代码在java中使用AES密钥进行加密和解密-

import java.nio.charset.Charset;
import java.security.GeneralSecurityException;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class EncryptionTest {

  public static void main(String[] args) {
    try {

      String key = "ThisIsASecretKey";
      byte[] ciphertext = encrypt(key, "1234567890123456");
      System.out.println( new String(ciphertext));
      System.out.println("decrypted value:" + (decrypt(key, ciphertext)));

    } catch (GeneralSecurityException e) {
      e.printStackTrace();
    }
  }

  public static byte[] encrypt(String key, String value)
      throws GeneralSecurityException {

    byte[] raw = key.getBytes(Charset.forName("US-ASCII"));
    if (raw.length != 16) {
      throw new IllegalArgumentException("Invalid key size.");
    }

    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec,
        new IvParameterSpec(new byte[16]));
    return cipher.doFinal(value.getBytes(Charset.forName("US-ASCII")));
  }

  public static String decrypt(String key, byte[] encrypted)
      throws GeneralSecurityException {

    byte[] raw = key.getBytes(Charset.forName("US-ASCII"));
    if (raw.length != 16) {
      throw new IllegalArgumentException("Invalid key size.");
    }
    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");

    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, skeySpec,
        new IvParameterSpec(new byte[16]));
    byte[] original = cipher.doFinal(encrypted);

    return new String(original, Charset.forName("US-ASCII"));
  }
}
现在我想将此代码返回的加密值存储在xml文件的一个节点中。 由于此代码块将加密值返回为-M¸½§J·h°P¥«ahæ8225; i»Uçñ&>9ó

当我尝试使用Java代码从XML文件中存储和/或检索该值时,它会给出异常。 像- com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException:1字节UTF-8序列的字节1无效


请建议如何使用Java代码从XML文件中检索此类值。

将其编码为base64或十六进制,即值ðçM¸½§J·h°p¥«ahæ8225; i»Uçñ&>9ó?是否可以使用Java代码添加/检索xml文件?