Java Code Examples for net.bither.bitherj.qrcode.QRCodeUtil#QR_CODE_SPLIT

The following examples show how to use net.bither.bitherj.qrcode.QRCodeUtil#QR_CODE_SPLIT . 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: PrivateKeyUtil.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public static String getEncryptedString(ECKey ecKey) {
    String salt = "1";
    if (ecKey.getKeyCrypter() instanceof KeyCrypterScrypt) {
        KeyCrypterScrypt scrypt = (KeyCrypterScrypt) ecKey.getKeyCrypter();
        salt = Utils.bytesToHexString(scrypt.getSalt());
    }
    EncryptedPrivateKey key = ecKey.getEncryptedPrivateKey();
    return Utils.bytesToHexString(key.getEncryptedBytes()) + QRCodeUtil.QR_CODE_SPLIT + Utils
            .bytesToHexString(key.getInitialisationVector()) + QRCodeUtil.QR_CODE_SPLIT + salt;
}
 
Example 3
Source File: PrivateKeyUtil.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public static String changePassword(String str, CharSequence oldpassword, CharSequence newPassword) {
    String[] strs = QRCodeUtil.splitOfPasswordSeed(str);
    if (strs.length != 3) {
        log.error("change Password: PrivateKeyFromString format error");
        return null;
    }

    byte[] temp = Utils.hexStringToByteArray(strs[2]);
    if (temp.length != KeyCrypterScrypt.SALT_LENGTH + 1 && temp.length != KeyCrypterScrypt.SALT_LENGTH) {
        log.error("decryption:  salt lenth is {} not {}", temp.length, KeyCrypterScrypt.SALT_LENGTH + 1);
        return null;
    }
    byte[] salt = new byte[KeyCrypterScrypt.SALT_LENGTH];
    if (temp.length == KeyCrypterScrypt.SALT_LENGTH) {
        salt = temp;
    } else {
        System.arraycopy(temp, 1, salt, 0, salt.length);
    }
    KeyCrypterScrypt crypter = new KeyCrypterScrypt(salt);
    EncryptedPrivateKey epk = new EncryptedPrivateKey(Utils.hexStringToByteArray
            (strs[1]), Utils.hexStringToByteArray(strs[0]));

    byte[] decrypted = crypter.decrypt(epk, crypter.deriveKey(oldpassword));
    EncryptedPrivateKey encryptedPrivateKey = crypter.encrypt(decrypted, crypter.deriveKey(newPassword));
    byte[] newDecrypted = crypter.decrypt(encryptedPrivateKey, crypter.deriveKey(newPassword));
    if (!Arrays.equals(decrypted, newDecrypted)) {
        throw new KeyCrypterException("change Password, cannot be successfully decrypted after encryption so aborting wallet encryption.");
    }
    Utils.wipeBytes(decrypted);
    Utils.wipeBytes(newDecrypted);
    return Utils.bytesToHexString(encryptedPrivateKey.getEncryptedBytes())
            + QRCodeUtil.QR_CODE_SPLIT + Utils.bytesToHexString(encryptedPrivateKey.getInitialisationVector())
            + QRCodeUtil.QR_CODE_SPLIT + strs[2];

}
 
Example 4
Source File: PrivateKeyUtil.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public static String getEncryptPrivateKeyStringFromAllAddresses() {
    String content = "";
    List<Address> privates = AddressManager.getInstance().getPrivKeyAddresses();
    for (int i = 0;
         i < privates.size();
         i++) {
        Address address = privates.get(i);
        content += address.getFullEncryptPrivKey();
        if (i < privates.size() - 1) {
            content += QRCodeUtil.QR_CODE_SPLIT;
        }
    }
    HDMKeychain keychain = AddressManager.getInstance().getHdmKeychain();
    if (keychain != null) {
        if (Utils.isEmpty(content)) {
            content += keychain.getQRCodeFullEncryptPrivKey();
        } else {
            content += QRCodeUtil.QR_CODE_SPLIT + keychain.getQRCodeFullEncryptPrivKey();
        }
    }
    HDAccount hdAccount = AddressManager.getInstance().getHDAccountHot();
    if (hdAccount != null) {
        if (Utils.isEmpty(content)) {
            content += hdAccount.getQRCodeFullEncryptPrivKey();
        } else {
            content += QRCodeUtil.QR_CODE_SPLIT + hdAccount.getQRCodeFullEncryptPrivKey();
        }
    }
    HDAccountCold hdAccountCold = AddressManager.getInstance().getHDAccountCold();
    if (hdAccountCold != null) {
        if (Utils.isEmpty(content)) {
            content += hdAccountCold.getQRCodeFullEncryptPrivKey();
        } else {
            content += QRCodeUtil.QR_CODE_SPLIT + hdAccountCold.getQRCodeFullEncryptPrivKey();
        }
    }
    return content;
}
 
Example 5
Source File: PrivateKeyUtil.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public static List<Address> getECKeysFromBackupString(String str, CharSequence password) {
    String[] strs = QRCodeUtil.splitOfPasswordSeed(str);
    if (strs.length % 3 != 0) {
        log.error("Backup: PrivateKeyFromString format error");
        return null;
    }
    ArrayList<Address> list = new ArrayList<Address>();
    for (int i = 0;
         i < strs.length;
         i += 3) {
        if (strs[i].indexOf(QRCodeUtil.HDM_QR_CODE_FLAG) == 0) {
            continue;
        }
        if (strs[i].indexOf(QRCodeUtil.HD_QR_CODE_FLAG) == 0){
            continue;
        }
        String encryptedString = strs[i] + QRCodeUtil.QR_CODE_SPLIT + strs[i + 1]
                + QRCodeUtil.QR_CODE_SPLIT + strs[i + 2];
        ECKey key = getECKeyFromSingleString(encryptedString, password);

        if (key == null) {
            return null;
        } else {
            Address address = new Address(key.toAddress(), key.getPubKey(), encryptedString,
                    false, key.isFromXRandom());
            key.clearPrivateKey();
            list.add(address);
        }
    }
    return list;
}
 
Example 6
Source File: PasswordSeed.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public String toPasswordSeedString() {
    try {
        String passwordSeedString = Base58.bas58ToHexWithAddress(this.address) + QRCodeUtil.QR_CODE_SPLIT
                + QRCodeUtil.getNewVersionEncryptPrivKey(this.keyStr);
        return passwordSeedString;
    } catch (AddressFormatException e) {
        throw new RuntimeException("passwordSeed  address is format error ," + this.address);

    }

}
 
Example 7
Source File: SignTxDialg.java    From bither-desktop-java with Apache License 2.0 4 votes vote down vote up
@Override
public void onPasswordEntered(final SecureCharSequence password) {
    dp = new DialogProgress();
    Thread thread = new Thread() {
        public void run() {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    dp.pack();
                    dp.setVisible(true);
                }
            });
            List<String> strings = new ArrayList<String>();
            if (qrCodeTransport.getHdmIndex() >= 0) {
                if (!AddressManager.getInstance().hasHDMKeychain()) {
                    SwingUtilities.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            dp.dispose();
                            new MessageDialog(LocaliserUtils.getString("hdm_send_with_cold_no_requested_seed"));

                        }
                    });
                    password.wipe();
                    SwingUtilities.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            dp.dispose();
                        }
                    });
                    return;
                }
                try {
                    DeterministicKey key = AddressManager.getInstance().getHdmKeychain()
                            .getExternalKey(qrCodeTransport.getHdmIndex(), password);

                    List<String> hashes = qrCodeTransport.getHashList();
                    strings = new ArrayList<String>();
                    for (String hash : hashes) {
                        ECKey.ECDSASignature signed = key.sign(Utils.hexStringToByteArray
                                (hash));
                        strings.add(Utils.bytesToHexString(signed.encodeToDER()));
                    }
                    key.wipe();
                } catch (Exception e) {
                    e.printStackTrace();

                    SwingUtilities.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            dp.dispose();
                            new MessageDialog(LocaliserUtils.getString("hdm_send_with_cold_no_requested_seed")).showMsg();
                        }
                    });
                    password.wipe();
                    return;
                }
            } else {
                Address address = WalletUtils.findPrivateKey(qrCodeTransport.getMyAddress());
                strings = address.signStrHashes(qrCodeTransport.getHashList(), password);
            }
            password.wipe();
            String result = "";
            for (int i = 0;
                 i < strings.size();
                 i++) {
                if (i < strings.size() - 1) {
                    result = result + strings.get(i) + QRCodeUtil.QR_CODE_SPLIT;
                } else {
                    result = result + strings.get(i);
                }
            }
            final String r = result;
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    dp.dispose();
                    dispose();
                    DisplayBitherQRCodePanel displayBitherQRCodePanle = new DisplayBitherQRCodePanel(r);
                    displayBitherQRCodePanle.showPanel();

                }
            });

        }

        ;
    };
    thread.start();

}
 
Example 8
Source File: EncryptedData.java    From bitherj with Apache License 2.0 4 votes vote down vote up
public String toEncryptedString() {
    return Utils.bytesToHexString(encryptedData).toUpperCase()
            + QRCodeUtil.QR_CODE_SPLIT + Utils.bytesToHexString(initialisationVector).toUpperCase()
            + QRCodeUtil.QR_CODE_SPLIT + Utils.bytesToHexString(saltForQRCode.getSalt()).toUpperCase();
}
 
Example 9
Source File: EncryptedData.java    From bitherj with Apache License 2.0 4 votes vote down vote up
public String toEncryptedStringForQRCode() {
    return Utils.bytesToHexString(encryptedData).toUpperCase()
            + QRCodeUtil.QR_CODE_SPLIT + Utils.bytesToHexString(initialisationVector).toUpperCase()
            + QRCodeUtil.QR_CODE_SPLIT + Utils.bytesToHexString(saltForQRCode.getQrCodeSalt()).toUpperCase();
}
 
Example 10
Source File: EncryptedData.java    From bitherj with Apache License 2.0 4 votes vote down vote up
public String toEncryptedStringForQRCode(boolean isCompress, boolean isFromXRandom) {
    SaltForQRCode newSaltForQRCode = new SaltForQRCode(saltForQRCode.getSalt(), isCompress, isFromXRandom);
    return Utils.bytesToHexString(encryptedData).toUpperCase()
            + QRCodeUtil.QR_CODE_SPLIT + Utils.bytesToHexString(initialisationVector).toUpperCase()
            + QRCodeUtil.QR_CODE_SPLIT + Utils.bytesToHexString(newSaltForQRCode.getQrCodeSalt()).toUpperCase();
}
 
Example 11
Source File: SignTxDialg.java    From bither-desktop-java with Apache License 2.0 4 votes vote down vote up
@Override
public void onPasswordEntered(final SecureCharSequence password) {
    dp = new DialogProgress();
    Thread thread = new Thread() {
        public void run() {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    dp.pack();
                    dp.setVisible(true);
                }
            });
            List<String> strings = new ArrayList<String>();
            if (qrCodeTransport.getHdmIndex() >= 0) {
                if (!AddressManager.getInstance().hasHDMKeychain()) {
                    SwingUtilities.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            dp.dispose();
                            new MessageDialog(LocaliserUtils.getString("hdm_send_with_cold_no_requested_seed"));

                        }
                    });
                    password.wipe();
                    SwingUtilities.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            dp.dispose();
                        }
                    });
                    return;
                }
                try {
                    DeterministicKey key = AddressManager.getInstance().getHdmKeychain()
                            .getExternalKey(qrCodeTransport.getHdmIndex(), password);

                    List<String> hashes = qrCodeTransport.getHashList();
                    strings = new ArrayList<String>();
                    for (String hash : hashes) {
                        ECKey.ECDSASignature signed = key.sign(Utils.hexStringToByteArray
                                (hash));
                        strings.add(Utils.bytesToHexString(signed.encodeToDER()));
                    }
                    key.wipe();
                } catch (Exception e) {
                    e.printStackTrace();

                    SwingUtilities.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            dp.dispose();
                            new MessageDialog(LocaliserUtils.getString("hdm_send_with_cold_no_requested_seed")).showMsg();
                        }
                    });
                    password.wipe();
                    return;
                }
            } else {
                Address address = WalletUtils.findPrivateKey(qrCodeTransport.getMyAddress());
                strings = address.signStrHashes(qrCodeTransport.getHashList(), password);
            }
            password.wipe();
            String result = "";
            for (int i = 0;
                 i < strings.size();
                 i++) {
                if (i < strings.size() - 1) {
                    result = result + strings.get(i) + QRCodeUtil.QR_CODE_SPLIT;
                } else {
                    result = result + strings.get(i);
                }
            }
            final String r = result;
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    dp.dispose();
                    dispose();
                    DisplayBitherQRCodePanel displayBitherQRCodePanle = new DisplayBitherQRCodePanel(r);
                    displayBitherQRCodePanle.showPanel();

                }
            });

        }

        ;
    };
    thread.start();

}