加密工作最佳,看起来文本在查看文件时似乎是加密的。我虽然坚持解密过程并且有错误:
"线程中的异常" main"显示java.lang.NullPointerException 在java.util.Arrays.copyOfRange(Arrays.java:3521)"
我用这个方法调用方法:
char [] password;
byte [] encrypted;
decrypt(input, new File(outFile),encrypted,password);
//解密方法
public void decrypt(File inputFile, File outputFile , byte [] encrypted, char [] password) throws Exception {
byte[] iv = Arrays.copyOfRange(encrypted, 0, IV_SIZE_IN_BITS / 8);
byte[] ciphertext = Arrays.copyOfRange(encrypted, iv.length, encrypted.length);
Cipher cipher = initCipher(Cipher.DECRYPT_MODE, password,iv);
FileOutputStream fos = null;
CipherInputStream cis = null;
FileInputStream fis = null;
try {
fis = new FileInputStream(inputFile);
cis = new CipherInputStream(fis, cipher);
fos = new FileOutputStream(outputFile);
byte[] data = new byte[1024];
int read = cis.read(data);
while (read != -1) {
fos.write(data, 0, read);
read = cis.read(data);
System.out.println(new String(data, "UTF-8").trim());
}
} finally {
cis.close();
fos.close();
fis.close();
}
}
NEW ---------
private static final String KEY_DERIVATION_FUNCTION = "PBKDF2WithHmacSHA256";
private static final String ENCRYPTION_ALGORITHM = "AES";
private static final String TRANSFORMATION = "AES/GCM/NoPadding";
private static final String GENERATOR_ALGORITHM = "SHA1PRNG";
private static final int KEY_SIZE_IN_BITS = 128;
private static final int IV_SIZE_IN_BITS = 128;
private static final int TAG_SIZE_IN_BITS = 128;
private static final int ITERATION_COUNT = 200_000;
private static Cipher initCipher(int mode,char [] password, byte[] iv) {
GCMParameterSpec gcmSpec = new GCMParameterSpec(TAG_SIZE_IN_BITS, iv);
SecretKey key = deriveKey(password, iv);
try {
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(mode, key, gcmSpec);
return cipher;
}
catch (
NoSuchAlgorithmException |
NoSuchPaddingException |
InvalidKeyException |
InvalidAlgorithmParameterException ex
) {
throw new AssertionError(ex);
}
}
答案 0 :(得分:1)
如果您真的使用此代码调用解密方法:
char [] password;
byte [] encrypted;
decrypt(input, new File(outFile),encrypted,password);
然后您的password
和encrypted
变量尚未初始化,并且均为空,因此为NullPointerException
。您必须将变量设置为引用包含加密数据和密码的实际数组。
答案 1 :(得分:1)
您没有使用以下命令指定加密算法,模式或填充:
Cipher cipher = initCipher(Cipher.DECRYPT_MODE, password,iv);
似乎它是默认的AES以外的算法,你想用什么算法?
始终准确指定您需要的内容,不要依赖默认值。我怀疑在你的情况下你需要算法:AES,模式:CBC,填充:PKCS#7(有时指定PKCS#5)。但这些必须与加密完全匹配。还为密钥和iv提供所需的精确长度,密钥长度使用128位,192位或256位(16字节24字节或32字节)和iv 16字节。
错误javax.crypto.AEADBadTagException
表示:
当以AEAD模式运行的密码(例如GCM / CCM)无法验证提供的身份验证标记时,抛出此异常。
答案 2 :(得分:0)
来自Java文档Java.util.Arrays.copyOfRange()的错误import csv, sympy
import sympy as sym
fw = open('polyexp.csv', "w")
fr = open('polyval.csv', "r")
a,b,c, d, e= sympy.symbols("a b c d e")
formula = (a+b+c+d+e) ** 3
poly = str(formula.expand())
ls = poly.split('+')
for i in range(len(ls)):
ls_val[i] = sym.sympify(ls[i])
for line in fr:
tup = line.split(",")
csvrow = list()
for key in range(len(ls_val)):
new = ls_val[key].subs([(a, tup[0]),(b,tup[1]),(c,tup[2]),(d,tup[3]),(e,tup[4])])
csvrow.append(new)
print >> fw, csvrow
:
NullPointerException - 如果original为null。
检查实际数据长度与请求的范围。同时验证java.lang.NullPointerException
,IV_SIZE_IN_BITS
和iv.length
的值。