没有解密我加密的内容

我有一个奇怪的问题……

基于我的解决方案将硬编码文件解密为byte []

所以,我写了一个小的Cypher类来帮助解密/解密…它曾用于模拟在某个地方硬编码的密钥和另一个存储在其他地方的预先加密的密钥。 但这有点无关紧要。

加密过程是这样的:

  • 检索硬编码的字节数组
  • 用它来解密key2
  • 使用key2来解密数据
  • 使用key1进一步解密数据
  • 有解密数据

我将加密数据存储为hex字符串,使用这两个函数进入那里

private static String byteArrayToHexString(byte[] b) { StringBuffer sb = new StringBuffer(b.length * 2); for (int i = 0; i < b.length; i++) { int v = b[i] & 0xff; if (v < 16) { sb.append('0'); } sb.append(Integer.toHexString(v)); } return sb.toString().toUpperCase(); } private static byte[] hexStringToByteArray(String s) { byte[] b = new byte[s.length() / 2]; for (int i = 0; i < b.length; i++) { int index = i * 2; int v = Integer.parseInt(s.substring(index, index + 2), 16); //THIS LINE b[i] = (byte) v; } return b; } 

这完美无瑕; 事实上它工作得很好,我在我的真实项目中实现了它。 由于我没有彻底测试,该项目未能运行。

原来它隐藏/解密几乎所有文件都可以,除了一个 – 那个人不想解密。

我已经确定了这个问题 – 这行抛出了IllegalNumberFormatexception; 在某些时候我也熟悉了这个http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6259307 。 如果某人描述了一种绕过长度为2的字符串转换为四个字节并抛出IllegalNumberFormatException的情况,我会并且可以恢复到这种方法。

所以,我想,因为我无法解码文件(显然不能在这里分享它供你们尝试)我需要以某种方式改变它以使其运输安全。 输入编码为base64字符串的Base64Coder类…

这似乎引入了一个新问题 – 填充正在逐渐消失。

问题很简单 – 我做错了什么? 我需要符合这些数据,它必须能够正确地和平等地加密/解密。 我想要一个关于最轻量级解决方案的建议,最少复制/粘贴…伪代码不会在这里做。

这就是我现在正在做的……

 public static char[] encrypt2(byte[] value) throws GeneralSecurityException, IOException { SecretKeySpec key1 = getSecretKeySpec(true); System.err.println("encrypt():\t" + key1.toString()); Cipher cipher = Cipher.getInstance(CRYPTOSYS); cipher.init(Cipher.ENCRYPT_MODE, key1, cipher.getParameters()); byte[] encrypted = cipher.doFinal(value); SecretKeySpec key2 = getSecretKeySpec(false); cipher = Cipher.getInstance(CRYPTOSYS); cipher.init(Cipher.ENCRYPT_MODE, key2, cipher.getParameters()); byte[] encrypted2 = cipher.doFinal(encrypted); return Base64Coder.encode(encrypted2); } public static byte[] decrypt2(char[] message) throws GeneralSecurityException, IOException { SecretKeySpec key1 = getSecretKeySpec(false); System.err.println("decrypt():\t" + key1.toString()); Cipher cipher = Cipher.getInstance(CRYPTOSYS); cipher.init(Cipher.DECRYPT_MODE, key1); byte[] decrypted = cipher.doFinal(Base64Coder.decode(message)); SecretKeySpec key2 = getSecretKeySpec(true); cipher = Cipher.getInstance(CRYPTOSYS); cipher.init(Cipher.DECRYPT_MODE, key2); byte[] decrypted2 = cipher.doFinal(decrypted); return decrypted2; } 

请注意,为了测试目的,密钥当前是完全公开的(硬编码)。

这是我的测试用例

 public static void main(String... args) throws Exception { // byte[] data = "hello".getBytes(); File PEM = new File(PATH_TO_FILES + SOURCE_PEM); File DER = new File(PATH_TO_FILES + SOURCE_DER); File cryptoPEM = new File(PATH_TO_FILES + "cryptopem"); File cryptoDER = new File(PATH_TO_FILES + "cryptoder"); byte[] cryptokey = encryptA(ASSET_KEY); System.out.println(new String(cryptokey)); //pem key System.out.println("PEM"); byte[] data = getBytesFromFile(PEM); char[] crypted = encrypt2(data); // FileOutputStream fos = new FileOutputStream(cryptoPEM); FileWriter fw = new FileWriter(cryptoPEM); fw.write(crypted); fw.flush(); //der key System.out.println("DER"); data = getBytesFromFile(DER); crypted = encrypt2(data); fw = new FileWriter(cryptoDER); fw.write(crypted); fw.flush(); //opentext System.out.println("checking PEM..."); crypted = Base64Coder.encode(getBytesFromFile(cryptoPEM)); byte[] decrypted = decrypt2(crypted, false); byte[] decryptedData = decrypted; if (!Arrays.equals(getBytesFromFile(PEM), decryptedData)) { throw new Exception("PEM Data was not decrypted successfully"); } System.out.println("checking DER..."); crypted = Base64Coder.encode(getBytesFromFile(cryptoDER)); decrypted = decrypt2(crypted, false); decryptedData = decrypted; if (!Arrays.equals(getBytesFromFile(DER), decryptedData)) { throw new Exception("DER Data was not decrypted successfully"); } } 

而且我现在得到一个InvalidBlockSizeException ….请有人对此有所了解,我只想让这个工作……

将“key2”替换为后来在“AES / CBC / PKCS5Padding”中使用的IV是我现在正在考虑的一个选项。 除了加密的第二步,基本上没有任何改变。 从理论上和方法上来说,我会保持相同的东西 – 除非当然描述了更好的解决方案。

最后,我想指出这是一个程序员问题,而不是IT安全学生的问题,所以正确的代码比包含不太可能的边缘情况的理论响应更有价值。

编辑:好吧,我不能给你导致IllegalNumberFormatException的数字,因为我从今天早上丢失了代码。 我似乎无法复制这个问题,所以我想尝试找出那部分是没有用的。

以下是样本测试的输出:

 encrypt(): javax.crypto.spec.SecretKeySpec@15dd7 5@  _׵G j  ! c;D i lR?z j\ PEM encrypt(): javax.crypto.spec.SecretKeySpec@15dd7 DER encrypt(): javax.crypto.spec.SecretKeySpec@15dd7 checking PEM... decrypt(): javax.crypto.spec.SecretKeySpec@15c78 Exception in thread "main" javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher 

这意味着Base64有点搞砸了……

我认为问题在于:

 //opentext System.out.println("checking PEM..."); crypted = Base64Coder.encode(getBytesFromFile(cryptoPEM)); 

以前,您将char []写入文件,但现在您正在从文件中读取byte []并在base64中重新编码。 文件的内容应该已经是base64编码的!

您需要一个名为getCharsFromFile的新函数,它返回char []或String并将其直接传递给decrypt2。

有什么理由为什么你不使用Appache Commons?

http://commons.apache.org/codec/apidocs/org/apache/commons/codec/binary/Base64.html

今天早上审查了代码并调整了一点后,我就开始工作了。

 public static byte[] encrypt2(byte[] value) throws GeneralSecurityException, IOException { SecretKeySpec key1 = getSecretKeySpec(true); System.err.println("encrypt():\t" + key1.toString()); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, key1, cipher.getParameters()); byte[] encrypted = cipher.doFinal(value); SecretKeySpec key2 = getSecretKeySpec(false); System.err.println("encrypt():\t" + key2.toString()); cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, key2, new IvParameterSpec(getIV())); byte[] encrypted2 = cipher.doFinal(encrypted); return encrypted2;//Base64Coder.encode(encrypted2); } public static byte[] decrypt2(byte[] message, boolean A) throws GeneralSecurityException, IOException { SecretKeySpec key1 = getSecretKeySpec(false); System.err.println("decrypt():\t" + key1.toString()); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, key1, new IvParameterSpec(getIV())); byte[] decrypted = cipher.doFinal(message); SecretKeySpec key2 = getSecretKeySpec(true); System.err.println("decrypt():\t" + key2.toString()); cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, key2); byte[] decrypted2 = cipher.doFinal(decrypted); return decrypted2; }