Java 使用google tink库解密密钥时出现标记不匹配错误
我对密码学一无所知。我正在使用poc对字符串进行加密和解密。当我解密加密字符串时,它有时会工作,但有时会抛出标记不匹配错误。我遗漏了什么吗 这是我的密码:Java 使用google tink库解密密钥时出现标记不匹配错误,java,tink,Java,Tink,我对密码学一无所知。我正在使用poc对字符串进行加密和解密。当我解密加密字符串时,它有时会工作,但有时会抛出标记不匹配错误。我遗漏了什么吗 这是我的密码: EncryptionServiceImpl.java 公共类EncryptionServiceImpl{ 私有静态最终记录器log=LoggerFactory.getLogger(“EncryptionServiceImpl”); 专用最终密钥密钥密钥密钥密钥密钥密钥密钥密钥; 私人最终Aead Aead; public Encryption
EncryptionServiceImpl.java
公共类EncryptionServiceImpl{
私有静态最终记录器log=LoggerFactory.getLogger(“EncryptionServiceImpl”);
专用最终密钥密钥密钥密钥密钥密钥密钥密钥密钥;
私人最终Aead Aead;
public EncryptionServiceImpl()引发GeneralSecurityException{
AeadConfig.register();
this.keysetHandle=keysetHandle.generateNew(AeadKeyTemplates.AES128GCM);
aead=AeadFactory.getPrimitive(keysetHandle);
}
公共字符串加密(字符串文本)引发GeneralSecurityException{
log.info(字符串格式(“加密%s”,文本));
字节[]明文=text.getBytes();
byte[]additionalData=“masterkey”.getBytes();
字节[]密文=aead.encrypt(明文,附加数据);
字符串输出=新字符串(密文);
log.info(String.format(“加密文本:%s”,输出));
返回输出;
}
公共字符串解密(字符串文本)引发GeneralSecurityException{
log.info(String.format(“解密%s”,文本));
字节[]密文=text.getBytes();
byte[]additionalData=“masterkey”.getBytes();
字节[]decipheredData=aead.decrypt(密文,附加数据);
字符串输出=新字符串(解密数据);
log.info(String.format(“解密文本:%s”,输出));
返回输出;
}
}
EncryptionServiceImplTest.java
公共类EncryptionServiceImplTest{
@试验
public void encrypt()引发IOException、GeneralSecurityException{
EncryptionServiceImpl encryptionService=新的EncryptionServiceImpl();
String encryptedText=encryptionService.encrypt(“Hello World”);
断言(encryptedText,Matchers.notNullValue());
}
@试验
public void decrypt()引发IOException、GeneralSecurityException{
EncryptionServiceImpl encryptionService=新的EncryptionServiceImpl();
String encryptedText=encryptionService.encrypt(“Hello World”);
String decrypedText=encryptionService.decrypt(encryptedText);
断言(decrypedText,Matchers.is(“Hello World”);
}
}
例外情况:
信息:密文前缀与密钥匹配,但无法解密: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.runReflectVeCall(FrameworkMethod.java:50)
位于org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
位于org.junit.runners.model.FrameworkMethod.invokeeexplosive(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.junitsetclassexecuter.runTestClass(junitsetclassexecuter.java:114)
位于org.gradle.api.internal.tasks.testing.junit.junitsetclassexecuter.execute(junitsetclassexecuter.java:57)
位于org.gradle.api.internal.tasks.testing.junit.junitestclassprocessor.processTestClass(junitestclassprocessor.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.dis
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;
}
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"));
}