簡體   English   中英

這個Java加密代碼線程安全嗎?

[英]Is this Java encryption code thread safe?

我想將以下代碼用於高並發性應用程序,其中某些數據必須加密和解密。 所以我需要知道應該同步這段代碼的哪一部分,如果有的話,以避免不可預測的問題。

public class DesEncrypter {
    Cipher ecipher;
    Cipher dcipher;

    // 8-byte Salt
    byte[] salt = {
        (byte)0xA9, (byte)0x9B, (byte)0xC8, (byte)0x32,
        (byte)0x56, (byte)0x35, (byte)0xE3, (byte)0x03
    };

    int iterationCount = 19;

    DesEncrypter(String passPhrase) {
        try {
            // Create the key
            KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount);

            SecretKey key = SecretKeyFactory.getInstance( "PBEWithMD5AndDES").generateSecret(keySpec);
            ecipher = Cipher.getInstance(key.getAlgorithm());
            dcipher = Cipher.getInstance(key.getAlgorithm());

            // Prepare the parameter to the ciphers
            AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);

            // Create the ciphers
            ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
            dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
        } catch (...)
    }

    public String encrypt(String str) {
        try {
            // Encode the string into bytes using utf-8
            byte[] utf8 = str.getBytes("UTF8");
            // Encrypt
            byte[] enc = ecipher.doFinal(utf8);
            // Encode bytes to base64 to get a string
            return new sun.misc.BASE64Encoder().encode(enc);

        } catch (...)
    }

    public String decrypt(String str) {
        try {
            // Decode base64 to get bytes
            byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
            // Decrypt
            byte[] utf8 = dcipher.doFinal(dec);
            // Decode using utf-8
            return new String(utf8, "UTF8");
        } catch (...)
    }
}

如果我在每個調用的encrypt()和decrypt()方法中創建一個新的密碼,那么我可以避免並發問題,我只是不確定每次調用獲取一個新的密碼實例是否有很多開銷。

   public String encrypt(String str) {
        try {
            // Encode the string into bytes using utf-8
            byte[] utf8 = str.getBytes("UTF8");
            // Encrypt
            //new cipher instance
            ecipher = Cipher.getInstance(key.getAlgorithm());

            byte[] enc = ecipher.doFinal(utf8);
            // Encode bytes to base64 to get a string
            return new sun.misc.BASE64Encoder().encode(enc);

        } catch (...)

標准規則是 - 除非Javadoc明確聲明Java庫中的類是線程安全的,否則您應該假設它不是。

在這個特定的例子中:

  • 各種類都沒有記錄為線程安全的。
  • Cipher.getInstance(...)SecretKeyFactory.getInstance(...)方法被記錄為返回新對象; 即不引用其他線程可能引用的現有對象。

    更新 - javadoc說:

    “返回一個新的SecretKeyFactory對象,該對象封裝了第一個支持指定算法的Provider的SecretKeyFactorySpi實現。”

    此外, 源代碼明確地確認創建並返回了新對象。

簡而言之,這意味着您的DesEncryptor類當前不是線程安全的,但您應該能夠通過同步相關操作(例如encodedecode )使其成為線程安全的,而不是暴露兩個Cipher對象。 如果使方法同步可能會產生瓶頸,那么為每個線程創建一個單獨的DesEncryptor實例。

如果它們被多個線程同時使用,那么事情只需要是線程安全的。 因為這個類的每個實例都可能只被一個線程使用,所以不必擔心它是否是線程安全的。

在一個不相關的說明,有一個硬編碼鹽,nonce或IV 從來都不是一個好主意。

Cipher對象不是線程安全的,因為它保留了有關加密過程的內部狀態。 這也適用於您的DesEncrypter類 - 每個線程都需要使用自己的DesEncrypter實例,除非您同步encodedecode方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM