الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

التشفير باستخدام خوارزميةDES ومشكلة استقبال مفتاح تشفير من TExtfield

مغلق
بدأه alfajr2007 في 18 مايو 2007 · 9 رد · 1,082 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

1Cipher encrypt,decrypt;

2-SecretKey Key;

3-RandomAccessFile ranf=null;

4-FileOutputStream out=null;

5-{try{

6-KeyGenerator Kg=KeyGenerator.getInstance("DES");

7-Key=Kg.generateKey() ;

8-String skey=t1.gettext();

9-key=skey;

ااخوتي الكرام انا استخدم خوارزمية DES في تشفير الملفات والشفرة السابقة تعمل على توليد مفتاح سري للتشفير اليا وعن

طريق نفس المفتاح يمكن اعادة فتح الملف المشفر بهذه الخوارزمية.

انا احاول ان يكون المفتاح مرسل من textfiled ولكن وجدت صعوبة في الخطوة رقم 9 في التحويل فقيمة

TexteField عبارة عن String والمفتاح الذي تحتاجه الدالة Init() للتشفير وفك التشفير يجب ان يكون من كائن من نوعSecretKey فكيف استطيع التحويل جزاكم الله كل خير

فهل هناك دلة للتحويل ولكم جزيل الشكر اخواني

انما يتعثر من لم يخلص----ابن القيم

الواجبات اكثر من الاوقات فساعد غيرك على الاستفادة من وقته ---حسن البنا

#2

الموضوع الذي تتحدث عنه يسمى Password Based Encryption

ابحث عنه و ستجد الكثير باذن الله

منقطع من حين لاخر

#3

حياك الله ياابو فؤاد وشكرا كثيرا على الرد

ولقد بحثت كثيرا كما تقول ولكن بصدق تهت فكلها مواضيع عامة ولم اجد ضالتي ووجدت حل في منتدى شركة صن ان حل المشكلة موجود في jdk1.6فهل هذا صحيح وكذالك لا املك jdk1.6 هل يوجد حل اخواني الكرام ارجو الافادة

(h) اخوكم ابوعزام

انما يتعثر من لم يخلص----ابن القيم

الواجبات اكثر من الاوقات فساعد غيرك على الاستفادة من وقته ---حسن البنا

#4

ضع الحل الذي وجدته هنا

منقطع من حين لاخر

#5

حياك الله اخي الكريم هذا ماوجدته

import java.io.*;

import javax.crypto.*;

import javax.crypto.interfaces.*;

import javax.crypto.spec.*;

import java.security.spec.*;

import java.util.zip.*;

import java.util.*;

/**

* Test Triple DES

*/

public class DesCoder {

public String initCipher() {

String result = "";

String passPhrase = "test";

try { // 8-byte Salt

byte[] salt = {(byte)0xA9, (byte)0x9B, (byte)0xC8, (byte)0x32,

(byte)0x56, (byte)0x35, (byte)0xE3, (byte)0x03 };

int iterationCount = 19;

KeySpec keySpec =new DESKeySpec( passPhrase.toCharArray(),salt, iterationCount );

/ *SecretKey key = SecretKeyFactory.getInstance("DES" ).generateSecret( keySpec ); */

SecretKey key = SecretKeyFactory.getInstance("DES" ).generateSecret( keySpec );

Cipher cipher = Cipher.getInstance( key.getAlgorithm() );

//المشكلة التي تواجهني هنا حيث لم يتعرف الاصدار jdk1.4 على هذا الصنف AlgorithmParameterSpec

AlgorithmParameterSpec paramSpec =

new DESParameterSpec( salt, iterationCount );

// Initialize the ciphers

cipher.init( Cipher.ENCRYPT_MODE, key, paramSpec );

result = "All is well";

}

catch ( java.security.InvalidAlgorithmParameterException e ) {

result = e.getMessage();

}

catch ( java.security.spec.InvalidKeySpecException e ) {

result = e.getMessage();

}

catch ( javax.crypto.NoSuchPaddingException e ) {

result = e.getMessage();

}

catch ( java.security.NoSuchAlgorithmException e ) {

result = e.getMessage();

}

catch ( java.security.InvalidKeyException e ) {

result = e.getMessage();

}

catch ( Exception e ) {

result = e.getMessage();

return result;

}

public static void main( String[] args ) {

System.out.println( new DesCoder().initCipher() );

}

}

وجزاكم الله كل خير

galal79@yahoo.com

_____1.txt

انما يتعثر من لم يخلص----ابن القيم

الواجبات اكثر من الاوقات فساعد غيرك على الاستفادة من وقته ---حسن البنا

#6

المشكلة في الكلاس DESParameterSpec

وهو ليس موجود في الجافا

اقتباس
This class has been developed by IAIK according to the documentation publically available.

For SUN´s documentation of this class see

http://java.sun.com/security/JCE1.2/earlya...amaterSpec.html

http://javadoc.iaik.tugraz.at/iaik_jce/cur...ameterSpec.html

منقطع من حين لاخر

#7

بص في ال documentaion بتاعة الفئه SecretKey وشوف ال constructor بيقبل إيه ، لو بيقبل String يبقي إتحلت

تم تعديل هذه المشاركة بواسطة مبتدئ جافا في 1 يونيو 2007 في 17:48

#8

السلام عليكم..للاسف Secret عبارة عن واجهة interface وليس صنف وبالتالي لايمكك دالة بناء

انما يتعثر من لم يخلص----ابن القيم

الواجبات اكثر من الاوقات فساعد غيرك على الاستفادة من وقته ---حسن البنا

#10

Using Password-Based Encryption

In this example, we prompt the user for a password from which we derive an encryption key.

It would seem logical to collect and store the password in an object of type java.lang.String. However, here's the caveat: Objects of type String are immutable, i.e., there are no methods defined that allow you to change (overwrite) or zero out the contents of a String after usage. This feature makes String objects unsuitable for storing security sensitive information such as user passwords. You should always collect and store security sensitive information in a char array instead.

For that reason, the javax.crypto.spec.PBEKeySpec class takes (and returns) a password as a char array.

The following method is an example of how to collect a user password as a char array:


* Reads user password from given input stream.
*/
public char[] readPasswd(InputStream in) throws IOException {
char[] lineBuffer;
char[] buf;
int i;

buf = lineBuffer = new char[128];

int room = buf.length;
int offset = 0;
int c;

loop: while (true) {
switch (c = in.read()) {
case -1:
case '\n':
break loop;

case '\r':
int c2 = in.read();
if ((c2 != '\n') && (c2 != -1)) {
if (!(in instanceof PushbackInputStream)) {
in = new PushbackInputStream(in);
}
((PushbackInputStream)in).unread(c2);
} else
break loop;

default:
if (--room < 0) {
buf = new char[offset + 128];
room = buf.length - offset - 1;
System.arraycopy(lineBuffer, 0, buf, 0, offset);
Arrays.fill(lineBuffer, ' ');
lineBuffer = buf;
}
buf[offset++] = (char) c;
break;
}
}

if (offset == 0) {
return null;
}

char[] ret = new char[offset];
System.arraycopy(buf, 0, ret, 0, offset);
Arrays.fill(buf, ' ');

return ret;
}
		/**

In order to use Password-Based Encryption (PBE) as defined in PKCS #5, we have to specify a salt and an iteration count. The same salt and iteration count that are used for encryption must be used for decryption:


PBEParameterSpec pbeParamSpec;
SecretKeyFactory keyFac;

// Salt
byte[] salt = {
(byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c,
(byte)0x7e, (byte)0xc8, (byte)0xee, (byte)0x99
};

// Iteration count
int count = 20;

// Create PBE parameter set
pbeParamSpec = new PBEParameterSpec(salt, count);

// Prompt user for encryption password.
// Collect user password as char array (using the
// "readPasswd" method from above), and convert
// it into a SecretKey object, using a PBE key
// factory.
System.out.print("Enter encryption password: ");
System.out.flush();
pbeKeySpec = new PBEKeySpec(readPasswd(System.in));
keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);

// Create PBE Cipher
Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");

// Initialize PBE Cipher with key and parameters
pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);

// Our cleartext
byte[] cleartext = "This is another example".getBytes();

// Encrypt the cleartext
byte[] ciphertext = pbeCipher.doFinal(cleartext);
		PBEKeySpec pbeKeySpec;

http://java.sun.com/j2se/1.4.2/docs/guide/...CERefGuide.html

منقطع من حين لاخر

هذا الموضوع مغلق.

مواضيع مشابهة

عدد الزوار حالياً

المتواجدون خلال آخر دقيقتين · يتحدّث كل ٣٠ ثانية

—الإجمالي—أعضاء مسجّلون—زوار بدون تسجيل

جارٍ التحقق من المتواجدين…