Search in sources :

Example 1 with CryptoException

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

the class AccountServiceImpl method importAccountFormKeyStore.

@Override
public Result<Account> importAccountFormKeyStore(AccountKeyStore keyStore, String password) {
    if (null == keyStore || StringUtils.isBlank(keyStore.getAddress())) {
        return Result.getFailed(AccountErrorCode.PARAMETER_ERROR);
    }
    if (!AddressTool.validAddress(keyStore.getAddress())) {
        return Result.getFailed(AccountErrorCode.ADDRESS_ERROR);
    }
    Account account;
    byte[] priKey = null;
    if (null != keyStore.getPrikey() && keyStore.getPrikey().length > 0) {
        if (!ECKey.isValidPrivteHex(Hex.encode(keyStore.getPrikey()))) {
            return Result.getFailed(AccountErrorCode.PARAMETER_ERROR);
        }
        priKey = keyStore.getPrikey();
        try {
            account = AccountTool.createAccount(Hex.encode(priKey));
        } catch (NulsException e) {
            return Result.getFailed(e.getErrorCode());
        }
        // 如果私钥生成的地址和keystore的地址不相符,说明私钥错误
        if (!account.getAddress().getBase58().equals(keyStore.getAddress())) {
            return Result.getFailed(AccountErrorCode.PRIVATE_KEY_WRONG);
        }
    } else if (null == keyStore.getPrikey() && null != keyStore.getEncryptedPrivateKey()) {
        if (!StringUtils.validPassword(password)) {
            return Result.getFailed(AccountErrorCode.PASSWORD_IS_WRONG);
        }
        try {
            priKey = AESEncrypt.decrypt(Hex.decode(keyStore.getEncryptedPrivateKey()), password);
            account = AccountTool.createAccount(Hex.encode(priKey));
        } catch (CryptoException e) {
            return Result.getFailed(AccountErrorCode.PASSWORD_IS_WRONG);
        } catch (NulsException e) {
            return Result.getFailed(e.getErrorCode());
        }
        // 如果私钥生成的地址和keystore的地址不相符,说明密码错误
        if (!account.getAddress().getBase58().equals(keyStore.getAddress())) {
            return Result.getFailed(AccountErrorCode.PASSWORD_IS_WRONG);
        }
    } else {
        return Result.getFailed(AccountErrorCode.PARAMETER_ERROR);
    }
    Alias aliasDb = null;
    if (StringUtils.isNotBlank(keyStore.getAlias())) {
        aliasDb = aliasService.getAlias(keyStore.getAlias());
    }
    if (null != aliasDb && AddressTool.getStringAddressByBytes(aliasDb.getAddress()).equals(account.getAddress().toString())) {
        account.setAlias(aliasDb.getAlias());
    } else {
        List<AliasPo> list = aliasStorageService.getAliasList().getData();
        for (AliasPo aliasPo : list) {
            // 如果全网别名中的地址有和当前导入的账户地址相同,则赋值别名到账户中
            if (AddressTool.getStringAddressByBytes(aliasPo.getAddress()).equals(account.getAddress().toString())) {
                account.setAlias(aliasPo.getAlias());
                break;
            }
        }
    }
    if (StringUtils.validPassword(password)) {
        try {
            account.encrypt(password);
        } catch (NulsException e) {
            Log.error(e);
            return Result.getFailed(e.getErrorCode());
        }
    }
    account.setOk(false);
    AccountPo po = new AccountPo(account);
    Result result = accountStorageService.saveAccount(po);
    if (result.isFailed()) {
        return result;
    }
    accountCacheService.localAccountMaps.put(account.getAddress().getBase58(), account);
    TaskManager.asynExecuteRunnable(new Runnable() {

        @Override
        public void run() {
            String address = account.getAddress().getBase58();
            Result res = accountLedgerService.importLedger(address);
            if (res.isFailed()) {
                AccountServiceImpl.this.removeAccount(address, password);
            } else {
                AccountServiceImpl.this.finishImport(account);
            }
        }
    });
    return Result.getSuccess().setData(account);
}
Also used : NulsException(io.nuls.kernel.exception.NulsException) AccountPo(io.nuls.account.storage.po.AccountPo) AliasPo(io.nuls.account.storage.po.AliasPo) CryptoException(io.nuls.core.tools.crypto.Exception.CryptoException) Result(io.nuls.kernel.model.Result)

Example 2 with CryptoException

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

the class Account method validatePassword.

/**
 * 验证账户密码是否正确
 * Verify that the account password is correct
 */
public boolean validatePassword(String password) {
    boolean result = StringUtils.validPassword(password);
    if (!result) {
        return result;
    }
    byte[] unencryptedPrivateKey;
    try {
        unencryptedPrivateKey = AESEncrypt.decrypt(this.getEncryptedPriKey(), password);
    } catch (CryptoException e) {
        return false;
    }
    BigInteger newPriv = new BigInteger(1, unencryptedPrivateKey);
    ECKey key = ECKey.fromPrivate(newPriv);
    if (!Arrays.equals(key.getPubKey(), getPubKey())) {
        return false;
    }
    return true;
}
Also used : BigInteger(java.math.BigInteger) ECKey(io.nuls.core.tools.crypto.ECKey) CryptoException(io.nuls.core.tools.crypto.Exception.CryptoException)

Example 3 with CryptoException

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

the class Account method getPriKey.

public byte[] getPriKey(String password) throws NulsException {
    if (!StringUtils.validPassword(password)) {
        throw new NulsException(AccountErrorCode.PASSWORD_IS_WRONG);
    }
    byte[] unencryptedPrivateKey;
    try {
        unencryptedPrivateKey = AESEncrypt.decrypt(this.getEncryptedPriKey(), password);
    } catch (CryptoException e) {
        throw new NulsException(AccountErrorCode.PASSWORD_IS_WRONG);
    }
    BigInteger newPriv = new BigInteger(1, unencryptedPrivateKey);
    ECKey key = ECKey.fromPrivate(newPriv);
    if (!Arrays.equals(key.getPubKey(), getPubKey())) {
        throw new NulsException(AccountErrorCode.PASSWORD_IS_WRONG);
    }
    return unencryptedPrivateKey;
}
Also used : NulsException(io.nuls.kernel.exception.NulsException) BigInteger(java.math.BigInteger) ECKey(io.nuls.core.tools.crypto.ECKey) CryptoException(io.nuls.core.tools.crypto.Exception.CryptoException)

Example 4 with CryptoException

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

the class Account method getEcKey.

/**
 * 根据密码获取ECKey
 */
public ECKey getEcKey(String password) throws NulsException {
    ECKey eckey = null;
    byte[] unencryptedPrivateKey;
    // 判断当前账户是否存在私钥,如果不存在私钥这为锁定账户
    BigInteger newPriv = null;
    if (this.isLocked()) {
        AssertUtil.canNotEmpty(password, "the password can not be empty");
        if (!validatePassword(password)) {
            throw new NulsException(AccountErrorCode.PASSWORD_IS_WRONG);
        }
        try {
            unencryptedPrivateKey = AESEncrypt.decrypt(this.getEncryptedPriKey(), password);
            newPriv = new BigInteger(1, unencryptedPrivateKey);
        } catch (CryptoException e) {
            throw new NulsException(AccountErrorCode.PASSWORD_IS_WRONG);
        }
    } else {
        newPriv = new BigInteger(1, this.getPriKey());
    }
    eckey = ECKey.fromPrivate(newPriv);
    if (!Arrays.equals(eckey.getPubKey(), getPubKey())) {
        throw new NulsException(AccountErrorCode.PASSWORD_IS_WRONG);
    }
    return eckey;
}
Also used : NulsException(io.nuls.kernel.exception.NulsException) BigInteger(java.math.BigInteger) ECKey(io.nuls.core.tools.crypto.ECKey) CryptoException(io.nuls.core.tools.crypto.Exception.CryptoException)

Example 5 with CryptoException

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

the class AESEncrypt method decrypt.

public static byte[] decrypt(EncryptedData dataToDecrypt, KeyParameter aesKey) throws CryptoException {
    Util.checkNotNull(dataToDecrypt);
    Util.checkNotNull(aesKey);
    try {
        ParametersWithIV keyWithIv = new ParametersWithIV(new KeyParameter(aesKey.getKey()), dataToDecrypt.getInitialisationVector());
        // Decrypt the validator.
        BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
        cipher.init(false, keyWithIv);
        byte[] cipherBytes = dataToDecrypt.getEncryptedBytes();
        byte[] decryptedBytes = new byte[cipher.getOutputSize(cipherBytes.length)];
        final int length1 = cipher.processBytes(cipherBytes, 0, cipherBytes.length, decryptedBytes, 0);
        final int length2 = cipher.doFinal(decryptedBytes, length1);
        return Arrays.copyOf(decryptedBytes, length1 + length2);
    } catch (Exception e) {
        throw new CryptoException();
    }
}
Also used : ParametersWithIV(org.spongycastle.crypto.params.ParametersWithIV) PaddedBufferedBlockCipher(org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher) BufferedBlockCipher(org.spongycastle.crypto.BufferedBlockCipher) PaddedBufferedBlockCipher(org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher) KeyParameter(org.spongycastle.crypto.params.KeyParameter) CBCBlockCipher(org.spongycastle.crypto.modes.CBCBlockCipher) AESFastEngine(org.spongycastle.crypto.engines.AESFastEngine) CryptoException(io.nuls.core.tools.crypto.Exception.CryptoException) CryptoException(io.nuls.core.tools.crypto.Exception.CryptoException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

CryptoException (io.nuls.core.tools.crypto.Exception.CryptoException)6 ECKey (io.nuls.core.tools.crypto.ECKey)4 NulsException (io.nuls.kernel.exception.NulsException)4 BigInteger (java.math.BigInteger)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 AccountPo (io.nuls.account.storage.po.AccountPo)1 AliasPo (io.nuls.account.storage.po.AliasPo)1 NulsRuntimeException (io.nuls.kernel.exception.NulsRuntimeException)1 Result (io.nuls.kernel.model.Result)1 TransferTransaction (io.nuls.protocol.model.tx.TransferTransaction)1 IOException (java.io.IOException)1 BufferedBlockCipher (org.spongycastle.crypto.BufferedBlockCipher)1 AESFastEngine (org.spongycastle.crypto.engines.AESFastEngine)1 CBCBlockCipher (org.spongycastle.crypto.modes.CBCBlockCipher)1 PaddedBufferedBlockCipher (org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher)1 KeyParameter (org.spongycastle.crypto.params.KeyParameter)1 ParametersWithIV (org.spongycastle.crypto.params.ParametersWithIV)1