use of org.xel.crypto.EncryptedData in project elastic-core-maven by OrdinaryDude.
the class EncryptTo method processRequest.
@Override
protected JSONStreamAware processRequest(HttpServletRequest req) throws NxtException {
long recipientId = ParameterParser.getAccountId(req, "recipient", true);
byte[] recipientPublicKey = Account.getPublicKey(recipientId);
if (recipientPublicKey == null) {
return INCORRECT_RECIPIENT;
}
boolean isText = !"false".equalsIgnoreCase(req.getParameter("messageToEncryptIsText"));
boolean compress = !"false".equalsIgnoreCase(req.getParameter("compressMessageToEncrypt"));
String plainMessage = Convert.emptyToNull(req.getParameter("messageToEncrypt"));
if (plainMessage == null) {
return MISSING_MESSAGE_TO_ENCRYPT;
}
byte[] plainMessageBytes;
try {
plainMessageBytes = isText ? Convert.toBytes(plainMessage) : Convert.parseHexString(plainMessage);
} catch (RuntimeException e) {
return INCORRECT_MESSAGE_TO_ENCRYPT;
}
String secretPhrase = ParameterParser.getSecretPhrase(req, true);
EncryptedData encryptedData = Account.encryptTo(recipientPublicKey, plainMessageBytes, secretPhrase, compress);
return JSONData.encryptedData(encryptedData);
}
use of org.xel.crypto.EncryptedData in project elastic-core-maven by OrdinaryDude.
the class MessageEncryptionTest method encryptBytes.
@Test
public void encryptBytes() {
byte[] data = { (byte) 0x01, (byte) 0x02, (byte) 0xF1, (byte) 0xF2 };
EncryptedData encryptedData = encrypt(data);
Assert.assertArrayEquals(data, decrypt(encryptedData));
}
use of org.xel.crypto.EncryptedData in project elastic-core-maven by OrdinaryDude.
the class SendMessageTest method sendClientEncryptedMessage.
@Test
public void sendClientEncryptedMessage() {
EncryptedData encryptedData = BOB.getAccount().encryptTo(Convert.toBytes("hello world"), ALICE.getSecretPhrase(), true);
JSONObject response = new org.xel.http.APICall.Builder("sendMessage").param("secretPhrase", ALICE.getSecretPhrase()).param("recipient", BOB.getStrId()).param("encryptedMessageData", Convert.toHexString(encryptedData.getData())).param("encryptedMessageNonce", Convert.toHexString(encryptedData.getNonce())).param("feeNQT", 0).build().invoke();
Logger.logDebugMessage("sendMessage: " + response);
String transaction = (String) response.get("transaction");
JSONObject attachment = (JSONObject) ((JSONObject) response.get("transactionJSON")).get("attachment");
JSONObject encryptedMessage = (JSONObject) attachment.get("encryptedMessage");
Assert.assertNotSame(64, ((String) encryptedMessage.get("data")).length());
Assert.assertNotSame(32, ((String) encryptedMessage.get("nonce")).length());
generateBlock();
response = new org.xel.http.APICall.Builder("readMessage").param("secretPhrase", BOB.getSecretPhrase()).param("transaction", transaction).build().invoke();
Logger.logDebugMessage("readMessage: " + response);
Assert.assertEquals("hello world", response.get("decryptedMessage"));
}
use of org.xel.crypto.EncryptedData in project elastic-core-maven by OrdinaryDude.
the class JSONData method prunableMessage.
static JSONObject prunableMessage(PrunableMessage prunableMessage, String secretPhrase, byte[] sharedKey) {
JSONObject json = new JSONObject();
json.put("transaction", Long.toUnsignedString(prunableMessage.getId()));
if (prunableMessage.getMessage() == null || prunableMessage.getEncryptedData() == null) {
json.put("isText", prunableMessage.getMessage() != null ? prunableMessage.messageIsText() : prunableMessage.encryptedMessageIsText());
}
putAccount(json, "sender", prunableMessage.getSenderId());
if (prunableMessage.getRecipientId() != 0) {
putAccount(json, "recipient", prunableMessage.getRecipientId());
}
json.put("transactionTimestamp", prunableMessage.getTransactionTimestamp());
json.put("blockTimestamp", prunableMessage.getBlockTimestamp());
EncryptedData encryptedData = prunableMessage.getEncryptedData();
if (encryptedData != null) {
json.put("encryptedMessage", encryptedData(prunableMessage.getEncryptedData()));
json.put("encryptedMessageIsText", prunableMessage.encryptedMessageIsText());
byte[] decrypted = null;
try {
if (secretPhrase != null) {
decrypted = prunableMessage.decrypt(secretPhrase);
} else if (sharedKey != null && sharedKey.length > 0) {
decrypted = prunableMessage.decrypt(sharedKey);
}
if (decrypted != null) {
json.put("decryptedMessage", Convert.toString(decrypted, prunableMessage.encryptedMessageIsText()));
}
} catch (RuntimeException e) {
putException(json, e, "Decryption failed");
}
json.put("isCompressed", prunableMessage.isCompressed());
}
if (prunableMessage.getMessage() != null) {
json.put("message", Convert.toString(prunableMessage.getMessage(), prunableMessage.messageIsText()));
json.put("messageIsText", prunableMessage.messageIsText());
}
return json;
}
use of org.xel.crypto.EncryptedData in project elastic-core-maven by OrdinaryDude.
the class ParameterParser method getEncryptedData.
public static EncryptedData getEncryptedData(HttpServletRequest req, String messageType) throws ParameterException {
String dataString = Convert.emptyToNull(req.getParameter(messageType + "Data"));
String nonceString = Convert.emptyToNull(req.getParameter(messageType + "Nonce"));
if (nonceString == null) {
return null;
}
byte[] data;
byte[] nonce;
try {
nonce = Convert.parseHexString(nonceString);
} catch (RuntimeException e) {
throw new ParameterException(JSONResponses.incorrect(messageType + "Nonce"));
}
if (dataString != null) {
try {
data = Convert.parseHexString(dataString);
} catch (RuntimeException e) {
throw new ParameterException(JSONResponses.incorrect(messageType + "Data"));
}
} else {
if (req.getContentType() == null || !req.getContentType().startsWith("multipart/form-data")) {
return null;
}
try {
Part part = req.getPart(messageType + "File");
if (part == null) {
return null;
}
FileData fileData = new FileData(part).invoke();
data = fileData.getData();
} catch (IOException | ServletException e) {
Logger.logDebugMessage("error in reading file data", e);
throw new ParameterException(JSONResponses.incorrect(messageType + "File"));
}
}
return new EncryptedData(data, nonce);
}
Aggregations