溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Java里如何利用Unicode統一碼加密

發布時間:2025-04-12 21:11:12 來源:億速云 閱讀:125 作者:小樊 欄目:編程語言

在Java中,可以使用Unicode編碼對字符串進行加密。這里有一個簡單的示例,展示了如何使用Unicode編碼對字符串進行加密和解密:

public class UnicodeEncryption {

    public static void main(String[] args) {
        String originalText = "Hello, World!";
        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);
    }

    public static String encrypt(String text) {
        StringBuilder encryptedText = new StringBuilder();
        for (char c : text.toCharArray()) {
            encryptedText.append(String.format("\\u%04x", (int) c));
        }
        return encryptedText.toString();
    }

    public static String decrypt(String encryptedText) {
        StringBuilder decryptedText = new StringBuilder();
        String[] hexValues = encryptedText.split("\\\\u");
        for (int i = 1; i < hexValues.length; i++) {
            int intValue = Integer.parseInt(hexValues[i], 16);
            decryptedText.append((char) intValue);
        }
        return decryptedText.toString();
    }
}

在這個示例中,我們定義了兩個方法:encryptdecrypt。encrypt 方法將原始字符串中的每個字符轉換為其對應的Unicode編碼,然后將它們拼接在一起。decrypt 方法則執行相反的操作,將Unicode編碼轉換回字符。

運行這個程序,你會看到以下輸出:

Original text: Hello, World!
Encrypted text: \u0048\u0065\u006c\u006c\u006f\u002c\u0020\u0057\u006f\u0072\u006c\u0064\u0021
Decrypted text: Hello, World!

請注意,這個示例僅用于演示目的,實際上并不是一個安全的加密方法。在實際應用中,建議使用更強大的加密算法,如AES或RSA。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女