Search in sources :

Example 11 with EncryptedData

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;
    }
}
Also used : JSONObject(org.json.simple.JSONObject) EncryptedData(org.xel.crypto.EncryptedData)

Example 12 with EncryptedData

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);
    }
}
Also used : EncryptedData(org.xel.crypto.EncryptedData)

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