簡體   English   中英

一些字符串的 Base64 代碼和 AES 加密錯誤

[英]Base64 Code and AES Encryption error with some strings

我正在嘗試使用 AES 算法和 Base64 通過網絡加密發送受保護的文本。 對於我嘗試過的幾個文本,它工作正常,但有一些字符串沒有正確解碼。 我在字符串上發現了一個無法解碼的模式。 他們身上似乎有一條斷裂線。 示例:加密步驟:

1) 純文本

2) AES 加密文本:

Íe l1 Ÿ?‰|6ÎÔ.jcæ÷Ï_ðNSO~A'q@Ó[

3) BASE64 AES 文本:

zWUNbDEgnz+JfDbO1C5qY+b3z1/wTlNPmAdBknFA01s=

解密步驟:

1) BASE64 AES 文本:

zWUNbDEgnz+JfDbO1C5qY+b3z1/wTlNPmAdBknFA01s=

2) AES 加密文本:

Íe l1 Ÿ?‰|6ÎÔ.jcæ÷Ï_ðNSO~A'q@Ó[

3) 純文本:

í¿Ð“tº„£¹ÍG‰\\îœ

我加密的任何其他文本在其加密版本上不包含斷裂線都被正確解密。

前任:

加密步驟:

1) 純文本:P@$$w0rd

2) AES 加密文本:

÷÷Odã29ôÐÑÌe£™ø

3) Base64 AES 文本:

9xH3T2TjMjn00NHMZaOZ+A==

解密步驟:

3) Base64 AES 文本:

9xH3T2TjMjn00NHMZaOZ+A==

2) AES 加密文本:

÷÷Odã29ôÐÑÌe£™ø

3) 純文本:

P@$$w0rd

下面是我為此目的創建的對象。

Base64Codec 對象:

package security;
import java.util.Base64;
import java.util.Base64.Decoder;
import java.util.Base64.Encoder;
public class Base64Codec
{
    private static Base64Codec objInstance;
    private Encoder objEncoder;
    private Decoder objDecoder;
    private Base64Codec()
    {
        this.setEncoder();
        this.setDecoder();
    }
    public static Base64Codec getInstance()
    {
        if (objInstance == null)
        {
            objInstance = new Base64Codec();
        }
        return objInstance;
    }
    private void setEncoder()
    {
        this.objEncoder = Base64.getEncoder();
    }
    private Encoder getEncoder()
    {
        return this.objEncoder;
    }
    private void setDecoder()
    {
        this.objDecoder = Base64.getDecoder();
    }
    private Decoder getDecoder()
    {
        return this.objDecoder;
    }
    protected String encode(String strValue)
    {
        return this.getEncoder().encodeToString(strValue.getBytes());
    }
    protected String decode(String strValue)
    {
        return new String(this.getDecoder().decode(strValue));
    }
}

高級加密標准對象

package security;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
public class AdvancedEncryptionStandard
{
    private static final String strKey = "02E68E02BE7400FE11E8A45B60017F98";
    private static final byte[] bytKey = strKey.getBytes();
    private static final String strAlgorithm = "AES";
    private static AdvancedEncryptionStandard objInstance;
    private Cipher objCipher;
    private SecretKeySpec objSecretKey;
    private AdvancedEncryptionStandard() throws NoSuchAlgorithmException, NoSuchPaddingException
    {
        this.setSecretKey();
        this.setCipher();
    }
    public static AdvancedEncryptionStandard getInstance() throws NoSuchAlgorithmException, NoSuchPaddingException
    {
        if (objInstance == null)
        {
            objInstance = new AdvancedEncryptionStandard();
        }
        return objInstance;
    }
    private byte[] getKey()
    {
        return bytKey;
    }
    private String getAlgorithm()
    {
        return strAlgorithm;
    }
    private void setSecretKey()
    {
        this.objSecretKey = new SecretKeySpec(this.getKey(), this.getAlgorithm());
    }
    private SecretKeySpec getSecretKey()
    {
        return this.objSecretKey;
    }
    private void setCipher() throws NoSuchAlgorithmException, NoSuchPaddingException
    {
        this.objCipher = Cipher.getInstance(this.getAlgorithm());
    }
    private Cipher getCipher()
    {
        return this.objCipher;
    }
    protected String encrypt(String strValue) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException
    {
        this.getCipher().init(Cipher.ENCRYPT_MODE, this.getSecretKey());
        return new String(this.getCipher().doFinal(strValue.getBytes()));
    }
    protected String decrypt(String strValue) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException
    {
        this.getCipher().init(Cipher.DECRYPT_MODE, this.getSecretKey());
        return new String(this.getCipher().doFinal(strValue.getBytes()));
    }
}

安全文本對象

package security;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
public class SecureText
{
    private static SecureText objInstance;
    private SecureText()
    {

    }
    public static SecureText getInstance()
    {
        if (objInstance == null)
        {
            objInstance = new SecureText();
        }
        return objInstance;
    }
    public String encode(String strValue) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException
    {
        return Base64Codec.getInstance().encode(AdvancedEncryptionStandard.getInstance().encrypt(strValue));
    }
    public String decode(String strValue) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException
    {
        return AdvancedEncryptionStandard.getInstance().decrypt(Base64Codec.getInstance().decode(strValue));
    }
}

測試:

public static void main(String[] args)
    {
//Test SecureText Object. AES and Base64
        String strSecureText = <plain text>
        System.out.println(strSecureText);
        System.out.println(SecureText.getInstance().decode(strSecureText));
//Test AES and Base64 separated
        String strCoded;
        String strEncrypted;
        String strDecrypted;
        String strDecoded;
        strEncrypted = AdvancedEncryptionStandard.getInstance().encrypt(<plain text>);
        System.out.println(strEncrypted);
        strCoded = Base64Codec.getInstance().encode(strEncrypted);
        System.out.println(strCoded);
        strDecoded = Base64Codec.getInstance().decode(strCoded);
        System.out.println(strDecoded);
        strDecrypted = AdvancedEncryptionStandard.getInstance().decrypt(strDecoded);
        System.out.println(strDecrypted);
        String strSecureText = SecureText.getInstance().encode(<plain text>);
    }

我看到你的代碼有兩個問題(關於加密,對於結構你已經有足夠的評論)

對於我嘗試過的幾個文本,它工作正常,但有一些字符串沒有正確解碼。 我在字符串上發現了一個無法解碼的模式。

永遠不要嘗試將 Java 中的隨機字節數組表示為String JVM 會嘗試暗示字符集轉換,您可能會丟失/更改數據。 這同樣適用於將String轉換為字節時,明確說明編碼是一種很好的做法。

加密數據只是一個字節數組,一旦您打算打印它們,請將字節數組編碼為可讀的內容,例如 hex 或 base64。 不要使用new String(encryptedArray)

下一個問題是我沒有看到您使用 IV(並且似乎要求經過身份驗證的加密太多),請閱讀我的博客,了解如何至少幾乎正確地進行加密

暫無
暫無

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

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