-
Notifications
You must be signed in to change notification settings - Fork 52
/
EncryptUtils.java
96 lines (84 loc) · 3.06 KB
/
EncryptUtils.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
/*
* Copyright (c) 2014-2021 Weasis Team and other contributors.
*
* This program and the accompanying materials are made available under the terms of the Eclipse
* Public License 2.0 which is available at http://www.eclipse.org/legal/epl-2.0, or the Apache
* License, Version 2.0 which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*/
package org.weasis.util;
import java.util.Arrays;
import java.util.Base64;
/**
* @author Nicolas Roduit
*/
public class EncryptUtils {
private static final char START = '~';
private static final int KEY_MIN_SIZE = 10; // inclusive
private static final int KEY_MAX_SIZE = 30; // inclusive
private static final String ERROR_MSG = "message or key arguments cannot be null!";
private EncryptUtils() {}
public static String encrypt(String message, String key) {
if (message == null || key == null) {
throw new IllegalArgumentException(ERROR_MSG);
}
String result = xorMessage(message.trim(), key);
return Base64.getUrlEncoder().encodeToString(result.getBytes());
}
private static String xorMessage(String message, String key) {
if (message == null || key == null) {
throw new IllegalArgumentException(ERROR_MSG);
}
char[] mChars = message.toCharArray();
char[] kChars = key.toCharArray();
if (mChars.length < 1) {
throw new IllegalArgumentException("Cannot encode empty message!");
}
if (kChars.length < KEY_MIN_SIZE || kChars.length > KEY_MAX_SIZE) {
throw new IllegalArgumentException(
"key size must be >= " + KEY_MIN_SIZE + " and <= " + KEY_MAX_SIZE + " characters");
}
int kl = kChars.length;
int ml = mChars.length;
// If key is longer than message, take the key length for result
char[] newmsg = new char[kl > ml ? kl : ml];
for (int i = 0; i < newmsg.length; i++) {
newmsg[i] = (char) (mChars[i % ml] ^ kChars[i % kl]);
}
if (ml < kl) {
newmsg[ml] = (char) (START ^ kChars[ml]);
}
return new String(newmsg);
}
public static String decrypt(String message, String key) {
if (message == null || key == null) {
throw new IllegalArgumentException(ERROR_MSG);
}
String result = new String(Base64.getUrlDecoder().decode(message.trim()));
return unXorMessage(result, key);
}
private static String unXorMessage(String message, String key) {
if (message == null || key == null) {
throw new IllegalArgumentException(ERROR_MSG);
}
char[] mChars = message.toCharArray();
char[] kChars = key.toCharArray();
int kl = kChars.length;
int ml = mChars.length;
// If key is longer than message, take the key length for result
char[] newmsg = new char[kl > ml ? kl : ml];
int cutMessage = -1;
for (int i = 0; i < newmsg.length; i++) {
newmsg[i] = (char) (mChars[i % ml] ^ kChars[i % kl]);
if (newmsg[i] == START) {
cutMessage = i;
break;
}
}
if (cutMessage > 0) {
newmsg = Arrays.copyOfRange(newmsg, 0, cutMessage);
}
return new String(newmsg);
}
}