我使用openssl
加密文件,然后将其放在HDFS上,我使用AES / ECB,128位和盐选项,并且通过一些研究我发现openssl使用PKCS5填充作为默认值,这些都是{{{ 3}}。这是我的加密过程:
# echo -n "password" > .pw
# openssl enc -aes-128-ecb -salt -in .pw -out .pw.enc
# hdfs dfs -put .pw.enc /user/user1/
Sqoop版本是1.4.6
命令:
sqoop import \
-Dorg.apache.sqoop.credentials.loader.class=org.apache.sqoop.util.password.CryptoFileLoader \
-Dorg.apache.sqoop.credentials.loader.crypto.passphrase=sqoop \
--connect jdbc:oracle:thin:@host/database \
--username user1 \
--password-file /user/user1/.pw.enc \
--table db.table1 \
--hive-import \
--hive-overwrite \
--hive-table hivedb.table1 \
--hive-drop-import-delims
给出:
17/03/08 15:10:37 WARN tool.BaseSqoopTool: Failed to load password file
java.io.IOException: Can't decrypt the password
at org.apache.sqoop.util.password.CryptoFileLoader.loadPassword(CryptoFileLoader.java:151)
at org.apache.sqoop.util.CredentialsUtil.fetchPasswordFromLoader(CredentialsUtil.java:81)
at org.apache.sqoop.util.CredentialsUtil.fetchPassword(CredentialsUtil.java:66)
at org.apache.sqoop.tool.BaseSqoopTool.applyCredentialsOptions(BaseSqoopTool.java:1042)
at org.apache.sqoop.tool.BaseSqoopTool.applyCommonOptions(BaseSqoopTool.java:997)
at org.apache.sqoop.tool.ImportTool.applyOptions(ImportTool.java:875)
at org.apache.sqoop.tool.SqoopTool.parseArguments(SqoopTool.java:435)
at org.apache.sqoop.Sqoop.run(Sqoop.java:131)
at org.apache.hadoop.util.ToolRunner.run(ToolRunner.java:70)
at org.apache.sqoop.Sqoop.runSqoop(Sqoop.java:179)
at org.apache.sqoop.Sqoop.runTool(Sqoop.java:218)
at org.apache.sqoop.Sqoop.runTool(Sqoop.java:227)
at org.apache.sqoop.Sqoop.main(Sqoop.java:236)
Caused by: javax.crypto.BadPaddingException: Given final block not properly padded
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:966)
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:824)
at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:436)
at javax.crypto.Cipher.doFinal(Cipher.java:2165)
at org.apache.sqoop.util.password.CryptoFileLoader.loadPassword(CryptoFileLoader.java:149)
... 12 more
Error while loading password file: Can't decrypt the password
我尝试手动提供其他CryptoFileLoader参数,并将本地文件传递给--password-file
。
我可以使用openssl
成功解密文件。我无法用Java程序解密(?)
我看到填充有问题,但我不知道它是什么以及如何使用某种填充方法或其他任何方法加密文件,我没有加密经验。
类中还有org.apache.sqoop.credentials.loader.crypto.iterations
参数,表示PBKDF2迭代次数,但我不知道它是否会发生任何变化。
感谢您的帮助。
答案 0 :(得分:2)
我不是Sqoop和Hadoop的专家,但从您的例外开始
CryptoFileLoader.loadPassword(CryptoFileLoader.java:151)
的源代码
在我看来,事情与您所做的有些不同:密码使用 PBKDF2 算法存储在加密文件中,这不等同于应用AES-128-ECB。来自wikipedia:
PBKDF2将伪随机函数(例如基于散列的消息验证代码(HMAC))应用于输入密码或密码以及salt值,并多次重复该过程以生成派生密钥,然后可将其用作后续操作中的加密密钥。增加的计算工作使密码破解变得更加困难,并且被称为密钥扩展。
无法从Openssl命令行执行PBKDF2。我用Java做了一个小测试,它可能是另一种选择
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
public class Test {
/*Default is AES in electronic code book with padding.*/
private static String DEFAULT_ALG = "AES/ECB/PKCS5Padding";
/*Default salt is not much secure, use your own!*/
private static String DEFAULT_SALT = "SALT";
/*Iterate 10000 times by default.*/
private static int DEFAULT_ITERATIONS = 10000;
/*One of valid key sizes for default algorithm (AES).*/
private static int DEFAULT_KEY_LEN = 128;
public static void main(String[] args) throws IOException {
String inputFileName = "C:\\temp\\in.txt"; /*Enter your input (plain) file path */
String outputFileName = "C:\\temp\\out.bin"; /*Enter your output (encrypted) file path */
String passPhrase = "mypassphrase"; /*Enter your passphrase */
String salt = DEFAULT_SALT;
String alg = DEFAULT_ALG;
int iterations = DEFAULT_ITERATIONS;
int keyLen = DEFAULT_KEY_LEN;
SecretKeyFactory factory = null;
try {
factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
} catch (NoSuchAlgorithmException e) {
throw new IOException("Can't load SecretKeyFactory", e);
}
SecretKeySpec key = null;
try {
String algOnly = alg.split("/")[0];
key = new SecretKeySpec(
factory.generateSecret(
new PBEKeySpec(passPhrase.toCharArray(), salt.getBytes(), iterations, keyLen)).getEncoded(),
algOnly);
} catch (Exception e) {
throw new IOException("Can't generate secret key", e);
}
Cipher crypto = null;
try {
crypto = Cipher.getInstance(alg);
} catch (Exception e) {
throw new IOException("Can't initialize the decryptor", e);
}
Path inputFileLocation = Paths.get(inputFileName);
byte[] decrypted = Files.readAllBytes(inputFileLocation);
byte[] encrypted;
try {
crypto.init(Cipher.ENCRYPT_MODE, key);
encrypted = crypto.doFinal(decrypted);
} catch (Exception e) {
throw new IOException("Can't decrypt the password", e);
}
Path outputFileLocation = Paths.get(outputFileName);
Files.write(outputFileLocation, encrypted);
}
}
答案 1 :(得分:1)
正如Simone的回答 - openssl和java实现之间的加密算法存在差异。这就是为什么你可以使用openssl解密而没有问题(因为它再次调用它自己的(不同的)算法)。
经过多次挖掘后,我发现(Dave Thompson)的答案表明:
简答:openssl enc(没有-K for raw)使用的不是PBKDF2;它几乎是PBKDF1,迭代次数为1。
这个问题似乎有两种解决方法:
a)在java中查找可以解密openssl正在做什么的东西 - 在answer内的帖子中引用了一个java库'BouncyCastle'(如果你很乐意使用它而不是标准的CryptoFile)已经实现了openssl正在使用的完全相同的算法。
或
b)找到一些其他命令行实用程序来代替实现PBKDF2的openssl。在nabble.com posting中也提到了许多不同语言的实现。
(Dave对所引用的关键观察结果给予了应有的评价)