Bonjour le forum,
j'ai une exception que je n'arrive pas à gérer et je sollicite donc votre aide.
Au démarage de mon appli, je créé/lie un fichier XML qui contient des préférences utilisateurs (Langue, chemin d'accès à des fichiers, mot de passe).
Le cryptage de mon fichier se fait bien mais je n'arrive pas à lire le flux crypté...
Pour ajouter un peu de Sécurité, je crypte ce fichier et lorsque j'essaye de le parser j'ai une exception qui est levée:
Voila le code de ma classe qui gère ce fichier XML :23 févr. 2009 15:17:08 snec.XmlConfig <init>
GRAVE: null
org.jdom.input.JDOMParseException: Error on line 1: Premature end of file.
at org.jdom.input.SAXBuilder.build(SAXBuilder.java:468)
at org.jdom.input.SAXBuilder.build(SAXBuilder.java:770)
at snec.XmlConfig.<init>(XmlConfig.java:80)
at snec.Initialisations.<init>(Initialisations.java:77)
at snec.Main.main(Main.java:26)
Caused by: org.xml.sax.SAXParseException: Premature end of file.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:174)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:388)
at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1411)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:1058)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:807)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:107)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
at org.jdom.input.SAXBuilder.build(SAXBuilder.java:453)
... 4 more
Caused by: org.xml.sax.SAXParseException: Premature end of file.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:174)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:388)
at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1411)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:1058)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:807)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:107)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
at org.jdom.input.SAXBuilder.build(SAXBuilder.java:453)
at org.jdom.input.SAXBuilder.build(SAXBuilder.java:770)
at snec.XmlConfig.<init>(XmlConfig.java:80)
at snec.Initialisations.<init>(Initialisations.java:77)
at snec.Main.main(Main.java:26)
Exception in thread "main" java.lang.NullPointerException
at java.util.ResourceBundle$CacheKey.calculateHashCode(ResourceBundle.java:588)
at java.util.ResourceBundle$CacheKey.<init>(ResourceBundle.java:517)
at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1197)
at java.util.ResourceBundle.getBundle(ResourceBundle.java:787)
at snec.language.Message.<init>(Message.java:21)
at snec.Main.main(Main.java:27)
Java Result: 1
Merci d'avance pour le coup de pouce,
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162 public class XmlConfig { /*################################################################## * Attributes ###################################################################*/ //Element ROOT of the xml file. static private Element ROOT = new Element("CONFIGURATION"); //The object Document is the xml file with a ROOT defined previously. static private Document XML_CONFIG = new Document(ROOT); //Configuration file path static String pathConfigFile = "." + File.separator + "config.xml"; //XML Configuration file static File configFile = new File(pathConfigFile); //The cipher key allowing the encryption private static Cipher encrypt_cipher; //The cipher key allowing the decryption private static Cipher decrypt_cipher; private boolean cryptic; /*################################################################## * Constructor ###################################################################*/ /** * Constructor which create the new xml file. */ public XmlConfig() { //Creation of the instance of saxbuilder. SAXBuilder Builder = new SAXBuilder(); //The parser has to check the DTD defined in the xmlfile. DTD id TO DO ! //NOW DTD IS NOT CHECK ! Put the validation option to true so as to check it. Builder.setValidation(false); //Create key to crypt configuration file createKeys(); //Create a configuration file if not exist. if (!configFile.exists()) { createXmlConfig(); } try { InputStream inStream = new FileInputStream(pathConfigFile); inStream = new CipherInputStream(inStream, decrypt_cipher); //Get the XML configuration file XML_CONFIG = Builder.build(inStream); ROOT = XML_CONFIG.getRootElement(); inStream.close(); } catch (JDOMException ex) { Logger.getLogger(XmlConfig.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(XmlConfig.class.getName()).log(Level.SEVERE, null, ex); } } /** * */ public static void createXmlConfig() { PrintWriter out = null; try { out = new PrintWriter(pathConfigFile, "utf-8"); } catch (FileNotFoundException ex) { Logger.getLogger(XmlConfig.class.getName()).log(Level.SEVERE, null, ex); } catch (UnsupportedEncodingException ex) { Logger.getLogger(XmlConfig.class.getName()).log(Level.SEVERE, null, ex); } //Language is initialize to Default: English out.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); out.println("<CONFIGURATION>"); out.println("\t<LANGUAGE>snec.language.DefaultMessage</LANGUAGE>"); out.println("</CONFIGURATION>"); out.close(); //Open a stream of the config.ini file OutputStream outStream; try { outStream = new FileOutputStream(pathConfigFile); outStream = new CipherOutputStream(outStream, encrypt_cipher); outStream.close(); } catch (IOException ex) { Logger.getLogger(XmlConfig.class.getName()).log(Level.SEVERE, null, ex); } } /** * Get a TAG value from XML configuration file * @param key Tag name * @return value of the Tag you want to get */ public static String getConfigValue(String key) { Element keyTagElement = ROOT.getChild(key); List<Element> listTagElement = ROOT.getChildren(); //Get value if TAG exist in configuration file or return null if (listTagElement.contains(keyTagElement)) { return ROOT.getChild(key).getText(); } else { return null; } } public static void modifyConfig(String key, String value) { Element keyElement = new Element(key); Iterator IteratorElement = ROOT.getChildren().iterator(); int i = 0, index = 0; boolean found = false; while (IteratorElement.hasNext() && index == 0) { Element el = (Element) IteratorElement.next(); if (el.getName().equals(key)) { IteratorElement.remove(); index = i; found = true; } i++; } if (!found) { index = ROOT.getChildren().size(); } List<Element> listE = ROOT.getChildren(); listE.add(index++, keyElement); keyElement.setText(value); saveXml(); } /** * This class function save the Document in a xml file. */ public static void saveXml() { OutputStream outStream; try { XMLOutputter out = new XMLOutputter(Format.getPrettyFormat()); FileOutputStream fos = new FileOutputStream(pathConfigFile); out.output(XML_CONFIG, fos); fos.close(); //Open a stream of the config.ini file //outStream = new FileOutputStream(pathConfigFile); //outStream = new CipherOutputStream(outStream, encrypt_cipher); //outStream.close(); } catch (java.io.IOException e) { e.printStackTrace(); } } private static void createKeys() { String algo = "AES"; //password = "SneCtEaM15092008" byte[] password = new byte[]{0x53, 0x6E, 0x65, 0x43, 0x74, 0x45, 0x61, 0x4D, 0x31, 0x35, 0x30, 0x39, 0x32, 0x30, 0x30, 0x38}; Key key = new SecretKeySpec(password, algo); try { encrypt_cipher = Cipher.getInstance(algo); decrypt_cipher = Cipher.getInstance(algo); encrypt_cipher.init(Cipher.ENCRYPT_MODE, key); decrypt_cipher.init(Cipher.DECRYPT_MODE, key); } catch (Exception ex) { ex.printStackTrace(); } } }
Bap
Partager