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;
}
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;
}
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);
}
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());
}
Aggregations