需求
可以将数据加密解密,并能将其发送给朋友。
思路
加密解密这种一般都是调别人包里的算法,这里用的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();
}
}