gpt4 book ai didi

java - 使用Google Tink库解密 key 时出现标签不匹配错误

转载 作者:行者123 更新时间:2023-12-02 10:01:33 24 4
gpt4 key购买 nike

我是密码学的新手。我正在研究POC来加密和解密字符串。当我解密加密的字符串时,它有时可以工作,但有时会抛出标签不匹配错误。我有什么想念的吗?

这是我的代码:

EncryptionServiceImpl.java

public 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.java

public class EncryptionServiceImplTest {

@Test
public void encrypt() throws IOException, GeneralSecurityException {
EncryptionServiceImpl encryptionService = new EncryptionServiceImpl();
String encryptedText = encryptionService.encrypt("Hello World");
assertThat(encryptedText, Matchers.notNullValue());
}

@Test
public void decrypt() throws IOException, GeneralSecurityException {
EncryptionServiceImpl encryptionService = new EncryptionServiceImpl();

String encryptedText = encryptionService.encrypt("Hello World");
String decrypedText = encryptionService.decrypt(encryptedText);

assertThat(decrypedText, Matchers.is("Hello World"));
}
}



例外:
INFO:密文前缀与密钥匹配,但无法解密:javax.crypto.AEADBadTagException:标记不匹配!
com.encryption.api.service.EncryptionServiceImplTest>解密失败

java.security.GeneralSecurityException at EncryptionServiceImplTest.java:25


解密失败
java.security.GeneralSecurityException:解密失败
    在com.google.crypto.tink.aead.AeadFactory $ 1.decrypt(AeadFactory.java:109)
    在com.encryption.api.service.EncryptionServiceImpl.decrypt(EncryptionServiceImpl.java:53)
    在com.encryption.api.service.EncryptionServiceImplTest.decrypt(EncryptionServiceImplTest.java:25)
    在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
    在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    在java.lang.reflect.Method.invoke(Method.java:498)
    在org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:50)
    在org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    在org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    在org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    在org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290)
    在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)
    在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    在org.junit.runners.ParentRunner.access处$ 000(ParentRunner.java:58)
    在org.junit.runners.ParentRunner上$ 2.evaluate(ParentRunner.java:268)
    在org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    在org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:114)
    在org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:57)
    在org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:66)
    在org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:51)
    在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
    在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    在java.lang.reflect.Method.invoke(Method.java:498)
    在org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    在org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    在org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
    在org.gradle.internal.dispatch.ProxyDispatchAdapter $ DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
    在com.sun.proxy。$ Proxy1.processTestClass(未知来源)
    在org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:108)
    在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
    在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    在java.lang.reflect.Method.invoke(Method.java:498)
    在org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    在org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    在org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection $ DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:146)
    在org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection $ DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:128)
    在org.gradle.internal.remote.internal.hub.MessageHub $ Handler.run(MessageHub.java:404)
    在org.gradle.internal.concurrent.ExecutorPolicy $ CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)
    在org.gradle.internal.concurrent.ManagedExecutorImpl $ 1.run(ManagedExecutorImpl.java:46)
    在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:624)
    在org.gradle.internal.concurrent.ThreadFactoryImpl $ ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)
    在java.lang.Thread.run(Thread.java:748)

1个测试完成,1个失败

最佳答案

如果加密消息的字节序列存储在字符串中,则必须使用适当的编码。适当意味着编码必须允许序列中的所有字节或字节组合。如果不是这种情况,则字节序列中的值会自动更改,并且在存储期间不会被注意。如果随后在解密过程中从字符串中重建了字节数组,则原始和重建的字节数组会有所不同,并且解密失败。 here对此进行了很好的解释。

由于AES-GCM为每种加密生成一个新的初始化向量,因此即使使用相同的纯文本,每种加密的加密消息也有所不同。

两者都导致一个事实,在您的示例中,加密有时有效,有时却无效:只要字节序列与您使用的编码兼容,解密就有效,否则无效。

如果要独立于编码,则只需使用字节数组本身,即encrypt方法将返回字节数组而不是字符串,并且类似地,字节数组将传递给< cc>-方法。

关于java - 使用Google Tink库解密 key 时出现标签不匹配错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55583921/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com