Я использую DES для шифрования / дешифрования, так как это не рекомендуется, но это старый код, поэтому я не могу перейти на AES, теперь мой код работает нормально в локальной среде (например, mac) с производственной базой данных, также работает нормальнона UAT, который является дистрибутивом Linux, основанным на SUSE, но дешифрование не работает на Production, основанном на redhat.при производстве выдается «Длина ввода (с отступом), не кратная 8 байтам». Недопустимое исключение размера блока
@Service
public class EncryptionUtil {
private static final Logger log = LogManager.getLogger(EncryptionUtil.class);
@Autowired
GpsCacheManager gpsCacheManager;
private Cipher ecipher;
private Cipher dcipher;
@Autowired
private StringUtils stringUtils;
public EncryptionUtil() throws Exception {
ecipher = Cipher.getInstance("DES");
dcipher = Cipher.getInstance("DES");
initCipher();
}
private void initCipher() {
try {
String response = “[-3232, -34, -98, 111, -222, 33, -22, 55]”;
String[] byteValues = response.substring(1, response.length() - 1).split(",");
byte[] bytes = new byte[byteValues.length];
for (int i = 0, len = bytes.length; i < len; i++) {
bytes[i] = Byte.parseByte(byteValues[i].trim());
}
SecretKey key = new SecretKeySpec(bytes, "DES");
ecipher.init(Cipher.ENCRYPT_MODE, key);
dcipher.init(Cipher.DECRYPT_MODE, key);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
public String encryptUTF8(String str) throws Exception {
// Encode the string into bytes using utf-8
byte[] utf8 = str.getBytes("UTF8");
// Encrypt
byte[] enc = ecipher.doFinal(utf8);
// Encode bytes to base64 to get a string
return new String(Base64.encodeBase64(enc));
}
public String decryptUTF8(String str) throws Exception {
if (stringUtils == null) {
stringUtils = new StringUtils();
}
//do not decrypt if a valid email.
if (stringUtils.isValidEmail(str)) {
return str;
}
// Decode base64 to get bytes
byte[] dec = Base64.decodeBase64(str.getBytes());
byte[] utf8 = null;
try {
utf8 = dcipher.doFinal(dec);
} catch (IllegalBlockSizeException e) {
return str;
}
// Decode using utf-8
return new String(utf8, "UTF8");
}
}