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