3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-14 11:15:51 +00:00

ECKey: fix bug where creation time was lost when encrypting/decrypting.

This commit is contained in:
Mike Hearn 2014-03-20 14:23:12 +01:00
parent e397928ec3
commit 822c9011b2
2 changed files with 21 additions and 56 deletions

View File

@ -847,7 +847,9 @@ public class ECKey implements Serializable {
final byte[] privKeyBytes = getPrivKeyBytes();
checkState(privKeyBytes != null, "Private key is not available");
EncryptedPrivateKey encryptedPrivateKey = keyCrypter.encrypt(privKeyBytes, aesKey);
return new ECKey(encryptedPrivateKey, getPubKey(), keyCrypter);
ECKey result = new ECKey(encryptedPrivateKey, getPubKey(), keyCrypter);
result.setCreationTimeSeconds(creationTimeSeconds);
return result;
}
/**
@ -869,6 +871,7 @@ public class ECKey implements Serializable {
ECKey key = new ECKey(new BigInteger(1, unencryptedPrivateKey), null, isCompressed());
if (!Arrays.equals(key.getPubKey(), getPubKey()))
throw new KeyCrypterException("Provided AES key is wrong");
key.setCreationTimeSeconds(creationTimeSeconds);
return key;
}

View File

@ -50,6 +50,7 @@ import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import static com.google.bitcoin.core.Utils.reverseBytes;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.junit.Assert.*;
public class ECKeyTest {
@ -253,55 +254,25 @@ public class ECKeyTest {
@Test
public void testUnencryptedCreate() throws Exception {
ECKey unencryptedKey = new ECKey();
// The key should initially be unencrypted.
assertTrue(!unencryptedKey.isEncrypted());
// Copy the private key bytes for checking later.
byte[] originalPrivateKeyBytes = new byte[32];
System.arraycopy(unencryptedKey.getPrivKeyBytes(), 0, originalPrivateKeyBytes, 0, 32);
log.info("Original private key = " + Utils.bytesToHexString(originalPrivateKeyBytes));
// Encrypt the key.
ECKey encryptedKey = unencryptedKey.encrypt(keyCrypter, keyCrypter.deriveKey(PASSWORD1));
// The key should now be encrypted.
assertTrue("Key is not encrypted but it should be", encryptedKey.isEncrypted());
// The unencrypted private key bytes of the encrypted keychain
// should be null or all be blank.
byte[] privateKeyBytes = encryptedKey.getPrivKeyBytes();
if (privateKeyBytes != null) {
for (int i = 0; i < privateKeyBytes.length; i++) {
assertEquals("Byte " + i + " of the private key was not zero but should be", 0, privateKeyBytes[i]);
}
}
// Decrypt the key.
unencryptedKey = encryptedKey.decrypt(keyCrypter, keyCrypter.deriveKey(PASSWORD1));
// The key should be unencrypted
assertTrue("Key is not unencrypted but it should be", !unencryptedKey.isEncrypted());
// The reborn unencrypted private key bytes should match the
// original private key.
privateKeyBytes = unencryptedKey.getPrivKeyBytes();
log.info("Reborn decrypted private key = " + Utils.bytesToHexString(privateKeyBytes));
for (int i = 0; i < privateKeyBytes.length; i++) {
assertEquals("Byte " + i + " of the private key did not match the original", originalPrivateKeyBytes[i],
privateKeyBytes[i]);
}
Utils.rollMockClock(0);
ECKey key = new ECKey();
long time = key.getCreationTimeSeconds();
assertNotEquals(0, time);
assertTrue(!key.isEncrypted());
byte[] originalPrivateKeyBytes = key.getPrivKeyBytes();
ECKey encryptedKey = key.encrypt(keyCrypter, keyCrypter.deriveKey(PASSWORD1));
assertEquals(time, encryptedKey.getCreationTimeSeconds());
assertTrue(encryptedKey.isEncrypted());
assertNull(encryptedKey.getPrivKeyBytes());
key = encryptedKey.decrypt(keyCrypter, keyCrypter.deriveKey(PASSWORD1));
assertTrue(!key.isEncrypted());
assertArrayEquals(originalPrivateKeyBytes, key.getPrivKeyBytes());
}
@Test
public void testEncryptedCreate() throws Exception {
ECKey unencryptedKey = new ECKey();
// Copy the private key bytes for checking later.
byte[] originalPrivateKeyBytes = new byte[32];
System.arraycopy(unencryptedKey.getPrivKeyBytes(), 0, originalPrivateKeyBytes, 0, 32);
byte[] originalPrivateKeyBytes = checkNotNull(unencryptedKey.getPrivKeyBytes());
log.info("Original private key = " + Utils.bytesToHexString(originalPrivateKeyBytes));
EncryptedPrivateKey encryptedPrivateKey = keyCrypter.encrypt(unencryptedKey.getPrivKeyBytes(), keyCrypter.deriveKey(PASSWORD1));
@ -320,17 +291,8 @@ public class ECKeyTest {
// Decrypt the key.
ECKey rebornUnencryptedKey = encryptedKey.decrypt(keyCrypter, keyCrypter.deriveKey(PASSWORD1));
// The key should be unencrypted
assertTrue("Key is not unencrypted but it should be", !rebornUnencryptedKey.isEncrypted());
// The reborn unencrypted private key bytes should match the original private key.
privateKeyBytes = rebornUnencryptedKey.getPrivKeyBytes();
log.info("Reborn decrypted private key = " + Utils.bytesToHexString(privateKeyBytes));
for (int i = 0; i < privateKeyBytes.length; i++) {
assertEquals("Byte " + i + " of the private key did not match the original", originalPrivateKeyBytes[i], privateKeyBytes[i]);
}
assertTrue(!rebornUnencryptedKey.isEncrypted());
assertArrayEquals(originalPrivateKeyBytes, rebornUnencryptedKey.getPrivKeyBytes());
}
@Test