Search in sources :

Example 1 with EncryptedData

use of io.nuls.core.tools.crypto.EncryptedData in project nuls by nuls-io.

the class Account method decrypt.

/**
 * 根据解密账户, 包括生成账户明文私钥
 * According to the decryption account, including generating the account plaintext private key
 */
private boolean decrypt(String password) throws NulsException {
    try {
        byte[] unencryptedPrivateKey = AESEncrypt.decrypt(this.getEncryptedPriKey(), password);
        BigInteger newPriv = new BigInteger(1, unencryptedPrivateKey);
        ECKey key = ECKey.fromPrivate(newPriv);
        if (!Arrays.equals(key.getPubKey(), getPubKey())) {
            return false;
        }
        key.setEncryptedPrivateKey(new EncryptedData(this.getEncryptedPriKey()));
        this.setPriKey(key.getPrivKeyBytes());
        this.setEcKey(key);
    } catch (Exception e) {
        throw new NulsException(AccountErrorCode.PASSWORD_IS_WRONG);
    }
    return true;
}
Also used : NulsException(io.nuls.kernel.exception.NulsException) BigInteger(java.math.BigInteger) ECKey(io.nuls.core.tools.crypto.ECKey) EncryptedData(io.nuls.core.tools.crypto.EncryptedData) CryptoException(io.nuls.core.tools.crypto.Exception.CryptoException) NulsException(io.nuls.kernel.exception.NulsException)

Example 2 with EncryptedData

use of io.nuls.core.tools.crypto.EncryptedData in project nuls by nuls-io.

the class AccountPo method toAccount.

public Account toAccount() {
    Account account = new Account();
    account.setCreateTime(this.getCreateTime());
    try {
        account.setAddress(Address.fromHashs(this.getAddress()));
    } catch (Exception e) {
        Log.error(e);
    }
    account.setAlias(this.getAlias());
    account.setExtend(this.getExtend());
    account.setPriKey(this.getPriKey());
    account.setPubKey(this.getPubKey());
    account.setEncryptedPriKey(this.getEncryptedPriKey());
    if (this.getPriKey() != null && this.getPriKey().length > 1) {
        account.setEcKey(ECKey.fromPrivate(new BigInteger(1, account.getPriKey())));
    } else {
        account.setEcKey(ECKey.fromEncrypted(new EncryptedData(this.getEncryptedPriKey()), this.getPubKey()));
    }
    account.setStatus(this.getStatus());
    account.setRemark(this.remark);
    account.setOk(this.ok);
    return account;
}
Also used : Account(io.nuls.account.model.Account) BigInteger(java.math.BigInteger) EncryptedData(io.nuls.core.tools.crypto.EncryptedData)

Example 3 with EncryptedData

use of io.nuls.core.tools.crypto.EncryptedData in project nuls by nuls-io.

the class AccountServiceImpl method exportAccountToKeyStore.

@Override
public Result<AccountKeyStore> exportAccountToKeyStore(String address, String password) {
    if (!AddressTool.validAddress(address)) {
        return Result.getFailed(AccountErrorCode.PARAMETER_ERROR);
    }
    Account account = getAccountByAddress(address);
    if (null == account) {
        return Result.getFailed(AccountErrorCode.ACCOUNT_NOT_EXIST);
    }
    AccountKeyStore accountKeyStore = new AccountKeyStore();
    // 只要加过密(且没解锁),就验证密码
    if (account.isEncrypted() && account.isLocked()) {
        if (!account.validatePassword(password)) {
            return Result.getFailed(AccountErrorCode.PASSWORD_IS_WRONG);
        }
    }
    // 只要加过密(不管是否解锁)都不导出明文私钥, If the account is encrypted (regardless of unlocked), the plaintext private key is not exported
    if (account.isEncrypted()) {
        EncryptedData encryptedData = new EncryptedData(account.getEncryptedPriKey());
        accountKeyStore.setEncryptedPrivateKey(Hex.encode(encryptedData.getEncryptedBytes()));
    } else {
        accountKeyStore.setPrikey(account.getPriKey());
    }
    accountKeyStore.setAddress(account.getAddress().toString());
    accountKeyStore.setAlias(account.getAlias());
    accountKeyStore.setPubKey(account.getPubKey());
    return Result.getSuccess().setData(accountKeyStore);
}
Also used : EncryptedData(io.nuls.core.tools.crypto.EncryptedData)

Example 4 with EncryptedData

use of io.nuls.core.tools.crypto.EncryptedData in project nuls by nuls-io.

the class Account method encrypt.

/**
 * 根据密码加密账户(给账户设置密码)
 * Password-encrypted account (set password for account)
 */
public void encrypt(String password, boolean isForce) throws NulsException {
    if (this.isEncrypted()) {
        if (isForce) {
            if (isLocked()) {
                throw new NulsException(AccountErrorCode.ACCOUNT_IS_ALREADY_ENCRYPTED_AND_LOCKED);
            }
        } else {
            throw new NulsException(AccountErrorCode.ACCOUNT_IS_ALREADY_ENCRYPTED);
        }
    }
    ECKey eckey = this.getEcKey();
    byte[] privKeyBytes = eckey.getPrivKeyBytes();
    EncryptedData encryptedPrivateKey = AESEncrypt.encrypt(privKeyBytes, EncryptedData.DEFAULT_IV, new KeyParameter(Sha256Hash.hash(password.getBytes())));
    eckey.setEncryptedPrivateKey(encryptedPrivateKey);
    ECKey result = ECKey.fromEncrypted(encryptedPrivateKey, getPubKey());
    this.setPriKey(new byte[0]);
    this.setEcKey(result);
    this.setEncryptedPriKey(encryptedPrivateKey.getEncryptedBytes());
}
Also used : NulsException(io.nuls.kernel.exception.NulsException) KeyParameter(org.spongycastle.crypto.params.KeyParameter) ECKey(io.nuls.core.tools.crypto.ECKey) EncryptedData(io.nuls.core.tools.crypto.EncryptedData)

Aggregations

EncryptedData (io.nuls.core.tools.crypto.EncryptedData)4 ECKey (io.nuls.core.tools.crypto.ECKey)2 NulsException (io.nuls.kernel.exception.NulsException)2 BigInteger (java.math.BigInteger)2 Account (io.nuls.account.model.Account)1 CryptoException (io.nuls.core.tools.crypto.Exception.CryptoException)1 KeyParameter (org.spongycastle.crypto.params.KeyParameter)1