【Java练习】文本处理:密码短信

需求

可以将数据加密解密,并能将其发送给朋友。

思路

加密解密这种一般都是调别人包里的算法,这里用的javax.crypto。
发送短信也需要调用API。
咋说呢,说复杂也复杂,说简单也简单,就用gpt生成的代码吧。

实现

需要引入依赖:

        <dependency>
            <groupId>com.twilio.sdk</groupId>
            <artifactId>twilio</artifactId>
            <version>8.30.1</version>
        </dependency>

窗体代码:

public class CryptoSmsApp extends JFrame {

    private JTextField inputTextField;
    private JTextField phoneNumberField;
    private JTextArea outputTextArea;
    private SecretKey secretKey;

    public CryptoSmsApp() {
        setTitle("Crypto SMS App");
        setSize(500, 400);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        try {
            secretKey = CryptoUtils.generateKey();
        } catch (Exception e) {
            e.printStackTrace();
        }

        inputTextField = new JTextField();
        phoneNumberField = new JTextField();
        outputTextArea = new JTextArea();
        outputTextArea.setLineWrap(true);
        outputTextArea.setWrapStyleWord(true);
        outputTextArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(outputTextArea);

        JPanel topPanel = new JPanel(new GridLayout(3, 1));
        topPanel.add(new JLabel("Input Text:"));
        topPanel.add(inputTextField);
        topPanel.add(new JLabel("Recipient Phone Number:"));
        topPanel.add(phoneNumberField);

        JButton encryptButton = new JButton("Encrypt and Send");
        encryptButton.addActionListener(e -> encryptAndSend());

        JButton decryptButton = new JButton("Decrypt");
        decryptButton.addActionListener(e -> decryptMessage());

        JPanel buttonPanel = new JPanel();
        buttonPanel.add(encryptButton);
        buttonPanel.add(decryptButton);

        add(topPanel, BorderLayout.NORTH);
        add(scrollPane, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);
    }

    private void encryptAndSend() {
        String message = inputTextField.getText();
        String phoneNumber = phoneNumberField.getText();
        try {
            String encryptedText = CryptoUtils.encrypt(message, secretKey);
            SmsUtils.sendSms(phoneNumber,encryptedText);
            outputTextArea.append("Encrypted and sent: " + encryptedText + "\n");
        } catch (Exception e) {
            System.out.println(e.getMessage());
            outputTextArea.append("Encrypted or send failed.\n");
        }
    }

    private void decryptMessage() {
        String encryptedText = inputTextField.getText();
        try {
            String decryptedText = CryptoUtils.decrypt(encryptedText, secretKey);
            outputTextArea.append("Decrypted: " + decryptedText + "\n");
        } catch (Exception e) {
            e.printStackTrace();
            outputTextArea.append("Decryption failed.\n");
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(()->new CryptoSmsApp().setVisible(true));
    }
}

其中加密工具类的代码:

public class CryptoUtils {
    private static final String ALGORITHM = "AES";
    private static final int KEY_SIZE = 128;

    public static SecretKey generateKey() throws Exception {
        KeyGenerator keyGen = KeyGenerator.getInstance(ALGORITHM);
        keyGen.init(KEY_SIZE);
        return keyGen.generateKey();
    }

    public static String encrypt(String data, SecretKey key) throws Exception {
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] encryptedData = cipher.doFinal(data.getBytes());
        return Base64.getEncoder().encodeToString(encryptedData);
    }

    public static String decrypt(String encryptedData, SecretKey key) throws Exception {
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] decodedData = Base64.getDecoder().decode(encryptedData);
        byte[] decryptedData = cipher.doFinal(decodedData);
        return new String(decryptedData);
    }
}

其中发送短信工具类的代码:

public class SmsUtils {
    public static final String ACCOUNT_SID = "YOUR_TWILIO_ACCOUNT_SID";
    public static final String AUTH_TOKEN = "YOUR_TWILIO_AUTH_TOKEN";
    public static final String FROM_PHONE_NUMBER = "YOUR_TWILIO_PHONE_NUMBER";

    public static void sendSms(String toPhoneNumber, String message) {
        Twilio.init(ACCOUNT_SID, AUTH_TOKEN);
        Message.creator(new PhoneNumber(toPhoneNumber), new PhoneNumber(FROM_PHONE_NUMBER), message).create();
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值