mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-13 11:19:04 +01:00
ECIES encryption for feedback and crash reports.
This commit is contained in:
47
briar-tests/src/org/briarproject/crypto/AsciiArmourTest.java
Normal file
47
briar-tests/src/org/briarproject/crypto/AsciiArmourTest.java
Normal file
@@ -0,0 +1,47 @@
|
||||
package org.briarproject.crypto;
|
||||
|
||||
import org.briarproject.BriarTestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class AsciiArmourTest extends BriarTestCase {
|
||||
|
||||
@Test
|
||||
public void testWrapOnSingleLine() {
|
||||
byte[] b = new byte[8];
|
||||
for (int i = 0; i < b.length; i++) b[i] = (byte) i;
|
||||
String expected = "0001020304050607\r\n";
|
||||
assertEquals(expected, AsciiArmour.wrap(b, 70));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWrapOnMultipleLines() {
|
||||
byte[] b = new byte[8];
|
||||
for (int i = 0; i < b.length; i++) b[i] = (byte) i;
|
||||
String expected = "0001020\r\n3040506\r\n07\r\n";
|
||||
assertEquals(expected, AsciiArmour.wrap(b, 7));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnwrapOnSingleLine() throws Exception {
|
||||
String s = "0001020304050607";
|
||||
byte[] expected = new byte[] {0, 1, 2, 3, 4, 5, 6, 7};
|
||||
assertArrayEquals(expected, AsciiArmour.unwrap(s));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnwrapOnMultipleLines() throws Exception {
|
||||
String s = "0001020\r\n3040506\r\n07";
|
||||
byte[] expected = new byte[] {0, 1, 2, 3, 4, 5, 6, 7};
|
||||
assertArrayEquals(expected, AsciiArmour.unwrap(s));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnwrapWithJunkCharacters() throws Exception {
|
||||
String s = "0001??020\rzz\n30z40..506\r\n07;;";
|
||||
byte[] expected = new byte[] {0, 1, 2, 3, 4, 5, 6, 7};
|
||||
assertArrayEquals(expected, AsciiArmour.unwrap(s));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package org.briarproject.crypto;
|
||||
|
||||
import org.briarproject.BriarTestCase;
|
||||
import org.junit.Test;
|
||||
import org.spongycastle.crypto.AsymmetricCipherKeyPair;
|
||||
import org.spongycastle.crypto.CryptoException;
|
||||
import org.spongycastle.crypto.params.ECPrivateKeyParameters;
|
||||
import org.spongycastle.crypto.params.ECPublicKeyParameters;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
|
||||
public class MessageEncrypterTest extends BriarTestCase {
|
||||
|
||||
private final SecureRandom random = new SecureRandom();
|
||||
|
||||
@Test
|
||||
public void testEncryptionAndDecryption() throws Exception {
|
||||
MessageEncrypter m = new MessageEncrypter(random);
|
||||
AsymmetricCipherKeyPair kp = m.generateKeyPair();
|
||||
ECPublicKeyParameters pub = (ECPublicKeyParameters) kp.getPublic();
|
||||
ECPrivateKeyParameters priv = (ECPrivateKeyParameters) kp.getPrivate();
|
||||
byte[] plaintext = new byte[123];
|
||||
random.nextBytes(plaintext);
|
||||
byte[] ciphertext = m.encrypt(pub, plaintext);
|
||||
byte[] decrypted = m.decrypt(priv, ciphertext);
|
||||
assertArrayEquals(plaintext, decrypted);
|
||||
}
|
||||
|
||||
@Test(expected = CryptoException.class)
|
||||
public void testDecryptionFailsWithAlteredCiphertext() throws Exception {
|
||||
MessageEncrypter m = new MessageEncrypter(random);
|
||||
AsymmetricCipherKeyPair kp = m.generateKeyPair();
|
||||
ECPublicKeyParameters pub = (ECPublicKeyParameters) kp.getPublic();
|
||||
ECPrivateKeyParameters priv = (ECPrivateKeyParameters) kp.getPrivate();
|
||||
byte[] ciphertext = m.encrypt(pub, new byte[123]);
|
||||
ciphertext[random.nextInt(ciphertext.length)] ^= 0xFF;
|
||||
m.decrypt(priv, ciphertext);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user