Search in sources :

Example 1 with DecryptionException

use of utils.security.DecryptionException in project jdchain-core by blockchain-jd-com.

the class KeyGenCommand method readKey.

/**
 * 读取密钥; <br>
 * 如果是私钥,则需要输入密码;
 *
 * @param keyFile
 */
public static void readKey(String keyFile, boolean decrypting) {
    String base58KeyString = FileUtils.readText(keyFile);
    byte[] keyBytes = Base58Utils.decode(base58KeyString);
    if (KeyGenUtils.isPubKeyBytes(keyBytes)) {
        if (decrypting) {
            // Try reading pubKey;
            PubKey pubKey = decodePubKey(keyBytes);
            ConsoleUtils.info("======================== pub key ========================\r\n" + "[%s]\r\n" + "Raw:[%s][%s]\r\n", base58KeyString, pubKey.getAlgorithm(), Base58Utils.encode(pubKey.toBytes()));
        } else {
            ConsoleUtils.info("======================== pub key ========================\r\n" + "[%s]\r\n", base58KeyString);
        }
        return;
    } else if (KeyGenUtils.isPrivKeyBytes(keyBytes)) {
        // Try reading privKye;
        try {
            if (decrypting) {
                byte[] pwdBytes = readPassword();
                PrivKey privKey = decryptedPrivKeyBytes(keyBytes, pwdBytes);
                ConsoleUtils.info("======================== priv key ========================\r\n" + "[%s]\r\n" + "Raw:[%s][%s]\r\n", base58KeyString, privKey.getAlgorithm(), Base58Utils.encode(privKey.toBytes()));
            } else {
                ConsoleUtils.info("======================== priv key ========================\r\n[%s]\r\n", base58KeyString);
            }
        } catch (DecryptionException e) {
            ConsoleUtils.error("Invalid password!");
        }
        return;
    } else {
        ConsoleUtils.error("Unknow key!!");
        return;
    }
}
Also used : PubKey(com.jd.blockchain.crypto.PubKey) KeyGenUtils.decodePubKey(com.jd.blockchain.crypto.KeyGenUtils.decodePubKey) KeyGenUtils.encodePubKey(com.jd.blockchain.crypto.KeyGenUtils.encodePubKey) KeyGenUtils.encodePrivKey(com.jd.blockchain.crypto.KeyGenUtils.encodePrivKey) PrivKey(com.jd.blockchain.crypto.PrivKey) DecryptionException(utils.security.DecryptionException)

Aggregations

KeyGenUtils.decodePubKey (com.jd.blockchain.crypto.KeyGenUtils.decodePubKey)1 KeyGenUtils.encodePrivKey (com.jd.blockchain.crypto.KeyGenUtils.encodePrivKey)1 KeyGenUtils.encodePubKey (com.jd.blockchain.crypto.KeyGenUtils.encodePubKey)1 PrivKey (com.jd.blockchain.crypto.PrivKey)1 PubKey (com.jd.blockchain.crypto.PubKey)1 DecryptionException (utils.security.DecryptionException)1