use of com.icodici.crypto.AbstractPrivateKey in project universa by UniversaBlockchain.
the class RSAOAEPPrivateKeyTest method decrypt.
/**
* Test {@link RSAOAEPPrivateKey#decrypt}.
*/
@Test
public void decrypt() throws Exception {
// Test on RSA vectors.
AbstractPrivateKey rsaPrivateKey = oaepSpec.getPrivateKey();
AbstractPublicKey rsaPublicKey = rsaPrivateKey.getPublicKey();
((RSAOAEPPrivateKey) rsaPrivateKey).resetDecryptor();
assertArrayEquals(rsaPrivateKey.decrypt(oaepSpec.C), oaepSpec.M);
((RSAOAEPPublicKey) rsaPublicKey).resetEncryptor();
assertArrayEquals(rsaPublicKey.encrypt(oaepSpec.M), oaepSpec.C);
// Test on some random data.
AbstractPrivateKey randomPrivateKey = randomPrivateKey4096;
AbstractPublicKey randomPublicKey = randomPrivateKey.getPublicKey();
SecureRandom rng = new SecureRandom();
int maxMessageSize = ((RSAOAEPPrivateKey) randomPrivateKey).getMaxBlockSize(), minMessageSize = maxMessageSize, messageSize = (maxMessageSize >= minMessageSize) ? rng.nextInt(maxMessageSize - minMessageSize + 1) + minMessageSize : 0;
// For our key pair we do multiple encryption-decryption cycles on different data.
for (int i = 0; i < NUMBER_OF_RANDOM_ENCRYPTION_DECRYPTION_CYCLES; i++) {
((RSAOAEPPrivateKey) randomPrivateKey).resetDecryptor();
((RSAOAEPPublicKey) randomPublicKey).resetEncryptor();
// Create random message
byte[] message = new byte[messageSize];
rng.nextBytes(message);
byte[] encrypted = randomPublicKey.encrypt(message), decrypted = randomPrivateKey.decrypt(encrypted);
assertArrayEquals(decrypted, message);
}
}
use of com.icodici.crypto.AbstractPrivateKey in project universa by UniversaBlockchain.
the class RSAOAEPPrivateKeyTest method toHash.
/**
* Test {@link RSAOAEPPrivateKey#toHash}.
*/
@Test
public void toHash() throws Exception {
// Test sample RSA vectors.
AbstractPrivateKey rsaPrivateKey = oaepSpec.getPrivateKey();
Map mapRSA = rsaPrivateKey.toHash();
assertArrayEquals((byte[]) mapRSA.get("e"), oaepSpec.e);
assertArrayEquals((byte[]) mapRSA.get("p"), oaepSpec.p);
assertArrayEquals((byte[]) mapRSA.get("q"), oaepSpec.q);
// SHA-1 is the default value
assertFalse(mapRSA.containsKey("mgf1Hash"));
// Test random key pair (4096 bit, SHA-512).
AbstractPublicKey randomPublicKey4096 = randomPrivateKey4096.getPublicKey();
Map mapPrivate4096 = randomPrivateKey4096.toHash(), mapPublic4096 = randomPublicKey4096.toHash();
RSAPrivateCrtKeyParameters privateKeyParameters4096 = ((RSAOAEPPrivateKey) randomPrivateKey4096).state.keyParameters;
RSAKeyParameters publicKeyParameters4096 = ((RSAOAEPPublicKey) (randomPrivateKey4096.getPublicKey())).state.keyParameters;
// Check private key serialization.
assertArrayEquals((byte[]) mapPrivate4096.get("e"), BigIntegers.asUnsignedByteArray(privateKeyParameters4096.getPublicExponent()));
assertArrayEquals((byte[]) mapPrivate4096.get("p"), BigIntegers.asUnsignedByteArray(privateKeyParameters4096.getP()));
assertArrayEquals((byte[]) mapPrivate4096.get("q"), BigIntegers.asUnsignedByteArray(privateKeyParameters4096.getQ()));
assertEquals(mapPrivate4096.get("mgf1Hash"), "SHA-512");
// Check public key serialization.
assertArrayEquals((byte[]) mapPublic4096.get("n"), BigIntegers.asUnsignedByteArray(publicKeyParameters4096.getModulus()));
assertArrayEquals((byte[]) mapPublic4096.get("e"), BigIntegers.asUnsignedByteArray(publicKeyParameters4096.getExponent()));
assertEquals(mapPublic4096.get("mgf1Hash"), "SHA-512");
}
use of com.icodici.crypto.AbstractPrivateKey in project universa by UniversaBlockchain.
the class RSAOAEPPublicKeyTest method checkSignature.
/**
* Test {@link RSAOAEPPublicKey#checkSignature}.
*/
@Test
public void checkSignature() throws Exception {
// Test sample RSA vectors.
AbstractPublicKey rsaPublicKey = pssSpec.getPublicKey();
AbstractPrivateKey rsaPrivateKey = pssSpec.getPrivateKey();
assertArrayEquals(rsaPrivateKey.sign(pssSpec.M, HashType.SHA1, RSASSAPSSTestVectors.salt), pssSpec.S);
assertTrue(rsaPublicKey.checkSignature(pssSpec.M, rsaPrivateKey.sign(pssSpec.M, HashType.SHA1, RSASSAPSSTestVectors.salt), HashType.SHA1, RSASSAPSSTestVectors.salt.length));
}
Aggregations