Java Code Examples for net.bither.bitherj.crypto.ECKey#getPrivKeyBytes()
The following examples show how to use
net.bither.bitherj.crypto.ECKey#getPrivKeyBytes() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Bip38.java From bitherj with Apache License 2.0 | 4 votes |
public static String encryptNoEcMultiply(byte[] stretcedKeyMaterial, ECKey key, byte[] salt) { // Encoded result int checksumLength = 4; byte[] encoded = new byte[39 + checksumLength]; int index = 0; encoded[index++] = (byte) 0x01; encoded[index++] = (byte) 0x42; // Flags byte byte non_EC_multiplied = (byte) 0xC0; byte compressedPublicKey = key.isCompressed() ? (byte) 0x20 : (byte) 0; encoded[index++] = (byte) (non_EC_multiplied | compressedPublicKey); // Salt System.arraycopy(salt, 0, encoded, index, salt.length); index += salt.length; // Derive Keys byte[] derivedHalf1 = new byte[32]; System.arraycopy(stretcedKeyMaterial, 0, derivedHalf1, 0, 32); byte[] derivedHalf2 = new byte[32]; System.arraycopy(stretcedKeyMaterial, 32, derivedHalf2, 0, 32); // Initialize AES key Rijndael aes = new Rijndael(); aes.makeKey(derivedHalf2, 256); // Get private key bytes byte[] complete = key.getPrivKeyBytes(); // Insert first encrypted key part byte[] toEncryptPart1 = new byte[16]; for (int i = 0; i < 16; i++) { toEncryptPart1[i] = (byte) ((((int) complete[i]) & 0xFF) ^ (((int) derivedHalf1[i]) & 0xFF)); } byte[] encryptedHalf1 = new byte[16]; aes.encrypt(toEncryptPart1, encryptedHalf1); System.arraycopy(encryptedHalf1, 0, encoded, index, encryptedHalf1.length); index += encryptedHalf1.length; // Insert second encrypted key part byte[] toEncryptPart2 = new byte[16]; for (int i = 0; i < 16; i++) { toEncryptPart2[i] = (byte) ((((int) complete[16 + i]) & 0xFF) ^ (((int) derivedHalf1[16 + i]) & 0xFF)); } byte[] encryptedHalf2 = new byte[16]; aes.encrypt(toEncryptPart2, encryptedHalf2); System.arraycopy(encryptedHalf2, 0, encoded, index, encryptedHalf2.length); index += encryptedHalf2.length; // Checksum Sha256Hash checkSum = Bip38Util.doubleSha256(encoded, 0, 39); byte[] start = checkSum.firstFourBytes(); System.arraycopy(start, 0, encoded, 39, checksumLength); // Base58 encode String result = Bip38Util.encode(encoded); return result; }
Example 2
Source File: Bip38.java From bitherj with Apache License 2.0 | 4 votes |
public static SecureCharSequence decryptNoEcMultiply(Bip38PrivateKey bip38Key, byte[] stretcedKeyMaterial) throws AddressFormatException { // Derive Keys byte[] derivedHalf1 = new byte[32]; System.arraycopy(stretcedKeyMaterial, 0, derivedHalf1, 0, 32); byte[] derivedHalf2 = new byte[32]; System.arraycopy(stretcedKeyMaterial, 32, derivedHalf2, 0, 32); // Initialize AES key Rijndael aes = new Rijndael(); aes.makeKey(derivedHalf2, 256); // Fetch first encrypted half byte[] encryptedHalf1 = new byte[16]; System.arraycopy(bip38Key.data, 0, encryptedHalf1, 0, encryptedHalf1.length); // Fetch second encrypted half byte[] encryptedHalf2 = new byte[16]; System.arraycopy(bip38Key.data, 16, encryptedHalf2, 0, encryptedHalf2.length); byte[] decryptedHalf1 = new byte[16]; aes.decrypt(encryptedHalf1, decryptedHalf1); byte[] decryptedHalf2 = new byte[16]; aes.decrypt(encryptedHalf2, decryptedHalf2); byte[] complete = new byte[32]; for (int i = 0; i < 16; i++) { complete[i] = (byte) ((((int) decryptedHalf1[i]) & 0xFF) ^ (((int) derivedHalf1[i]) & 0xFF)); complete[i + 16] = (byte) ((((int) decryptedHalf2[i]) & 0xFF) ^ (((int) derivedHalf1[i + 16]) & 0xFF)); } // Create private key ECKey key = new ECKey(new BigInteger(1, complete), null, bip38Key.compressed); // Validate result byte[] newSalt = calculateScryptSalt(key.toAddress()); if (!Arrays.equals(bip38Key.salt, newSalt)) { // The passphrase is either invalid or we are on the wrong network return null; } // Get SIPA format DumpedPrivateKey dumpedPrivateKey = new DumpedPrivateKey(key.getPrivKeyBytes(), key.isCompressed()); SecureCharSequence secureCharSequence = dumpedPrivateKey.toSecureCharSequence(); dumpedPrivateKey.clearPrivateKey(); key.clearPrivateKey(); return secureCharSequence; }