ref: master
src/test/java/EncryptionTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
/* * This file is part of cert-encoder * Copyright (c) 2024 Paolo Lulli. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, version 3. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ import net.lulli.encrypt.Pems; import net.lulli.encrypt.pki.PkiManager; import net.lulli.encrypt.symmetric.SymmetricEncryptionManager; import org.junit.Test; import java.io.File; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; import java.util.Arrays; public class EncryptionTest { @Test public void symmetricEncryptDecrypt() { var symmetricEncryptionManager = SymmetricEncryptionManager.INSTANCE; var plainText = "This is some long cleartext"; var key = "abcdefghijklmnopqrstuvz".getBytes(StandardCharsets.UTF_8); byte[] encrypted = symmetricEncryptionManager.encrypt(plainText, key); byte[] decrypted = symmetricEncryptionManager.decrypt(encrypted, key); assert Arrays.equals(plainText.getBytes(), decrypted); } @Test public void pkiEncryptDecrypt() throws Exception { var pkiEncryptionManager = PkiManager.INSTANCE; var outputEncryptedFile = File.createTempFile("pkiEncrDecr.", ".enc"); var outputDencryptedFile = File.createTempFile("pkiEncrDecr", ".dec"); var plainText = "this is a sample text"; var certString = Files.readString(Paths.get(getClass().getResource("local.crt").toURI()), Charset.forName("utf-8")); var keyString = Files.readString(Paths.get(getClass().getResource("local.key").toURI()), Charset.forName("utf-8")); var x509Certificate = Pems.readX509Certificate(certString); byte[] enc = pkiEncryptionManager.encrypt(plainText.getBytes(), x509Certificate); System.out.printf("Cleartext: [%s]\n Encrypted: [%s]\n", plainText, new String(enc)); Files.write(Paths.get(outputEncryptedFile.getAbsolutePath()), enc); var privateKey = Pems.readPKCS8PrivateKey(keyString); long millis = System.currentTimeMillis(); pkiEncryptionManager.decryptToFile(privateKey, enc, outputDencryptedFile.getAbsolutePath() + millis); byte[] decrypted = pkiEncryptionManager.decrypt(privateKey, enc); assert Arrays.equals(plainText.getBytes(), decrypted); } @Test public void pkiSign() throws Exception { var pkiEncryptionManager = PkiManager.INSTANCE; var plainText = "this is a sample text"; var certString = Files.readString(Paths.get(getClass().getResource("local.crt").toURI()), Charset.forName("utf-8")); var keyString = Files.readString(Paths.get(getClass().getResource("local.key").toURI()), Charset.forName("utf-8")); var x509Certificate = Pems.readX509Certificate(certString); var privateKey = Pems.readPKCS8PrivateKey(keyString); byte[] signatureClearText = pkiEncryptionManager.sign(x509Certificate, privateKey, plainText.getBytes()); System.out.printf("signatureClearText: [%s]\n", new String(signatureClearText)); } } |