File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\util\Protector.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.util;
25
26 import javax.crypto.SecretKey;
27 import javax.crypto.Cipher;
28 import javax.crypto.KeyGenerator;
29 import java.io.IOException;
30 import java.io.UnsupportedEncodingException;
31 import java.security.GeneralSecurityException;
32 import java.security.NoSuchAlgorithmException;
33
34 import com.trilead.ssh2.crypto.Base64;
35
36 /**
37 * Encrypt/decrypt data by using a "session" key that only lasts for
38 * the duration of the server instance.
39 *
40 * @author Kohsuke Kawaguchi
41 * @see Scrambler
42 * @since 1.162
43 */
44 public class Protector {
45 private static final String ALGORITHM = "DES";
46 private static final String MAGIC = ":::";
47
48 public static String protect(String secret) {
49 try {
50 Cipher cipher = Secret.getCipher(ALGORITHM);
51 cipher.init(Cipher.ENCRYPT_MODE, DES_KEY);
52 return new String(Base64.encode(cipher.doFinal((secret+ MAGIC).getBytes("UTF-8"))));
53 } catch (GeneralSecurityException e) {
54 throw new Error(e); // impossible
55 } catch (UnsupportedEncodingException e) {
56 throw new Error(e); // impossible
57 }
58 }
59
60 /**
61 * Returns null if fails to decrypt properly.
62 */
63 public static String unprotect(String data) {
64 if(data==null) return null;
65 try {
66 Cipher cipher = Secret.getCipher(ALGORITHM);
67 cipher.init(Cipher.DECRYPT_MODE, DES_KEY);
68 String plainText = new String(cipher.doFinal(Base64.decode(data.toCharArray())), "UTF-8");
69 if(plainText.endsWith(MAGIC))
70 return plainText.substring(0,plainText.length()-3);
71 return null;
72 } catch (GeneralSecurityException e) {
73 return null;
74 } catch (UnsupportedEncodingException e) {
75 throw new Error(e); // impossible
76 } catch (IOException e) {
77 return null;
78 }
79 }
80
81 private static final SecretKey DES_KEY;
82
83 static {
84 try {
85 DES_KEY = KeyGenerator.getInstance(ALGORITHM).generateKey();
86 } catch (NoSuchAlgorithmException e) {
87 throw new Error(e);
88 }
89 }
90 }
91