net.bither.bitherj.crypto.EncryptedData Java Examples

The following examples show how to use net.bither.bitherj.crypto.EncryptedData. 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: PrivateKeyUtil.java    From bitherj with Apache License 2.0 6 votes vote down vote up
public static HDAccountCold getHDAccountCold(String str, CharSequence password) {
    HDAccountCold hdAccountCold = null;
    String[] strs = QRCodeUtil.splitOfPasswordSeed(str);
    if (strs.length % 3 != 0) {
        log.error("Backup: PrivateKeyFromString format error");
        return null;
    }
    for (int i = 0;
         i < strs.length;
         i += 3) {

        if (strs[i].indexOf(QRCodeUtil.HD_QR_CODE_FLAG) == 0) {
            try {
                String encryptedString = strs[i].substring(1) + QRCodeUtil.QR_CODE_SPLIT + strs[i + 1]
                        + QRCodeUtil.QR_CODE_SPLIT + strs[i + 2];
                hdAccountCold = new HDAccountCold(new EncryptedData(encryptedString), password);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    return hdAccountCold;
}
 
Example #2
Source File: HDMBId.java    From bitherj with Apache License 2.0 6 votes vote down vote up
public String setSignatureAndGetAddressOfAddressOfSp(byte[] signed, CharSequence password, String firstHotAddress) throws Exception {
    String addressOfSP = null;
    String message = getBitidString();
    byte[] hash = Utils.getPreSignMessage(message);
    ECKey key = ECKey.signedMessageToKey(hash, signed);
    if (Utils.compareString(address, key.toAddress())) {
        throw new SignatureException();

    }
    String hotAddress = firstHotAddress != null ? firstHotAddress : AddressManager.getInstance().getHdmKeychain().getFirstAddressFromDb();
    UploadHDMBidApi uploadHDMBidApi = new UploadHDMBidApi(address, hotAddress, signed, decryptedPassword);
    uploadHDMBidApi.handleHttpPost();
    boolean result = uploadHDMBidApi.getResult();
    if (result) {
        encryptedBitherPassword = new EncryptedData(decryptedPassword, password);
        ECKey k = new ECKey(decryptedPassword, null);
        addressOfSP = k.toAddress();
        k.clearPrivateKey();
        if (firstHotAddress == null) {
            save(addressOfSP);
        }
    } else {
        throw new HttpException("UploadHDMBidApi error");
    }
    return addressOfSP;
}
 
Example #3
Source File: DesktopHDMKeychain.java    From bitherj with Apache License 2.0 6 votes vote down vote up
private void initHDAccount(DeterministicKey master, EncryptedData encryptedMnemonicSeed,
                           EncryptedData encryptedHDSeed, boolean isSyncedComplete) {
    String firstAddress;
    ECKey k = new ECKey(mnemonicSeed, null);
    String address = k.toAddress();
    k.clearPrivateKey();
    DeterministicKey accountKey = getAccount(master);
    DeterministicKey internalKey = getChainRootKey(accountKey, AbstractHD.PathType.INTERNAL_ROOT_PATH);
    DeterministicKey externalKey = getChainRootKey(accountKey, AbstractHD.PathType.EXTERNAL_ROOT_PATH);
    DeterministicKey key = externalKey.deriveSoftened(0);
    firstAddress = key.toAddress();
    accountKey.wipe();
    master.wipe();

    wipeHDSeed();
    wipeMnemonicSeed();
    hdSeedId = AbstractDb.desktopAddressProvider.addHDKey(encryptedMnemonicSeed.toEncryptedString(),
            encryptedHDSeed.toEncryptedString(), firstAddress, isFromXRandom, address, externalKey.getPubKeyExtended(), internalKey
                    .getPubKeyExtended());
    internalKey.wipe();
    externalKey.wipe();


}
 
Example #4
Source File: AddEnterpriseHDMSeedActivity.java    From bither-android with Apache License 2.0 5 votes vote down vote up
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == ImportFromQRTag) {
        final String seedStr = data.getStringExtra(ScanActivity.INTENT_EXTRA_RESULT);
        if (seedStr.indexOf(QRCodeUtil.Enterprise_HDM_QR_CODE_FLAG) == 0) {
            DialogPassword dialogPassword = new DialogPassword(this, new
                    ImportSeedPasswordListener(seedStr));
            dialogPassword.setCheckPre(false);
            dialogPassword.setCheckPasswordListener(new ICheckPasswordListener() {
                @Override
                public boolean checkPassword(SecureCharSequence password) {
                    String keyString = seedStr.substring(1);
                    String[] passwordSeeds = QRCodeUtil.splitOfPasswordSeed(keyString);
                    String encreyptString = Utils.joinString(new String[]{passwordSeeds[0],
                            passwordSeeds[1], passwordSeeds[2]}, QRCodeUtil.QR_CODE_SPLIT);
                    EncryptedData encryptedData = new EncryptedData(encreyptString);
                    byte[] result = null;
                    try {
                        result = encryptedData.decrypt(password);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return result != null;
                }
            });
            dialogPassword.setTitle(R.string.import_private_key_qr_code_password);
            dialogPassword.show();
        } else {
            DropdownMessage.showDropdownMessage(AddEnterpriseHDMSeedActivity.this, R.string
                    .enterprise_hdm_seed_import_format_error);
        }
        return;
    }
    super.onActivityResult(requestCode, resultCode, data);
}
 
Example #5
Source File: HDAccountCold.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public HDAccountCold(byte[] mnemonicSeed, CharSequence password, boolean isFromXRandom)
        throws MnemonicException.MnemonicLengthException {
    this.mnemonicSeed = mnemonicSeed;
    hdSeed = seedFromMnemonic(mnemonicSeed);
    this.isFromXRandom = isFromXRandom;
    DeterministicKey master = HDKeyDerivation.createMasterPrivateKey(hdSeed);
    EncryptedData encryptedHDSeed = new EncryptedData(hdSeed, password, isFromXRandom);
    EncryptedData encryptedMnemonicSeed = new EncryptedData(mnemonicSeed, password,
            isFromXRandom);
    ECKey k = new ECKey(mnemonicSeed, null);
    String address = k.toAddress();
    k.clearPrivateKey();
    DeterministicKey accountKey = getAccount(master);
    DeterministicKey externalKey = getChainRootKey(accountKey, AbstractHD.PathType
            .EXTERNAL_ROOT_PATH);
    DeterministicKey internalKey = getChainRootKey(accountKey, PathType
            .INTERNAL_ROOT_PATH);
    DeterministicKey key = externalKey.deriveSoftened(0);
    String firstAddress = key.toAddress();
    accountKey.wipe();
    master.wipe();
    wipeHDSeed();
    wipeMnemonicSeed();
    hdSeedId = AbstractDb.hdAccountProvider.addHDAccount(encryptedMnemonicSeed
                    .toEncryptedString(), encryptedHDSeed.toEncryptedString(), firstAddress,
            isFromXRandom, address, externalKey.getPubKeyExtended(), internalKey
                    .getPubKeyExtended());
    externalKey.wipe();
}
 
Example #6
Source File: HDMKeychain.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public static boolean checkPassword(String keysString, CharSequence password) throws
        MnemonicException.MnemonicLengthException {
    String[] passwordSeeds = QRCodeUtil.splitOfPasswordSeed(keysString);
    String address = Base58.hexToBase58WithAddress(passwordSeeds[0]);
    String encreyptString = Utils.joinString(new String[]{passwordSeeds[1], passwordSeeds[2],
            passwordSeeds[3]}, QRCodeUtil.QR_CODE_SPLIT);
    byte[] seed = new EncryptedData(encreyptString).decrypt(password);
    MnemonicCode mnemonic = MnemonicCode.instance();

    byte[] s = mnemonic.toSeed(mnemonic.toMnemonic(seed), "");

    DeterministicKey master = HDKeyDerivation.createMasterPrivateKey(s);

    DeterministicKey purpose = master.deriveHardened(44);

    DeterministicKey coinType = purpose.deriveHardened(0);

    DeterministicKey account = coinType.deriveHardened(0);

    DeterministicKey external = account.deriveSoftened(0);

    external.clearPrivateKey();

    DeterministicKey key = external.deriveSoftened(0);
    boolean result = Utils.compareString(address, Utils.toAddress(key.getPubKeyHash()));
    key.wipe();

    return result;
}
 
Example #7
Source File: HDMKeychain.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public PasswordSeed createPasswordSeed(CharSequence password) {
    if (isInRecovery()) {
        throw new AssertionError("HDM in recovery can not create passwordSeed");
    }
    String encrypted = AbstractDb.addressProvider.getEncryptMnemonicSeed(hdSeedId);
    byte[] priv = new EncryptedData(encrypted).decrypt(password);
    ECKey k = new ECKey(priv, null);
    String address = k.toAddress();
    Utils.wipeBytes(priv);
    k.clearPrivateKey();
    return new PasswordSeed(address, encrypted);
}
 
Example #8
Source File: HDMKeychain.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public boolean checkSingularBackupWithPassword(CharSequence password) {
    if (isInRecovery()) {
        return true;
    }
    if (getAllCompletedAddresses().size() == 0) {
        return true;
    }
    String backup = AbstractDb.addressProvider.getSingularModeBackup(getHdSeedId());
    if (backup == null) {
        return true;
    }
    EncryptedData encrypted = new EncryptedData(backup);
    byte[] mnemonic = encrypted.decrypt(password);
    boolean result;
    try {
        byte[] seed = seedFromMnemonic(mnemonic);
        byte[] pub = getAllCompletedAddresses().get(0).getPubCold();
        DeterministicKey master = HDKeyDerivation.createMasterPrivateKey(seed);
        DeterministicKey purpose = master.deriveHardened(44);
        DeterministicKey coinType = purpose.deriveHardened(0);
        DeterministicKey account = coinType.deriveHardened(0);
        DeterministicKey external = account.deriveSoftened(0);
        DeterministicKey first = external.deriveSoftened(0);
        master.wipe();
        purpose.wipe();
        coinType.wipe();
        account.wipe();
        external.wipe();
        Utils.wipeBytes(seed);
        result = Arrays.equals(first.getPubKey(), pub);
        first.wipe();
    } catch (MnemonicException.MnemonicLengthException e) {
        e.printStackTrace();
        result = false;
    }
    Utils.wipeBytes(mnemonic);
    return result;
}
 
Example #9
Source File: HDAccount.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public void decryptMnemonicSeed(CharSequence password) throws KeyCrypterException {
    if (hdSeedId < 0) {
        return;
    }
    String encrypted = getEncryptedMnemonicSeed();
    if (!Utils.isEmpty(encrypted)) {
        mnemonicSeed = new EncryptedData(encrypted).decrypt(password);
    }
}
 
Example #10
Source File: HDAccount.java    From bitherj with Apache License 2.0 5 votes vote down vote up
protected void decryptHDSeed(CharSequence password) throws MnemonicException
        .MnemonicLengthException {
    if (hdSeedId < 0 || password == null) {
        return;
    }
    String encryptedHDSeed = getEncryptedHDSeed();
    if (!Utils.isEmpty(encryptedHDSeed)) {
        hdSeed = new EncryptedData(encryptedHDSeed).decrypt(password);
    }
}
 
Example #11
Source File: HDAccount.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public HDAccount(EncryptedData encryptedMnemonicSeed, CharSequence password, boolean
        isSyncedComplete)
        throws MnemonicException.MnemonicLengthException {
    mnemonicSeed = encryptedMnemonicSeed.decrypt(password);
    hdSeed = seedFromMnemonic(mnemonicSeed);
    isFromXRandom = encryptedMnemonicSeed.isXRandom();
    EncryptedData encryptedHDSeed = new EncryptedData(hdSeed, password, isFromXRandom);
    DeterministicKey master = HDKeyDerivation.createMasterPrivateKey(hdSeed);
    DeterministicKey account = getAccount(master);
    account.clearPrivateKey();
    initHDAccount(account, encryptedMnemonicSeed, encryptedHDSeed, isFromXRandom,
            isSyncedComplete, null);
}
 
Example #12
Source File: HDAccount.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public HDAccount(SecureRandom random, CharSequence password, HDAccountGenerationDelegate generationDelegate) throws MnemonicException.MnemonicLengthException {
    isFromXRandom = random.getClass().getCanonicalName().indexOf("XRandom") >= 0;
    mnemonicSeed = new byte[16];
    random.nextBytes(mnemonicSeed);
    hdSeed = seedFromMnemonic(mnemonicSeed);
    EncryptedData encryptedHDSeed = new EncryptedData(hdSeed, password, isFromXRandom);
    EncryptedData encryptedMnemonicSeed = new EncryptedData(mnemonicSeed, password,
            isFromXRandom);
    DeterministicKey master = HDKeyDerivation.createMasterPrivateKey(hdSeed);
    DeterministicKey account = getAccount(master);
    account.clearPrivateKey();
    initHDAccount(account, encryptedMnemonicSeed, encryptedHDSeed, isFromXRandom, true,
            generationDelegate);
}
 
Example #13
Source File: HDAccount.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public HDAccount(byte[] mnemonicSeed, CharSequence password, boolean isSyncedComplete) throws
        MnemonicException
        .MnemonicLengthException {
    super();
    this.mnemonicSeed = mnemonicSeed;
    hdSeed = seedFromMnemonic(mnemonicSeed);
    DeterministicKey master = HDKeyDerivation.createMasterPrivateKey(hdSeed);
    EncryptedData encryptedHDSeed = new EncryptedData(hdSeed, password, isFromXRandom);
    EncryptedData encryptedMnemonicSeed = new EncryptedData(mnemonicSeed, password,
            isFromXRandom);
    DeterministicKey account = getAccount(master);
    account.clearPrivateKey();
    initHDAccount(account, encryptedMnemonicSeed, encryptedHDSeed, isFromXRandom,
            isSyncedComplete, null);
}
 
Example #14
Source File: DesktopHDMKeychain.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public static boolean checkPassword(String keysString, CharSequence password) throws
        MnemonicException.MnemonicLengthException {
    String[] passwordSeeds = QRCodeUtil.splitOfPasswordSeed(keysString);
    String address = Base58.hexToBase58WithAddress(passwordSeeds[0]);
    String encreyptString = Utils.joinString(new String[]{passwordSeeds[1], passwordSeeds[2],
            passwordSeeds[3]}, QRCodeUtil.QR_CODE_SPLIT);
    byte[] seed = new EncryptedData(encreyptString).decrypt(password);
    MnemonicCode mnemonic = MnemonicCode.instance();

    byte[] s = mnemonic.toSeed(mnemonic.toMnemonic(seed), "");

    DeterministicKey master = HDKeyDerivation.createMasterPrivateKey(s);

    DeterministicKey purpose = master.deriveHardened(44);

    DeterministicKey coinType = purpose.deriveHardened(0);

    DeterministicKey account = coinType.deriveHardened(0);

    DeterministicKey external = account.deriveSoftened(0);

    external.clearPrivateKey();

    DeterministicKey key = external.deriveSoftened(0);
    boolean result = Utils.compareString(address, Utils.toAddress(key.getPubKeyHash()));
    key.wipe();

    return result;
}
 
Example #15
Source File: DesktopHDMKeychain.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public DesktopHDMKeychain(EncryptedData encryptedMnemonicSeed, CharSequence password) throws
        HDMBitherIdNotMatchException, MnemonicException.MnemonicLengthException {
    mnemonicSeed = encryptedMnemonicSeed.decrypt(password);
    hdSeed = seedFromMnemonic(mnemonicSeed);
    isFromXRandom = encryptedMnemonicSeed.isXRandom();
    EncryptedData encryptedHDSeed = new EncryptedData(hdSeed, password, isFromXRandom);
    ArrayList<DesktopHDMAddress> as = new ArrayList<DesktopHDMAddress>();
    ArrayList<HDMAddress.Pubs> uncompPubs = new ArrayList<HDMAddress.Pubs>();

    ECKey k = new ECKey(mnemonicSeed, null);
    String address = k.toAddress();
    k.clearPrivateKey();
    String firstAddress = getFirstAddressFromSeed(password);
    wipeMnemonicSeed();
    wipeHDSeed();

    this.hdSeedId = AbstractDb.desktopAddressProvider.addHDKey(encryptedMnemonicSeed
                    .toEncryptedString(), encryptedHDSeed.toEncryptedString(), firstAddress,
            isFromXRandom, address, null, null);
    if (as.size() > 0) {
        //   EnDesktopAddressProvider.getInstance().completeHDMAddresses(getHdSeedId(), as);

        if (uncompPubs.size() > 0) {
            //  EnDesktopAddressProvider.getInstance().prepareHDMAddresses(getHdSeedId(), uncompPubs);
            for (HDMAddress.Pubs p : uncompPubs) {
                AbstractDb.addressProvider.setHDMPubsRemote(getHdSeedId(), p.index, p.remote);
            }
        }
    }
}
 
Example #16
Source File: HDMBId.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public byte[] decryptHDMBIdPassword(CharSequence password) {
    HDMBId hdmbId = AbstractDb.addressProvider.getHDMBId();
    if (!Utils.isEmpty(hdmbId.getEncryptedBitherPasswordString())) {
        encryptedBitherPassword = new EncryptedData(hdmbId.getEncryptedBitherPasswordString());
    }
    decryptedPassword = encryptedBitherPassword.decrypt(password);
    return decryptedPassword;
}
 
Example #17
Source File: AbstractHD.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public void decryptMnemonicSeed(CharSequence password) throws KeyCrypterException {
    if (hdSeedId < 0) {
        return;
    }
    String encrypted = getEncryptedMnemonicSeed();
    if (!Utils.isEmpty(encrypted)) {
        mnemonicSeed = new EncryptedData(encrypted).decrypt(password);
    }
}
 
Example #18
Source File: AbstractHD.java    From bitherj with Apache License 2.0 5 votes vote down vote up
private void initHDSeedFromMnemonicSeed(CharSequence password) throws MnemonicException
        .MnemonicLengthException {
    decryptMnemonicSeed(password);
    hdSeed = seedFromMnemonic(mnemonicSeed);
    wipeMnemonicSeed();
    AbstractDb.addressProvider.updateEncrypttMnmonicSeed(getHdSeedId(), new EncryptedData(hdSeed,
            password, isFromXRandom).toEncryptedString());
}
 
Example #19
Source File: AbstractHD.java    From bitherj with Apache License 2.0 5 votes vote down vote up
protected void decryptHDSeed(CharSequence password) throws MnemonicException
        .MnemonicLengthException {
    if (hdSeedId < 0 || password == null) {
        return;
    }
    String encryptedHDSeed = getEncryptedHDSeed();
    if (Utils.isEmpty(encryptedHDSeed)) {
        initHDSeedFromMnemonicSeed(password);
    } else {
        hdSeed = new EncryptedData(encryptedHDSeed).decrypt(password);
    }
}
 
Example #20
Source File: HDMSingular.java    From bitherj with Apache License 2.0 5 votes vote down vote up
private void setEntropyInterval(byte[] entropy, boolean xrandom) {
    hotMnemonicSeed = Arrays.copyOf(entropy, 32);
    coldMnemonicSeed = Arrays.copyOfRange(entropy, 32, 64);
    Utils.wipeBytes(entropy);
    initHotFirst();
    encryptedColdMnemonicSeed = new EncryptedData(coldMnemonicSeed, password, xrandom);
    coldQr = QRCodeUtil.HDM_QR_CODE_FLAG + PrivateKeyUtil.getFullencryptHDMKeyChain(xrandom, encryptedColdMnemonicSeed.toEncryptedString());
}
 
Example #21
Source File: AddEnterpriseHDMSeedActivity.java    From bither-android with Apache License 2.0 5 votes vote down vote up
@Override
public void onPasswordEntered(final SecureCharSequence password) {
    if (dp != null && !dp.isShowing()) {
        dp.setMessage(R.string.import_private_key_qr_code_importing);
    }
    new ThreadNeedService(dp, AddEnterpriseHDMSeedActivity.this) {

        @Override
        public void runWithService(BlockchainService service) {
            EncryptedData data = new EncryptedData(content.substring(1));
            try {
                EnterpriseHDMSeed seed = new EnterpriseHDMSeed(data.decrypt(new
                        SecureCharSequence(password)), data.isXRandom(), password);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        dp.dismiss();
                        setResult(RESULT_OK);
                        finish();
                    }
                });
            } catch (MnemonicException.MnemonicLengthException e) {
                e.printStackTrace();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        dp.dismiss();
                        DropdownMessage.showDropdownMessage(AddEnterpriseHDMSeedActivity
                                .this, R.string.enterprise_hdm_seed_add_fail);
                    }
                });
            }
        }
    }.start();
}
 
Example #22
Source File: HDMBId.java    From bitherj with Apache License 2.0 4 votes vote down vote up
public void setEncryptedData(EncryptedData encryptedData) {
    this.encryptedBitherPassword = encryptedData;
}
 
Example #23
Source File: HDMBId.java    From bitherj with Apache License 2.0 4 votes vote down vote up
public HDMBId(String addressForBitherId, EncryptedData encryptedBitherPassword) {
    this.address = addressForBitherId;
    this.encryptedBitherPassword = encryptedBitherPassword;
}
 
Example #24
Source File: HDMBId.java    From bitherj with Apache License 2.0 4 votes vote down vote up
public HDMBId(String addressForBitherId, String encryptedBitherPassword) {
    this(addressForBitherId, new EncryptedData(encryptedBitherPassword));
}
 
Example #25
Source File: HDAccountCold.java    From bitherj with Apache License 2.0 4 votes vote down vote up
public HDAccountCold(EncryptedData encryptedMnemonicSeed, CharSequence password) throws
        MnemonicException.MnemonicLengthException {
    this(encryptedMnemonicSeed.decrypt(password), password, encryptedMnemonicSeed.isXRandom());
}