Java Code Examples for net.bither.bitherj.utils.PrivateKeyUtil#encrypt()

The following examples show how to use net.bither.bitherj.utils.PrivateKeyUtil#encrypt() . 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: KeyUtil.java    From bither-desktop-java with Apache License 2.0 6 votes vote down vote up
public static List<Address> addPrivateKeyByRandomWithPassphras(IUEntropy iuEntropy, CharSequence password, int count) {
    PeerUtil.stopPeer();
    List<Address> addressList = new ArrayList<Address>();
    for (int i = 0; i < count; i++) {
        XRandom xRandom = new XRandom(iuEntropy);
        ECKey ecKey = ECKey.generateECKey(xRandom);
        ecKey = PrivateKeyUtil.encrypt(ecKey, password);
        Address address = new Address(ecKey.toAddress(),
                ecKey.getPubKey(), PrivateKeyUtil.getEncryptedString(ecKey), ecKey.isFromXRandom());
        ecKey.clearPrivateKey();
        addressList.add(address);
        AddressManager.getInstance().addAddress(address);

    }
    PeerUtil.startPeer();
    if (UserPreference.getInstance().getAppMode() == BitherjSettings.AppMode.COLD) {
        BackupUtil.backupColdKey(false);
    } else {
        BackupUtil.backupHotKey();
    }

    return addressList;

}
 
Example 2
Source File: KeyUtil.java    From bither-desktop-java with Apache License 2.0 6 votes vote down vote up
public static List<Address> addPrivateKeyByRandomWithPassphras(IUEntropy iuEntropy, CharSequence password, int count) {
    PeerUtil.stopPeer();
    List<Address> addressList = new ArrayList<Address>();
    for (int i = 0; i < count; i++) {
        XRandom xRandom = new XRandom(iuEntropy);
        ECKey ecKey = ECKey.generateECKey(xRandom);
        ecKey = PrivateKeyUtil.encrypt(ecKey, password);
        Address address = new Address(ecKey.toAddress(),
                ecKey.getPubKey(), PrivateKeyUtil.getEncryptedString(ecKey), ecKey.isFromXRandom());
        ecKey.clearPrivateKey();
        addressList.add(address);
        AddressManager.getInstance().addAddress(address);

    }
    PeerUtil.startPeer();
    if (UserPreference.getInstance().getAppMode() == BitherjSettings.AppMode.COLD) {
        BackupUtil.backupColdKey(false);
    } else {
        BackupUtil.backupHotKey();
    }

    return addressList;

}
 
Example 3
Source File: KeyUtil.java    From bither-android with Apache License 2.0 5 votes vote down vote up
public static List<Address> addPrivateKeyByRandomWithPassphras(BlockchainService service, IUEntropy iuEntropy, CharSequence password, int count) {
    if (service != null) {
        service.stopAndUnregister();
    }
    List<Address> addressList = new ArrayList<Address>();
    for (int i = 0; i < count; i++) {
        XRandom xRandom = new XRandom(iuEntropy);
        ECKey ecKey = ECKey.generateECKey(xRandom);
        ecKey = PrivateKeyUtil.encrypt(ecKey, password);
        Address address = new Address(ecKey.toAddress(),
                ecKey.getPubKey(), PrivateKeyUtil.getEncryptedString(ecKey), true, ecKey.isFromXRandom());
        ecKey.clearPrivateKey();
        addressList.add(address);
        AddressManager.getInstance().addAddress(address);

    }
    if (AppSharedPreference.getInstance().getAppMode() == BitherjSettings.AppMode.COLD) {
        BackupUtil.backupColdKey(false);
    } else {
        BackupUtil.backupHotKey();
    }
    if (service != null) {
        service.startAndRegister();
    }
    return addressList;

}
 
Example 4
Source File: ImportPrivateKey.java    From bitherj with Apache License 2.0 5 votes vote down vote up
private Address addECKey(ECKey ecKey) {
    String encryptedPrivateString;
    if (importPrivateKeyType == ImportPrivateKeyType.BitherQrcode) {
        encryptedPrivateString = QRCodeUtil.getNewVersionEncryptPrivKey(content);
    } else {
        ecKey = PrivateKeyUtil.encrypt(ecKey, password);
        encryptedPrivateString = PrivateKeyUtil.getEncryptedString(ecKey);
    }
    Address address = new Address(ecKey.toAddress(), ecKey.getPubKey(), encryptedPrivateString
            , false, ecKey.isFromXRandom());
    if (AddressManager.getInstance().getWatchOnlyAddresses().contains(address)) {
        password.wipe();
        importError(CAN_NOT_IMPORT_BITHER_COLD_PRIVATE_KEY);
        return null;
    } else if (AddressManager.getInstance().getPrivKeyAddresses().contains(address)) {
        password.wipe();
        importError(PRIVATE_KEY_ALREADY_EXISTS);
        return null;

    } else {
        if (importPrivateKeyType == ImportPrivateKeyType.BitherQrcode) {
            PasswordSeed passwordSeed = PasswordSeed.getPasswordSeed();
            if (passwordSeed != null && !passwordSeed.checkPassword(password)) {
                password.wipe();
                importError(PASSWORD_IS_DIFFEREND_LOCAL);
                return null;
            }
        } else {
            password.wipe();
        }
        return address;


    }

}
 
Example 5
Source File: PrivateKeyUEntropyDialog.java    From bither-desktop-java with Apache License 2.0 4 votes vote down vote up
@Override
public void run() {
    startGeneratingTime = System.currentTimeMillis();
    onProgress(startProgress);
    SecureCharSequence password = passwordGetter.getPassword();
    boolean success = false;
    final ArrayList<String> addressStrs = new ArrayList<String>();
    double progress = startProgress;
    double itemProgress = (1.0 - startProgress - saveProgress) / (double) targetCount;

    try {
        entropyCollector.start();
        PeerUtil.stopPeer();

        java.util.List<Address> addressList = new ArrayList<Address>();
        for (int i = 0;
             i < targetCount;
             i++) {
            if (cancelRunnable != null) {
                finishGenerate();
                SwingUtilities.invokeLater(cancelRunnable);
                return;
            }

            XRandom xRandom = new XRandom(entropyCollector);
            ECKey ecKey = ECKey.generateECKey(xRandom);
            ecKey.setFromXRandom(true);
            progress += itemProgress * progressKeyRate;
            onProgress(progress);
            if (cancelRunnable != null) {
                finishGenerate();
                SwingUtilities.invokeLater(cancelRunnable);

                return;
            }
            // start encrypt
            ecKey = PrivateKeyUtil.encrypt(ecKey, password);
            Address address = new Address(ecKey.toAddress(), ecKey.getPubKey(),
                    PrivateKeyUtil.getEncryptedString(ecKey), ecKey.isFromXRandom());
            ecKey.clearPrivateKey();
            addressList.add(address);
            addressStrs.add(address.getAddress());

            progress += itemProgress * progressEntryptRate;
            onProgress(progress);
        }
        entropyCollector.stop();
        passwordGetter.wipe();

        if (cancelRunnable != null) {
            finishGenerate();
            SwingUtilities.invokeLater(cancelRunnable);
            return;
        }
        KeyUtil.addAddressListByDesc(addressList);
        success = true;
    } catch (Exception e) {
        e.printStackTrace();
    }

    finishGenerate();
    if (success) {
        while (System.currentTimeMillis() - startGeneratingTime < MinGeneratingTime) {

        }
        onProgress(1);
        didSuccess(addressStrs);
    } else {
        onFailed();
    }
}
 
Example 6
Source File: PrivateKeyUEntropyActivity.java    From bither-android with Apache License 2.0 4 votes vote down vote up
@Override
public void runWithService(BlockchainService service) {
    boolean success = false;
    final ArrayList<String> addressStrs = new ArrayList<String>();
    double progress = startProgress;
    double itemProgress = (1.0 - startProgress - saveProgress) / (double) targetCount;

    try {
        entropyCollector.start();
        if (service != null) {
            service.stopAndUnregister();
        }

        XRandom xRandom = new XRandom(entropyCollector);

        List<Address> addressList = new ArrayList<Address>();
        for (int i = 0;
             i < targetCount;
             i++) {
            if (cancelRunnable != null) {
                finishGenerate(service);
                runOnUiThread(cancelRunnable);
                return;
            }

            ECKey ecKey = ECKey.generateECKey(xRandom);
            ecKey.setFromXRandom(true);

            progress += itemProgress * progressKeyRate;
            onProgress(progress);
            if (cancelRunnable != null) {
                finishGenerate(service);
                runOnUiThread(cancelRunnable);
                return;
            }


            // start encrypt
            ecKey = PrivateKeyUtil.encrypt(ecKey, password);
            Address address = new Address(ecKey.toAddress(), ecKey.getPubKey(),
                    PrivateKeyUtil.getEncryptedString(ecKey), true, ecKey.isFromXRandom());
            ecKey.clearPrivateKey();
            addressList.add(address);
            addressStrs.add(address.getAddress());

            progress += itemProgress * progressEntryptRate;
            onProgress(progress);
        }

        entropyCollector.stop();
        password.wipe();
        password = null;

        if (cancelRunnable != null) {
            finishGenerate(service);
            runOnUiThread(cancelRunnable);
            return;
        }

        KeyUtil.addAddressListByDesc(service, addressList);
        success = true;
    } catch (Exception e) {
        e.printStackTrace();
    }

    finishGenerate(service);
    if (success) {
        while (System.currentTimeMillis() - startGeneratingTime < MinGeneratingTime) {

        }
        onProgress(1);
        onSuccess(addressStrs);
    } else {
        onFailed();
    }
}
 
Example 7
Source File: PrivateKeyUEntropyDialog.java    From bither-desktop-java with Apache License 2.0 4 votes vote down vote up
@Override
public void run() {
    startGeneratingTime = System.currentTimeMillis();
    onProgress(startProgress);
    SecureCharSequence password = passwordGetter.getPassword();
    boolean success = false;
    final ArrayList<String> addressStrs = new ArrayList<String>();
    double progress = startProgress;
    double itemProgress = (1.0 - startProgress - saveProgress) / (double) targetCount;

    try {
        entropyCollector.start();
        PeerUtil.stopPeer();

        java.util.List<Address> addressList = new ArrayList<Address>();
        for (int i = 0;
             i < targetCount;
             i++) {
            if (cancelRunnable != null) {
                finishGenerate();
                SwingUtilities.invokeLater(cancelRunnable);
                return;
            }

            XRandom xRandom = new XRandom(entropyCollector);
            ECKey ecKey = ECKey.generateECKey(xRandom);
            ecKey.setFromXRandom(true);
            progress += itemProgress * progressKeyRate;
            onProgress(progress);
            if (cancelRunnable != null) {
                finishGenerate();
                SwingUtilities.invokeLater(cancelRunnable);

                return;
            }
            // start encrypt
            ecKey = PrivateKeyUtil.encrypt(ecKey, password);
            Address address = new Address(ecKey.toAddress(), ecKey.getPubKey(),
                    PrivateKeyUtil.getEncryptedString(ecKey), ecKey.isFromXRandom());
            ecKey.clearPrivateKey();
            addressList.add(address);
            addressStrs.add(address.getAddress());

            progress += itemProgress * progressEntryptRate;
            onProgress(progress);
        }
        entropyCollector.stop();
        passwordGetter.wipe();

        if (cancelRunnable != null) {
            finishGenerate();
            SwingUtilities.invokeLater(cancelRunnable);
            return;
        }
        KeyUtil.addAddressListByDesc(addressList);
        success = true;
    } catch (Exception e) {
        e.printStackTrace();
    }

    finishGenerate();
    if (success) {
        while (System.currentTimeMillis() - startGeneratingTime < MinGeneratingTime) {

        }
        onProgress(1);
        didSuccess(addressStrs);
    } else {
        onFailed();
    }
}