-
Notifications
You must be signed in to change notification settings - Fork 389
/
AbstractCrypt.java
178 lines (162 loc) · 4.25 KB
/
AbstractCrypt.java
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.wicket.util.crypt;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import javax.crypto.Cipher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Abstract base class for JCE based ICrypt implementations.
*
* @author Juergen Donnerstag
*/
public abstract class AbstractCrypt implements ICrypt
{
/** Default encryption key */
private static final String DEFAULT_ENCRYPTION_KEY = "WiCkEt-CrYpT";
/** Encoding used to convert java String from and to byte[] */
private static final String CHARACTER_ENCODING = "UTF-8";
/** Log. */
private static final Logger log = LoggerFactory.getLogger(AbstractCrypt.class);
/** Key used to de-/encrypt the data */
private String encryptionKey = DEFAULT_ENCRYPTION_KEY;
/**
* Constructor
*/
public AbstractCrypt()
{
}
/**
* Decrypts a string into a string.
*
* @param text
* text to decrypt
* @return the decrypted text
*/
@Override
public final String decryptUrlSafe(final String text)
{
try
{
byte[] decoded = new Base64(true).decode(text);
return new String(decryptByteArray(decoded), CHARACTER_ENCODING);
}
catch (Exception ex)
{
log.debug("Error decoding text: " + text, ex);
return null;
}
}
/**
* Encrypt a string into a string using URL safe Base64 encoding.
*
* @param plainText
* text to encrypt
* @return encrypted string
*/
@Override
public final String encryptUrlSafe(final String plainText)
{
try
{
byte[] encrypted = encryptStringToByteArray(plainText);
return new String(new Base64(-1, null, true).encode(encrypted), CHARACTER_ENCODING);
}
catch (GeneralSecurityException e)
{
log.error("Unable to encrypt text '" + plainText + "'", e);
return null;
}
catch (UnsupportedEncodingException e)
{
log.error("Unable to encrypt text '" + plainText + "'", e);
return null;
}
}
/**
* Get encryption private key
*
* @return encryption private key
*/
public String getKey()
{
return encryptionKey;
}
/**
* Set encryption private key
*
* @param key
* private key to make de-/encryption unique
*/
@Override
public void setKey(final String key)
{
encryptionKey = key;
}
/**
* Crypts the given byte array
*
* @param input
* byte array to be crypted
* @param mode
* crypt mode
* @return the input crypted. Null in case of an error
* @throws GeneralSecurityException
*/
protected abstract byte[] crypt(final byte[] input, final int mode)
throws GeneralSecurityException;
/**
* Decrypts an encrypted, but Base64 decoded byte array into a byte array.
*
* @param encrypted
* byte array to decrypt
* @return the decrypted text
*/
private byte[] decryptByteArray(final byte[] encrypted)
{
try
{
return crypt(encrypted, Cipher.DECRYPT_MODE);
}
catch (GeneralSecurityException e)
{
throw new RuntimeException(
"Unable to decrypt the text '" + new String(encrypted) + "'", e);
}
}
/**
* Encrypts the given text into a byte array.
*
* @param plainText
* text to encrypt
* @return the string encrypted
* @throws GeneralSecurityException
*/
private byte[] encryptStringToByteArray(final String plainText)
throws GeneralSecurityException
{
try
{
return crypt(plainText.getBytes(CHARACTER_ENCODING), Cipher.ENCRYPT_MODE);
}
catch (UnsupportedEncodingException ex)
{
throw new RuntimeException(ex.getMessage());
}
}
}