Я новичок в криптографии.Я работаю над документом, чтобы зашифровать и расшифровать строку.Когда я расшифровываю зашифрованную строку, она иногда срабатывает, но иногда выдает ошибку несоответствия тега.Я что-то пропустил?
Вот мой код:
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: Tag mismatch!com.encryption.api.service.EncryptionServiceImplTest> decrypt FAILED
java.security.GeneralSecurityException at EncryptionServiceImplTest.java:25
расшифровка не удалась java.security.GeneralSecurityException: расшифровка не удалась в com.google.crypto.tink.aead.AeadFactory $ 1.java: 109) в com.encryption.api.service.EncryptionServiceImpl.decrypt (EncryptionServiceImpl.java:53) в com.encryption.api.service.EncryptionServiceImplTest.decrypt (EncryptionServiceImplTlect.ho.Imp.Ign.Ign.jp.Собственный метод) в sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62) в sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43) в java.vo.etg.reg ()в org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall (FrameworkMethod.java:50) в org.junit.internal.runners.model.ReflectiveCallable.run (ReflectiveCallable.java:12) в org.junit.del.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.jun4 (ru).junit.runners.BlockJUnit4ClassRunner.runChild (BlockJUnit4ClassRunner.java:57) в org.junit.runners.ParentRunner $ 3.run (ParentRunner.java:290) в org.junit.runners.ParentRunjj) в org.junit.runners.ParentRunner.runChildren (ParentRunner.java:288) в org.junit.runners.ParentRunner.access $ 000 (ParentRunner.java:58) в org.junit.runners.ParentRunner $ 2.evaluate (Java: 268) в org.junit.runners.ParentRunner.run (ParentRunner.java:363) в org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass (JUnitTestClassExecudle.g:.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.intotive.Ignative.Ignative.Noke0invoke (NativeMethodAccessorImpl.java:62) в sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43) в java.lang.reflect.Method.invoke (Method.java:498) в org.Файлgradle.internal.dispatch.ProxyDispatchAdapter $ DispatchingInvocationHandler.invoke (ProxyDispatchAdapter.java:93) по адресу com.sun.proxy.processTestClass (TestWorker.java:108) в sun.reflect.NativeMethodAccessorImpl.invoke0 (собственный метод) в sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62) в sun.reflect.DelegatingMethodAccessorImpl.invod.hoho.javaMethod.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)
at java.lang.Thread.run (Thread.java:748)
1 тест завершен, 1 не пройден