下面是一个简单的文本加密器和解密器的Java代码示例:

import java.util.Base64;

public class TextEncrypterDecrypter {
    private static final String KEY = "MySecretKey";

    public static String encrypt(String text) {
        try {
            byte[] encryptedBytes = text.getBytes("UTF-8");
            for (int i = 0; i < encryptedBytes.length; i++) {
                encryptedBytes[i] ^= KEY.getBytes()[i % KEY.length()];
            }
            return Base64.getEncoder().encodeToString(encryptedBytes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String decrypt(String encryptedText) {
        try {
            byte[] encryptedBytes = Base64.getDecoder().decode(encryptedText);
            for (int i = 0; i < encryptedBytes.length; i++) {
                encryptedBytes[i] ^= KEY.getBytes()[i % KEY.length()];
            }
            return new String(encryptedBytes, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        String originalText = "Hello, 你好!";
        System.out.println("Original Text: " + originalText);

        String encryptedText = encrypt(originalText);
        System.out.println("Encrypted Text: " + encryptedText);

        String decryptedText = decrypt(encryptedText);
        System.out.println("Decrypted Text: " + decryptedText);
    }
}

这个示例中使用了异或运算对文本进行加密和解密,并使用Base64编码进行存储和传输。KEY是用于加密和解密的密钥,可以根据需要进行更改。运行上述代码,将输出以下结果:

Original Text: Hello, 你好!
Encrypted Text: GhoLXh4bFQ8BdQ==
Decrypted Text: Hello, 你好!

可以看到,原始文本经过加密后再解密,最终结果与原始文本相同。请注意,这只是一个简单的示例,实际的加密算法应该更加复杂和安全

用java写一个文本加密器要求支持将中文和英文都进行加密再写一个对应的解密器

原文地址: http://www.cveoy.top/t/topic/h4tw 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录