Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fixed compression flag with NoEC, added tests

  • Loading branch information...
commit a15fbc453579e1dd48ff1615a9c03416e53f3395 1 parent f9e47dc
Diego Basch authored
5 src/main/java/com/fruitcat/bitcoin/BIP38.java
View
@@ -126,7 +126,7 @@ public static GeneratedKey encryptedKeyFromIntermediate(byte[] intermediate) thr
* @return a string with the encoded confirmation.
* @throws GeneralSecurityException
*/
- private static String confirm(byte flagByte, byte[] addressHash, byte [] ownerEntropy, byte[] factorB, byte[] derivedHalf1, byte[] derivedHalf2)
+ private static String confirm(byte flagByte, byte[] addressHash, byte[] ownerEntropy, byte[] factorB, byte[] derivedHalf1, byte[] derivedHalf2)
throws GeneralSecurityException {
byte[] pointB = CURVE.getG().multiply(new BigInteger(1, factorB)).getEncoded();
byte pointBPrefix = (byte) (pointB[0] ^ (derivedHalf2[31] & 1));
@@ -363,7 +363,8 @@ public static String decryptNoEC(String passphrase, byte[] encryptedKey) throws
keyBytes[i] = (byte) (k1[i] ^ derivedHalf1[i]);
keyBytes[i + 16] = (byte) (k2[i] ^ derivedHalf1[i + 16]);
}
- boolean compressed = (keyBytes[2] & (byte) 0xe0) == 0;
+
+ boolean compressed = (encryptedKey[2] & (byte) 0x20) == 0x20;
ECKey k = new ECKey(new BigInteger(1, keyBytes), null, compressed);
return k.getPrivateKeyEncoded(MainNetParams.get()).toString();
}
5 src/main/java/com/fruitcat/bitcoin/Utils.java
View
@@ -47,7 +47,7 @@ public static ECPoint compressPoint(ECPoint uncompressed) {
* @return
* @throws NoSuchAlgorithmException
*/
- public static byte[] doubleHash(byte [] data, int off, int len) throws NoSuchAlgorithmException {
+ public static byte[] doubleHash(byte[] data, int off, int len) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(data, off, len);
return md.digest(md.digest());
@@ -121,13 +121,12 @@ public static String base58Check(byte [] b) throws NoSuchAlgorithmException {
catch (java.io.IOException e) {
throw new RuntimeException(e.getMessage());
}
-
}
return baos.toByteArray();
}
//for debugging
- protected static void pb(String name, byte [] x) {
+ protected static void pb(String name, byte[] x) {
System.out.print(name + ": ");
for (byte b : x) {
int l = b >= 0 ? b : 256 + b;
54 src/test/java/com/fruitcat/bitcoin/BIP38Test.java
View
@@ -15,26 +15,62 @@
String testPass = "TestingOneTwoThree";
+ @Test
+ public void noCompressionNoECMultiply() throws Exception {
+ //test 1
+ String encryptedKey = "6PRVWUbkzzsbcVac2qwfssoUJAN1Xhrg6bNk8J7Nzm5H7kxEbn2Nh2ZoGg";
+ String key = "5KN7MzqK5wt2TP1fQCYyHBtDrXdJuXbUzm4A9rKAteGu3Qi5CVR";
+ String decryptedKey = BIP38.decrypt(testPass, encryptedKey);
+ assertEquals(key, decryptedKey);
+ //test 2
+ encryptedKey = "6PRNFFkZc2NZ6dJqFfhRoFNMR9Lnyj7dYGrzdgXXVMXcxoKTePPX1dWByq";
+ key = "5HtasZ6ofTHP6HCwTqTkLDuLQisYPah7aUnSKfC7h4hMUVw2gi5";
+ decryptedKey = BIP38.decrypt("Satoshi", encryptedKey);
+ assertEquals(key, decryptedKey);
+ }
+
+ @Test
+ public void compressionNoECMultiply() throws Exception {
+ //test 1
+ String encryptedKey = "6PYNKZ1EAgYgmQfmNVamxyXVWHzK5s6DGhwP4J5o44cvXdoY7sRzhtpUeo";
+ String key = "L44B5gGEpqEDRS9vVPz7QT35jcBG2r3CZwSwQ4fCewXAhAhqGVpP";
+ String decryptedKey = BIP38.decrypt(testPass, encryptedKey);
+ assertEquals(key, decryptedKey);
+ //test 2
+ encryptedKey = "6PYLtMnXvfG3oJde97zRyLYFZCYizPU5T3LwgdYJz1fRhh16bU7u6PPmY7";
+ key = "KwYgW8gcxj1JWJXhPSu4Fqwzfhp5Yfi42mdYmMa4XqK7NJxXUSK7";
+ decryptedKey = BIP38.decrypt("Satoshi", encryptedKey);
+ assertEquals(key, decryptedKey);
+ }
+
//EC multiply, no compression, no lot/sequence numbers
@Test
- public void decryptECNoCompressionNoLot() throws Exception {
+ public void ecMultiplyNoCompressionNoLot() throws Exception {
+ //test 1
String encryptedKey = "6PfQu77ygVyJLZjfvMLyhLMQbYnu5uguoJJ4kMCLqWwPEdfpwANVS76gTX";
String key = "5K4caxezwjGCGfnoPTZ8tMcJBLB7Jvyjv4xxeacadhq8nLisLR2";
String decryptedKey = BIP38.decrypt(testPass, encryptedKey);
assertEquals(key, decryptedKey);
- key = "5JaG7FiZ8BeR6UVRvmToHGnj1noMFre6F4Y6Kaup8k1P2KNQQbw";
- encryptedKey = "6PfMVKnt1UJzfi1KYfRRANwNEaF3yHpz9dujgFUV5GbYUdyTU6En8DrFAb";
- decryptedKey = BIP38.decrypt("correct horse battery staple", encryptedKey);
+ //test 2
+ key = "5KJ51SgxWaAYR13zd9ReMhJpwrcX47xTJh2D3fGPG9CM8vkv5sH";
+ encryptedKey = "6PfLGnQs6VZnrNpmVKfjotbnQuaJK4KZoPFrAjx1JMJUa1Ft8gnf5WxfKd";
+ decryptedKey = BIP38.decrypt("Satoshi", encryptedKey);
assertEquals(key, decryptedKey);
}
+ //EC multiply, no compression, lot/sequence
@Test
- public void decryptNoECNoCompression() throws Exception {
- String encryptedKey = "6PRVWUbkzzsbcVac2qwfssoUJAN1Xhrg6bNk8J7Nzm5H7kxEbn2Nh2ZoGg";
- String key = "5KN7MzqK5wt2TP1fQCYyHBtDrXdJuXbUzm4A9rKAteGu3Qi5CVR";
- String decryptedKey = BIP38.decrypt(testPass, encryptedKey);
+ public void ecMultiplyNoCompressionLot() throws Exception {
+ //test 1
+ String encryptedKey = "6PgNBNNzDkKdhkT6uJntUXwwzQV8Rr2tZcbkDcuC9DZRsS6AtHts4Ypo1j";
+ String key = "5JLdxTtcTHcfYcmJsNVy1v2PMDx432JPoYcBTVVRHpPaxUrdtf8";
+ String decryptedKey = BIP38.decrypt("MOLON LABE", encryptedKey);
+ assertEquals(key, decryptedKey);
+ //test 2
+ encryptedKey = "6PgGWtx25kUg8QWvwuJAgorN6k9FbE25rv5dMRwu5SKMnfpfVe5mar2ngH";
+ key = "5KMKKuUmAkiNbA3DazMQiLfDq47qs8MAEThm4yL8R2PhV1ov33D";
+ decryptedKey = BIP38.decrypt("ΜΟΛΩΝ ΛΑΒΕ", encryptedKey);
assertEquals(key, decryptedKey);
-
}
//round encrypt and decrypt with a random ascii password
Please sign in to comment.
Something went wrong with that request. Please try again.