eu.dety.burp.joseph.gui.DecoderPanel.java Source code

Java tutorial

Introduction

Here is the source code for eu.dety.burp.joseph.gui.DecoderPanel.java

Source

/**
 * JOSEPH - JavaScript Object Signing and Encryption Pentesting Helper
 * Copyright (C) 2016 Dennis Detering
 * <p>
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 * <p>
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * <p>
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
package eu.dety.burp.joseph.gui;

import burp.IBurpExtenderCallbacks;
import burp.IExtensionHelpers;
import eu.dety.burp.joseph.utilities.Decoder;
import eu.dety.burp.joseph.utilities.Logger;
import org.apache.commons.codec.binary.Base64;

/**
 * Decoder tab to assist with base64url encoding/decoding
 * 
 * @author Dennis Detering
 * @version 1.0
 */
public class DecoderPanel extends javax.swing.JPanel {
    private static final Logger loggerInstance = Logger.getInstance();
    private IExtensionHelpers helpers;

    public DecoderPanel(IBurpExtenderCallbacks callbacks) {
        this.helpers = callbacks.getHelpers();
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this
     * method is always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed"
    // desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        formatButtonGroup = new javax.swing.ButtonGroup();
        jScrollPane1 = new javax.swing.JScrollPane();
        inputTextarea = new javax.swing.JTextArea();
        encodeButton = new javax.swing.JButton();
        decodeButton = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        outputTextarea = new javax.swing.JTextArea();
        formatText = new javax.swing.JRadioButton();
        formatHex = new javax.swing.JRadioButton();

        inputTextarea.setColumns(20);
        inputTextarea.setRows(5);
        jScrollPane1.setViewportView(inputTextarea);

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("JOSEPH"); // NOI18N
        encodeButton.setText(bundle.getString("ENCODE_B64")); // NOI18N
        encodeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                encodeButtonActionPerformed(evt);
            }
        });

        decodeButton.setText(bundle.getString("DECODE_B64")); // NOI18N
        decodeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                decodeButtonActionPerformed(evt);
            }
        });

        outputTextarea.setColumns(20);
        outputTextarea.setRows(5);
        jScrollPane2.setViewportView(outputTextarea);

        formatButtonGroup.add(formatText);
        formatText.setSelected(true);
        formatText.setText(bundle.getString("TEXT")); // NOI18N
        formatText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                formatTextActionPerformed(evt);
            }
        });

        formatButtonGroup.add(formatHex);
        formatHex.setText(bundle.getString("HEX")); // NOI18N
        formatHex.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                formatHexActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addGroup(layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup().addContainerGap()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jScrollPane1)
                                        .addGroup(layout.createSequentialGroup().addComponent(encodeButton)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(decodeButton)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                        46, Short.MAX_VALUE)
                                                .addComponent(formatText)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(formatHex)))))
                        .addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 200,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(formatText).addComponent(formatHex))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(encodeButton).addComponent(decodeButton)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 200,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
    }// </editor-fold>//GEN-END:initComponents

    private void encodeButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_encodeButtonActionPerformed
        String content = inputTextarea.getText();
        byte[] input;

        try {
            if (formatHex.isSelected()) {
                input = Decoder.hexToBytes(content);
            } else {
                input = helpers.stringToBytes(content);
            }

            outputTextarea.setText(Decoder.base64UrlEncode(input));
        } catch (Exception e) {
            loggerInstance.log(getClass(), "Base64url encoding failed: " + e.getMessage(), Logger.LogLevel.ERROR);
            outputTextarea.setText("");
        }
    }// GEN-LAST:event_encodeButtonActionPerformed

    private void decodeButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_decodeButtonActionPerformed
        String content = outputTextarea.getText();
        String input;

        try {
            if (formatHex.isSelected()) {
                input = Decoder.bytesToHex(Base64.decodeBase64(content));
            } else {
                input = Decoder.getDecoded(content);
            }

            inputTextarea.setText(input);

        } catch (Exception e) {
            loggerInstance.log(getClass(), "Base64url encoding failed: " + e.getMessage(), Logger.LogLevel.ERROR);
            inputTextarea.setText("");
        }
    }// GEN-LAST:event_decodeButtonActionPerformed

    private void formatTextActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_formatTextActionPerformed
        if (formatText.isSelected()) {
            String content = inputTextarea.getText();

            if (!content.isEmpty()) {
                try {
                    inputTextarea.setText(helpers.bytesToString(Decoder.hexToBytes(content)));
                } catch (Exception e) {
                    loggerInstance.log(getClass(), "Formatting from hex to text failed: " + e.getMessage(),
                            Logger.LogLevel.ERROR);
                    inputTextarea.setText("");
                }
            }
        }
    }// GEN-LAST:event_formatTextActionPerformed

    private void formatHexActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_formatHexActionPerformed
        if (formatHex.isSelected()) {
            String content = inputTextarea.getText();

            if (!content.isEmpty()) {
                try {
                    inputTextarea.setText(Decoder.bytesToHex(helpers.stringToBytes(content)));
                } catch (Exception e) {
                    loggerInstance.log(getClass(), "Formatting from text to hex failed: " + e.getMessage(),
                            Logger.LogLevel.ERROR);
                    inputTextarea.setText("");
                }
            }
        }
    }// GEN-LAST:event_formatHexActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton decodeButton;
    private javax.swing.JButton encodeButton;
    private javax.swing.ButtonGroup formatButtonGroup;
    private javax.swing.JRadioButton formatHex;
    private javax.swing.JRadioButton formatText;
    private javax.swing.JTextArea inputTextarea;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea outputTextarea;
    // End of variables declaration//GEN-END:variables
}