मैं उच्च-समवर्ती अनुप्रयोग के लिए निम्न कोड का उपयोग करना चाहता हूं जहां कुछ डेटा एन्क्रिप्टेड और डिक्रिप्ट किया जाना चाहिए। तो मुझे पता होना चाहिए कि अप्रत्याशित मुद्दों से बचने के लिए, इस कोड का कौन सा हिस्सा सिंक्रनाइज़ किया जाना चाहिए, यदि कोई है।क्या यह जावा एन्क्रिप्शन कोड थ्रेड सुरक्षित है?
public class DesEncrypter {
Cipher ecipher;
Cipher dcipher;
// 8-byte Salt
byte[] salt = {
(byte)0xA9, (byte)0x9B, (byte)0xC8, (byte)0x32,
(byte)0x56, (byte)0x35, (byte)0xE3, (byte)0x03
};
int iterationCount = 19;
DesEncrypter(String passPhrase) {
try {
// Create the key
KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount);
SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
ecipher = Cipher.getInstance(key.getAlgorithm());
dcipher = Cipher.getInstance(key.getAlgorithm());
// Prepare the parameter to the ciphers
AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);
// Create the ciphers
ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
} catch (...)
}
public String encrypt(String str) {
try {
// Encode the string into bytes using utf-8
byte[] utf8 = str.getBytes("UTF8");
// Encrypt
byte[] enc = ecipher.doFinal(utf8);
// Encode bytes to base64 to get a string
return new sun.misc.BASE64Encoder().encode(enc);
} catch (...)
}
public String decrypt(String str) {
try {
// Decode base64 to get bytes
byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
// Decrypt
byte[] utf8 = dcipher.doFinal(dec);
// Decode using utf-8
return new String(utf8, "UTF8");
} catch (...)
}
}
अगर मैं) एन्क्रिप्ट (में एक नया सिफर बना सकते हैं और प्रत्येक मंगलाचरण के लिए डिक्रिप्ट() पद्धतियों, तो मैं संगामिति समस्याओं से बचने कर सकते हैं, मैं सिर्फ यकीन नहीं है अगर वहाँ एक नया प्राप्त करने में भूमि के ऊपर का एक बहुत है प्रत्येक आमंत्रण के लिए एक सिफर का उदाहरण।
public String encrypt(String str) {
try {
// Encode the string into bytes using utf-8
byte[] utf8 = str.getBytes("UTF8");
// Encrypt
//new cipher instance
ecipher = Cipher.getInstance(key.getAlgorithm());
byte[] enc = ecipher.doFinal(utf8);
// Encode bytes to base64 to get a string
return new sun.misc.BASE64Encoder().encode(enc);
} catch (...)
उसे नहीं पता था, धन्यवाद। मैं केवल आवेदन के लिए उपलब्ध सख्त ओएस अनुमतियों के तहत अलग-अलग फाइलों में नमक और पास-वाक्यांश को स्टोर करने की योजना बना रहा हूं। – user646584
निरंतर नमक का उपयोग करना (भले ही यह संरक्षित है) पहले स्थान पर नमक का उपयोग करने के अधिकांश बिंदु को हरा देता है। –
आप कह रहे हैं कि नमक रनटाइम पर यादृच्छिक रूप से प्राप्त किया जाना चाहिए? मैं उलझन में हूं, स्पष्ट रूप से पास-वाक्यांश बदल नहीं सकता है जब डेटा इसके साथ एन्क्रिप्ट किया गया है, तो एक सुरक्षित फ़ाइल, नमक और पासफ्रेज, बस पासफ्रेज इत्यादि के सामने आगे बढ़ने और संग्रहीत करने की आवश्यकता क्या है? – user646584