亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用谷歌叮當庫解密密鑰時出現標簽不匹配錯誤

使用谷歌叮當庫解密密鑰時出現標簽不匹配錯誤

慕斯王 2022-09-28 15:38:51
我是密碼學的新手。我正在研究一個poc來加密和解密字符串。當我解密加密的字符串時,它有時會起作用,但其他時候會引發Tag不匹配錯誤。我錯過了什么嗎?這是我的代碼:EncryptionServiceImpl.javapublic class EncryptionServiceImpl {    private static final Logger log = LoggerFactory.getLogger("EncryptionServiceImpl");    private final KeysetHandle keysetHandle;    private final Aead aead;    public EncryptionServiceImpl() throws GeneralSecurityException {        AeadConfig.register();        this.keysetHandle = KeysetHandle.generateNew(AeadKeyTemplates.AES128_GCM);        aead = AeadFactory.getPrimitive(keysetHandle);    }    public String encrypt(String text) throws GeneralSecurityException {        log.info(String.format("Encrypting %s", text));        byte[] plainText = text.getBytes();        byte[] additionalData = "masterkey".getBytes();        byte[] cipherText = aead.encrypt(plainText,additionalData);        String output = new String(cipherText);        log.info(String.format("The encrypted text: %s", output));        return output;    }    public String decrypt(String text) throws GeneralSecurityException {        log.info(String.format("Decrypting %s", text));        byte[] cipherText = text.getBytes();        byte[] additionalData = "masterkey".getBytes();        byte[] decipheredData = aead.decrypt(cipherText,additionalData);        String output = new String(decipheredData);        log.info(String.format("The decrypted text: %s", output));        return output;    }}EncryptionServiceImplTest.javapublic class EncryptionServiceImplTest {    @Test    public void encrypt() throws IOException, GeneralSecurityException {        EncryptionServiceImpl encryptionService = new EncryptionServiceImpl();        String encryptedText = encryptionService.encrypt("Hello World");        assertThat(encryptedText, Matchers.notNullValue());    }異常:信息:密文前綴與密鑰匹配,但無法解密:javax.crypto.AEAD標記異常:標記不匹配!com.encryption.api.service.EncryptionService測試>解密失敗
查看完整描述

3 回答

?
婷婷同學_

TA貢獻1844條經驗 獲得超8個贊

如果加密消息的字節序列存儲在字符串中,則必須使用適當的編碼。適當意味著編碼必須允許序列中的所有字節或字節組合。如果不是這種情況,則字節序列中的值會自動更改,并且在存儲期間不會被注意到。如果在解密期間從字符串重建字節數組,則原始字節數組和重建的字節數組會有所不同,并且解密將失敗。這里對此進行了很好的解釋。

由于 AES-GCM 為每個加密生成一個新的初始化向量,因此即使使用相同的明文,每個加密的加密消息也是不同的。

兩者都導致這樣一個事實,即在您的示例中,加密有時有效,有時無效:每當字節序列與您正在使用的編碼兼容時,解密就會起作用,否則就不起作用。

如果你想獨立于編碼,只需使用字節數組本身,即-method返回字節數組而不是字符串,類似地,字節數組傳遞給-method而不是字符串。encryptdecrypt


查看完整回答
反對 回復 2022-09-28
?
www說

TA貢獻1775條經驗 獲得超8個贊

我修改了代碼,但我仍然看到解密有時會因相同的請求而失敗。


public class Utils {

private static final Logger log = LoggerFactory.getLogger(Utils.class);

private Aead aead;

private static Utils utils;


private Utils() {

    try {

        AeadConfig.register();

        KeysetHandle keysetHandle = KeysetHandle.generateNew(AeadKeyTemplates.AES128_GCM);

        aead = AeadFactory.getPrimitive(keysetHandle);

    } catch (GeneralSecurityException e) {

        log.error(String.format("Error occured: %s",e.getMessage())).log();

    }

}


public static Utils getInstance() {

    if(null == utils) {

        utils = new Utils();

    }


    return utils;

}


public String encrypt(String text) throws GeneralSecurityException, UnsupportedEncodingException {

    byte[] plainText = text.getBytes("ISO-8859-1");

    byte[] additionalData = null;

    byte[] cipherText = aead.encrypt(plainText,additionalData);


    String output = Base64.getEncoder().encodeToString(cipherText);

    return output;

}


public String decrypt(String text) throws GeneralSecurityException, UnsupportedEncodingException {

    byte[] cipherText = Base64.getDecoder().decode(text);

    byte[] additionalData = null;

    byte[] decipheredData = aead.decrypt(cipherText,additionalData);


    String output = new String(decipheredData,"ISO-8859-1");

    return output;

}

}


查看完整回答
反對 回復 2022-09-28
?
富國滬深

TA貢獻1790條經驗 獲得超9個贊

當我在本地機器中運行時,我也沒有得到代碼中的錯誤。但是當我在云(谷歌云)中部署時,我開始看到錯誤。


這是我的朱尼特代碼:


public class UtilsTest {


private static final Utils cryptographicUtils = Utils.getInstance();


@Test

public void encrypt() throws IOException, GeneralSecurityException {

    String encryptedText = cryptographicUtils.encrypt("Hello World");

    assertThat(encryptedText, Matchers.notNullValue());

}


@Test

public void decrypt() throws IOException, GeneralSecurityException {

    String encryptedText = cryptographicUtils.encrypt("Hello 123456");

    String decrypedText = cryptographicUtils.decrypt(encryptedText);


    assertThat(decrypedText, Matchers.is("Hello 123456"));

}

}



查看完整回答
反對 回復 2022-09-28
  • 3 回答
  • 0 關注
  • 120 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號