में काम करता है मेरा एप्लिकेशन विंडोज़ में काम करता है, लेकिन लिनक्स में Given final block not properly padded
अपवाद के साथ विफल रहता है।अपवाद: "लिनक्स में अंतिम ब्लॉक को सही ढंग से पैड नहीं किया गया है", लेकिन यह विंडोज
विन्यास:
- JDK संस्करण: 1.6
- विंडोज: संस्करण 7
- लिनक्स: 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
मैं कैसे इस समस्या को हल कर सकते हैं?
मुझे आशा है कि आपको सुरक्षा की परवाह नहीं है। डीईएस और ईसीबी दोनों एक संदिग्ध विकल्प हैं। – CodesInChaos
आप मान रहे हैं कि 'SecureRandom' सभी प्लेटफ़ॉर्म पर समान तरीके से व्यवहार करता है। क्या जावा गारंटी देता है? एक कुंजी प्राप्त करने के लिए एक केडीएफ का प्रयोग करें, न कि पीआरएनजी। – CodesInChaos
क्या आप सुनिश्चित हैं कि 'कच्चे' में दोनों प्लेटफॉर्म पर एक ही डेटा है? डेटा को डीकोडके() 'में पास करने से पहले मुझे कुछ गलत एन्कोडिंग कवरेज पर संदेह होगा, हालांकि यदि आप बेस 64 का उपयोग करते हैं तो यह असंभव है। – axtavt