Search in sources :

Example 6 with EncryptedData

use of org.xel.crypto.EncryptedData in project elastic-core-maven by OrdinaryDude.

the class ParameterParser method getEncryptedMessage.

public static Appendix getEncryptedMessage(HttpServletRequest req, Account recipient, boolean prunable) throws ParameterException {
    boolean isText = !"false".equalsIgnoreCase(req.getParameter("messageToEncryptIsText"));
    boolean compress = !"false".equalsIgnoreCase(req.getParameter("compressMessageToEncrypt"));
    byte[] plainMessageBytes = null;
    byte[] recipientPublicKey = null;
    EncryptedData encryptedData = ParameterParser.getEncryptedData(req, "encryptedMessage");
    if (encryptedData == null) {
        String plainMessage = Convert.emptyToNull(req.getParameter("messageToEncrypt"));
        if (plainMessage == null) {
            if (req.getContentType() == null || !req.getContentType().startsWith("multipart/form-data")) {
                return null;
            }
            try {
                Part part = req.getPart("messageToEncryptFile");
                if (part == null) {
                    return null;
                }
                FileData fileData = new FileData(part).invoke();
                plainMessageBytes = fileData.getData();
                String detectedMimeType = Search.detectMimeType(plainMessageBytes);
                if (detectedMimeType != null) {
                    isText = detectedMimeType.startsWith("text/");
                }
                if (isText && !Arrays.equals(plainMessageBytes, Convert.toBytes(Convert.toString(plainMessageBytes)))) {
                    isText = false;
                }
            } catch (IOException | ServletException e) {
                Logger.logDebugMessage("error in reading file data", e);
                throw new ParameterException(INCORRECT_MESSAGE_TO_ENCRYPT);
            }
        } else {
            try {
                plainMessageBytes = isText ? Convert.toBytes(plainMessage) : Convert.parseHexString(plainMessage);
            } catch (RuntimeException e) {
                throw new ParameterException(INCORRECT_MESSAGE_TO_ENCRYPT);
            }
        }
        if (recipient != null) {
            recipientPublicKey = Account.getPublicKey(recipient.getId());
        }
        if (recipientPublicKey == null) {
            recipientPublicKey = Convert.parseHexString(Convert.emptyToNull(req.getParameter("recipientPublicKey")));
        }
        if (recipientPublicKey == null) {
            throw new ParameterException(MISSING_RECIPIENT_PUBLIC_KEY);
        }
        String secretPhrase = getSecretPhrase(req, false);
        if (secretPhrase != null) {
            encryptedData = Account.encryptTo(recipientPublicKey, plainMessageBytes, secretPhrase, compress);
        }
    }
    if (encryptedData != null) {
        if (prunable) {
            return new Appendix.PrunableEncryptedMessage(encryptedData, isText, compress);
        } else {
            return new Appendix.EncryptedMessage(encryptedData, isText, compress);
        }
    } else {
        if (prunable) {
            return new Appendix.UnencryptedPrunableEncryptedMessage(plainMessageBytes, isText, compress, recipientPublicKey);
        } else {
            return new Appendix.UnencryptedEncryptedMessage(plainMessageBytes, isText, compress, recipientPublicKey);
        }
    }
}
Also used : IOException(java.io.IOException) ServletException(javax.servlet.ServletException) Part(javax.servlet.http.Part) EncryptedData(org.xel.crypto.EncryptedData)

Example 7 with EncryptedData

use of org.xel.crypto.EncryptedData in project elastic-core-maven by OrdinaryDude.

the class ReadMessage method processRequest.

@Override
protected JSONStreamAware processRequest(HttpServletRequest req) throws ParameterException {
    long transactionId = ParameterParser.getUnsignedLong(req, "transaction", true);
    boolean retrieve = "true".equalsIgnoreCase(req.getParameter("retrieve"));
    Transaction transaction = Nxt.getBlockchain().getTransaction(transactionId);
    if (transaction == null) {
        return UNKNOWN_TRANSACTION;
    }
    PrunableMessage prunableMessage = PrunableMessage.getPrunableMessage(transactionId);
    if (prunableMessage == null && (transaction.getPrunablePlainMessage() != null || transaction.getPrunableEncryptedMessage() != null) && retrieve) {
        if (Nxt.getBlockchainProcessor().restorePrunedTransaction(transactionId) == null) {
            return PRUNED_TRANSACTION;
        }
        prunableMessage = PrunableMessage.getPrunableMessage(transactionId);
    }
    JSONObject response = new JSONObject();
    Appendix.Message message = transaction.getMessage();
    Appendix.EncryptedMessage encryptedMessage = transaction.getEncryptedMessage();
    Appendix.EncryptToSelfMessage encryptToSelfMessage = transaction.getEncryptToSelfMessage();
    if (message == null && encryptedMessage == null && encryptToSelfMessage == null && prunableMessage == null) {
        return NO_MESSAGE;
    }
    if (message != null) {
        response.put("message", Convert.toString(message.getMessage(), message.isText()));
        response.put("messageIsPrunable", false);
    } else if (prunableMessage != null && prunableMessage.getMessage() != null) {
        response.put("message", Convert.toString(prunableMessage.getMessage(), prunableMessage.messageIsText()));
        response.put("messageIsPrunable", true);
    }
    String secretPhrase = ParameterParser.getSecretPhrase(req, false);
    byte[] sharedKey = ParameterParser.getBytes(req, "sharedKey", false);
    if (sharedKey.length != 0 && secretPhrase != null) {
        return JSONResponses.either("secretPhrase", "sharedKey");
    }
    if (secretPhrase != null || sharedKey.length > 0) {
        EncryptedData encryptedData = null;
        boolean isText = false;
        boolean uncompress = true;
        if (encryptedMessage != null) {
            encryptedData = encryptedMessage.getEncryptedData();
            isText = encryptedMessage.isText();
            uncompress = encryptedMessage.isCompressed();
            response.put("encryptedMessageIsPrunable", false);
        } else if (prunableMessage != null && prunableMessage.getEncryptedData() != null) {
            encryptedData = prunableMessage.getEncryptedData();
            isText = prunableMessage.encryptedMessageIsText();
            uncompress = prunableMessage.isCompressed();
            response.put("encryptedMessageIsPrunable", true);
        }
        if (encryptedData != null) {
            try {
                byte[] decrypted = null;
                if (secretPhrase != null) {
                    byte[] readerPublicKey = Crypto.getPublicKey(secretPhrase);
                    byte[] senderPublicKey = Account.getPublicKey(transaction.getSenderId());
                    byte[] recipientPublicKey = Account.getPublicKey(transaction.getRecipientId());
                    byte[] publicKey = Arrays.equals(senderPublicKey, readerPublicKey) ? recipientPublicKey : senderPublicKey;
                    if (publicKey != null) {
                        decrypted = Account.decryptFrom(publicKey, encryptedData, secretPhrase, uncompress);
                    }
                } else {
                    decrypted = Crypto.aesDecrypt(encryptedData.getData(), sharedKey);
                    if (uncompress) {
                        decrypted = Convert.uncompress(decrypted);
                    }
                }
                response.put("decryptedMessage", Convert.toString(decrypted, isText));
            } catch (RuntimeException e) {
                Logger.logDebugMessage("Decryption of message to recipient failed: " + e.toString());
                JSONData.putException(response, e, "Wrong secretPhrase or sharedKey");
            }
        }
        if (encryptToSelfMessage != null && secretPhrase != null) {
            byte[] publicKey = Crypto.getPublicKey(secretPhrase);
            try {
                byte[] decrypted = Account.decryptFrom(publicKey, encryptToSelfMessage.getEncryptedData(), secretPhrase, encryptToSelfMessage.isCompressed());
                response.put("decryptedMessageToSelf", Convert.toString(decrypted, encryptToSelfMessage.isText()));
            } catch (RuntimeException e) {
                Logger.logDebugMessage("Decryption of message to self failed: " + e.toString());
            }
        }
    }
    return response;
}
Also used : Appendix(org.xel.Appendix) Transaction(org.xel.Transaction) JSONObject(org.json.simple.JSONObject) PrunableMessage(org.xel.PrunableMessage) EncryptedData(org.xel.crypto.EncryptedData)

Example 8 with EncryptedData

use of org.xel.crypto.EncryptedData in project elastic-core-maven by OrdinaryDude.

the class MessageEncryptionTest method encryptText.

@Test
public void encryptText() {
    JSONStreamAware json = JSONResponses.INCORRECT_ALIAS;
    EncryptedData encryptedData = encrypt(Convert.toBytes(json.toString()));
    Assert.assertEquals(json.toString(), Convert.toString(decrypt(encryptedData)));
}
Also used : EncryptedData(org.xel.crypto.EncryptedData) JSONStreamAware(org.json.simple.JSONStreamAware) Test(org.junit.Test) BlockchainTest(org.xel.BlockchainTest)

Example 9 with EncryptedData

use of org.xel.crypto.EncryptedData in project elastic-core-maven by OrdinaryDude.

the class MessageEncryptionTest method encryptEmpty.

@Test
public void encryptEmpty() {
    EncryptedData encryptedData = encrypt(Convert.toBytes(""));
    Assert.assertEquals("", Convert.toString(decrypt(encryptedData)));
}
Also used : EncryptedData(org.xel.crypto.EncryptedData) Test(org.junit.Test) BlockchainTest(org.xel.BlockchainTest)

Example 10 with EncryptedData

use of org.xel.crypto.EncryptedData in project elastic-core-maven by OrdinaryDude.

the class SendMessageTest method sendClientEncryptedMessageToSelf.

@Test
public void sendClientEncryptedMessageToSelf() {
    EncryptedData encryptedData = ALICE.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("encryptToSelfMessageData", Convert.toHexString(encryptedData.getData())).param("encryptToSelfMessageNonce", 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("encryptToSelfMessage");
    Assert.assertEquals(64 + 32, /* data + hash */
    ((String) encryptedMessage.get("data")).length());
    Assert.assertEquals(64, ((String) encryptedMessage.get("nonce")).length());
    generateBlock();
    response = new org.xel.http.APICall.Builder("readMessage").param("secretPhrase", ALICE.getSecretPhrase()).param("transaction", transaction).build().invoke();
    Logger.logDebugMessage("readMessage: " + response);
    Assert.assertEquals("hello world", response.get("decryptedMessageToSelf"));
}
Also used : JSONObject(org.json.simple.JSONObject) EncryptedData(org.xel.crypto.EncryptedData) Test(org.junit.Test) BlockchainTest(org.xel.BlockchainTest)

Aggregations

EncryptedData (org.xel.crypto.EncryptedData)12 JSONObject (org.json.simple.JSONObject)5 Test (org.junit.Test)5 BlockchainTest (org.xel.BlockchainTest)5 IOException (java.io.IOException)2 ServletException (javax.servlet.ServletException)2 Part (javax.servlet.http.Part)2 JSONStreamAware (org.json.simple.JSONStreamAware)1 Appendix (org.xel.Appendix)1 PrunableMessage (org.xel.PrunableMessage)1 Transaction (org.xel.Transaction)1