use of org.xel.crypto.EncryptedData in project elastic-core-maven by OrdinaryDude.
the class DecryptFrom method processRequest.
@Override
protected JSONStreamAware processRequest(HttpServletRequest req) throws NxtException {
byte[] publicKey = Account.getPublicKey(ParameterParser.getAccountId(req, true));
if (publicKey == null) {
return INCORRECT_ACCOUNT;
}
String secretPhrase = ParameterParser.getSecretPhrase(req, true);
byte[] data = Convert.parseHexString(Convert.nullToEmpty(req.getParameter("data")));
byte[] nonce = Convert.parseHexString(Convert.nullToEmpty(req.getParameter("nonce")));
EncryptedData encryptedData = new EncryptedData(data, nonce);
boolean isText = !"false".equalsIgnoreCase(req.getParameter("decryptedMessageIsText"));
boolean uncompress = !"false".equalsIgnoreCase(req.getParameter("uncompressDecryptedMessage"));
try {
byte[] decrypted = Account.decryptFrom(publicKey, encryptedData, secretPhrase, uncompress);
JSONObject response = new JSONObject();
response.put("decryptedMessage", isText ? Convert.toString(decrypted) : Convert.toHexString(decrypted));
return response;
} catch (RuntimeException e) {
Logger.logDebugMessage(e.toString());
return DECRYPTION_FAILED;
}
}
use of org.xel.crypto.EncryptedData in project elastic-core-maven by OrdinaryDude.
the class ParameterParser method getEncryptToSelfMessage.
public static Appendix.EncryptToSelfMessage getEncryptToSelfMessage(HttpServletRequest req) throws ParameterException {
boolean isText = !"false".equalsIgnoreCase(req.getParameter("messageToEncryptToSelfIsText"));
boolean compress = !"false".equalsIgnoreCase(req.getParameter("compressMessageToEncryptToSelf"));
byte[] plainMessageBytes = null;
EncryptedData encryptedData = ParameterParser.getEncryptedData(req, "encryptToSelfMessage");
if (encryptedData == null) {
String plainMessage = Convert.emptyToNull(req.getParameter("messageToEncryptToSelf"));
if (plainMessage == null) {
return null;
}
try {
plainMessageBytes = isText ? Convert.toBytes(plainMessage) : Convert.parseHexString(plainMessage);
} catch (RuntimeException e) {
throw new ParameterException(INCORRECT_MESSAGE_TO_ENCRYPT);
}
String secretPhrase = getSecretPhrase(req, false);
if (secretPhrase != null) {
byte[] publicKey = Crypto.getPublicKey(secretPhrase);
encryptedData = Account.encryptTo(publicKey, plainMessageBytes, secretPhrase, compress);
}
}
if (encryptedData != null) {
return new Appendix.EncryptToSelfMessage(encryptedData, isText, compress);
} else {
return new Appendix.UnencryptedEncryptToSelfMessage(plainMessageBytes, isText, compress);
}
}
Aggregations