私のアプリケーションはウィンドウでは動作しますが、Linuxでは例外が発生します。例外:Linuxで "最終ブロックに適切な埋め込みができませんでした"が、Windowsで動作します
構成:
- JDKのバージョン:1.6
- のWindows:バージョン7
- のLinux:CentOSの5.8 64ビット
私のコードは以下の通りです:
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class SecurityKey {
private static Key key = null;
private static String encode = "UTF-8";
private static String cipherKey = "DES/ECB/PKCS5Padding";
static {
try {
KeyGenerator generator = KeyGenerator.getInstance("DES");
String seedStr = "test";
generator.init(new SecureRandom(seedStr.getBytes()));
key = generator.generateKey();
} catch(Exception e) {
}
}
// SecurityKey.decodeKey("password")
public static String decodeKey(String str) throws Exception {
if(str == null)
return str;
Cipher cipher = null;
byte[] raw = null;
BASE64Decoder decoder = new BASE64Decoder();
String result = null;
cipher = Cipher.getInstance(cipherKey);
cipher.init(Cipher.DECRYPT_MODE, key);
raw = decoder.decodeBuffer(str);
byte[] stringBytes = null;
stringBytes = cipher.doFinal(raw); // Exception!!!!
result = new String(stringBytes, encode);
return result;
}
}
で行:
ciper.doFilnal(raw);
次の例外がスローされます。
javax.crypto.BadPaddingException: Given final block not properly padded
私はこの問題をどのように修正することができますか?
私はあなたがセキュリティを気にしないことを願っています。 DESとECBの両方は、疑わしい選択です。 – CodesInChaos
'SecureRandom'はすべてのプラットフォームで同じように動作すると仮定しています。 Javaはそれを保証しますか? PRNGではなく、キーを導出するためにKDFを使用します。 – CodesInChaos
'raw'には両方のプラットフォームで同じデータが含まれていますか?私は 'decodeKey()'にデータを渡す前に間違ったエンコーディングの変換を行っていると思われますが、base64を使用しているとは思いません。 – axtavt