Skip to content

Commit 23305f8

Browse files
committed
small refectoring, make it simpler
1 parent 5c91f85 commit 23305f8

File tree

1 file changed

+8
-22
lines changed

1 file changed

+8
-22
lines changed

src/main/java/prof7bit/bitcoin/wallettool/fileformats/BlockchainInfoHandler.xtend

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class BlockchainInfoHandler extends AbstractImportExportHandler{
7171
if (pass == null){
7272
throw new Exception("not possible without password")
7373
}
74-
val decrypted = decrypt(payload, pass, iterations)
74+
val decrypted = decryptWithPass(payload, pass, iterations)
7575
parseAndImport(decrypted, pass2)
7676
}
7777

@@ -114,7 +114,7 @@ class BlockchainInfoHandler extends AbstractImportExportHandler{
114114
if (bciKey.has("priv")){
115115
if (doubleEnc){
116116
val doubleEncText = bciKey.getString("priv")
117-
priv = decrypt(doubleEncText, doubleEncSalt, pass2, doubleEncIter)
117+
priv = decryptWithPass(doubleEncText, doubleEncSalt + pass2, doubleEncIter)
118118
} else {
119119
priv = bciKey.getString("priv")
120120
}
@@ -158,7 +158,7 @@ class BlockchainInfoHandler extends AbstractImportExportHandler{
158158
* @return decrypted plain text. This would be the JSON string representation of the wallet
159159
* @throws InvalidCipherTextException if decryption fails
160160
*/
161-
private def decrypt(String cipherText, String password, int iterations) throws InvalidCipherTextException {
161+
private def decryptWithPass(String cipherText, String password, int iterations) throws InvalidCipherTextException {
162162
val cipherdata = Base64.decode(cipherText);
163163

164164
//Separate the IV and cipher data
@@ -168,18 +168,6 @@ class BlockchainInfoHandler extends AbstractImportExportHandler{
168168
return decrypt(iv, input, password, iterations)
169169
}
170170

171-
/**
172-
* This is used to decrypt the individual key with the secondary password
173-
* @param encPrivKey the base64 encoded encrypted private key (from the "priv" field)
174-
* @param sharedKey used as a password salt (this is taken from the "sharedKey" field)
175-
* @param password2 the secondary password for this wallet
176-
* @param iterations as found in the "pbkdf2_iterations" field of the wallet
177-
* @throws InvalidCipherTextException if decryption fails
178-
*/
179-
private def decrypt(String encPrivKey, String sharedKey, String password2, int iterations) throws InvalidCipherTextException {
180-
decrypt(encPrivKey, sharedKey + password2, iterations)
181-
}
182-
183171
/**
184172
* Decrypt the byte array according to the specifications given by blockchain.info.
185173
* @param iv initialization vector used to initialize the cipher
@@ -211,16 +199,14 @@ class BlockchainInfoHandler extends AbstractImportExportHandler{
211199
* @return a newly initialized Cipher object usable to encrypt or decrypt the data from blockchain.info
212200
*/
213201
private def createCipher(String password, byte[] iv, int PBKDF2Iterations, Boolean forEncryption){
214-
val generator = new PKCS5S2ParametersGenerator
215202
val passbytes = PBEParametersGenerator.PKCS5PasswordToUTF8Bytes(password.toCharArray())
216-
generator.init(passbytes, iv, PBKDF2Iterations)
217-
val keyParam = generator.generateDerivedParameters(AESKeySize)
218203

219-
val params = new ParametersWithIV(keyParam, iv)
204+
val generator = new PKCS5S2ParametersGenerator
205+
generator.init(passbytes, iv, PBKDF2Iterations)
206+
val keyParams = new ParametersWithIV(generator.generateDerivedParameters(AESKeySize), iv)
220207

221-
val padding = new ISO10126d2Padding
222-
val cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), padding)
223-
cipher.init(forEncryption, params)
208+
val cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new ISO10126d2Padding)
209+
cipher.init(forEncryption, keyParams)
224210
return cipher
225211
}
226212

0 commit comments

Comments
 (0)