2018-06-13 17:26:48 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2018 Paul Schaub.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2020-01-11 13:11:14 +01:00
|
|
|
package org.pgpainless.symmetric_encryption;
|
2018-06-13 17:26:48 +02:00
|
|
|
|
2020-01-10 22:07:54 +01:00
|
|
|
import static org.junit.Assert.assertArrayEquals;
|
2018-06-13 17:26:48 +02:00
|
|
|
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.IOException;
|
2018-07-02 21:40:59 +02:00
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
2018-06-13 17:26:48 +02:00
|
|
|
|
|
|
|
import org.bouncycastle.bcpg.ArmoredOutputStream;
|
|
|
|
import org.bouncycastle.openpgp.PGPException;
|
|
|
|
import org.junit.Test;
|
2020-01-10 22:07:54 +01:00
|
|
|
import org.pgpainless.PGPainless;
|
2018-07-18 18:23:06 +02:00
|
|
|
import org.pgpainless.algorithm.SymmetricKeyAlgorithm;
|
|
|
|
import org.pgpainless.util.Passphrase;
|
2018-06-13 17:26:48 +02:00
|
|
|
|
2020-01-10 22:07:54 +01:00
|
|
|
public class SymmetricEncryptionTest {
|
2018-06-13 17:26:48 +02:00
|
|
|
|
2020-01-10 22:07:54 +01:00
|
|
|
private static final Logger LOGGER = Logger.getLogger(SymmetricEncryptionTest.class.getName());
|
2018-07-02 21:40:59 +02:00
|
|
|
|
2020-01-10 22:07:54 +01:00
|
|
|
private static final String message =
|
|
|
|
"I grew up with the understanding that the world " +
|
2018-06-13 17:26:48 +02:00
|
|
|
"I lived in was one where people enjoyed a sort of freedom " +
|
|
|
|
"to communicate with each other in privacy, without it " +
|
|
|
|
"being monitored, without it being measured or analyzed " +
|
|
|
|
"or sort of judged by these shadowy figures or systems, " +
|
|
|
|
"any time they mention anything that travels across " +
|
|
|
|
"public lines.\n" +
|
|
|
|
"\n" +
|
|
|
|
"- Edward Snowden -";
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testSymmetricEncryptionDecryption() throws IOException, PGPException {
|
|
|
|
byte[] plain = message.getBytes();
|
2020-04-11 11:35:48 +02:00
|
|
|
String password = "choose_a_better_password_please";
|
|
|
|
Passphrase passphrase = new Passphrase(password.toCharArray());
|
2018-07-12 23:16:30 +02:00
|
|
|
byte[] enc = PGPainless.encryptWithPassword(plain, passphrase, SymmetricKeyAlgorithm.AES_128);
|
2018-06-13 17:26:48 +02:00
|
|
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
|
|
ArmoredOutputStream armor = new ArmoredOutputStream(out);
|
|
|
|
armor.write(enc);
|
|
|
|
armor.flush();
|
|
|
|
armor.close();
|
|
|
|
|
|
|
|
// Print cipher text for validation with GnuPG.
|
2020-04-11 11:35:48 +02:00
|
|
|
LOGGER.log(Level.INFO, String.format("Use ciphertext below for manual validation with GnuPG " +
|
|
|
|
"(passphrase = '%s').\n\n%s", password, new String(out.toByteArray())));
|
2018-06-13 17:26:48 +02:00
|
|
|
|
2018-07-12 23:16:30 +02:00
|
|
|
byte[] plain2 = PGPainless.decryptWithPassword(enc, passphrase);
|
2020-01-10 22:07:54 +01:00
|
|
|
assertArrayEquals(plain, plain2);
|
2018-06-13 17:26:48 +02:00
|
|
|
}
|
|
|
|
}
|