Wednesday, March 31, 2010

Enkripsi Menggunakan XOR

Cryptography adalah suatu ilmu ataupun seni mengamankan pesan, dan dilakukan oleh cryptographer. Sedang, cryptanalysis adalah suatu ilmu dan seni membuka (breaking) ciphertext dan orang yang melakukannya disebut cryptanalyst.

Suatu pesan yang tidak disandikan disebut sebagai plaintext ataupun dapat disebut juga sebagai cleartext. Proses yang dilakukan untuk mengubah plaintext ke dalam ciphertext disebut encryption atau encipherment. Sedangkan proses untuk mengubah ciphertext kembali ke plaintext disebut decryption atau decipherment.

Berikut ini adalah contoh
enkripsi menggunakan XOR :




Berikut ini Source Code-nya menggunakan Java Applet :

import java.applet.*;
import java.awt.*;

public class EncryptXOR extends Applet
{
// Applet instance variables:

private Dimension dApplet = null;
private Image imOffScreen = null;
private Graphics grOffScreen = null;

private Button btnRandomKey = null;
private Button btnEncrypt = null;

private TextArea taCryptText = null;
private TextArea taPlainText = null;
private TextArea taKeyText = null;

private Label lblOne = null; // First label.
private Label lblTwo = null; // Second label.
private Label lblThree = null; // Third label.
private Label lblMessage = null; // Message label.


// Initialize the applet
public void init()
{
int i = 0;
this.setBackground(Color.lightGray);
GridBagLayout gbl = null;
GridBagConstraints gbc = null;

dApplet = this.size();

gbl = new GridBagLayout();
this.setLayout(gbl);

gbc = new GridBagConstraints();
gbc.insets = new Insets(5, 10, 5, 10); // top, left, bottom, right.
gbc.weightx = 0.0;
gbc.weighty = 0.0;
gbc.gridx = 0;

// First Label
lblOne = new Label("Plain Text", Label.CENTER);
gbc.gridy = 0;
gbl.setConstraints(lblOne, gbc);
this.add(lblOne);

// Add the plain text box:
taPlainText = new TextArea("", 6, 80); // 6 rows and 80 columns.
taPlainText.setEditable(true);
gbc.gridy = 1;
gbl.setConstraints(taPlainText, gbc);
this.add(taPlainText);

// Second Label
lblTwo = new Label("Key Text", Label.CENTER);
gbc.gridy = 2;
gbl.setConstraints(lblTwo, gbc);
this.add(lblTwo);

// Add the Key text box:
taKeyText = new TextArea("", 6, 80); // 6 rows and 80 columns.
taKeyText.setEditable(true);
gbc.gridy = 3;
gbl.setConstraints(taKeyText, gbc);
this.add(taKeyText);

// Add the random button:
btnRandomKey = new Button("Generate Random Key");
btnRandomKey.setForeground(Color.black);
btnRandomKey.setBackground(Color.lightGray);
gbc.gridy = 4;
gbl.setConstraints(btnRandomKey, gbc);
this.add(btnRandomKey);

// Message Label
lblMessage = new Label( "Type or paste text into the upper two text boxes.", Label.CENTER);
gbc.gridy = 5;
gbl.setConstraints(lblMessage, gbc);
this.add(lblMessage);

// Add the encrypt button:
btnEncrypt = new Button("Encrypt the Text");
btnEncrypt.setForeground(Color.black);
btnEncrypt.setBackground(Color.lightGray);
gbc.gridy = 6;
gbl.setConstraints(btnEncrypt, gbc);
this.add(btnEncrypt);

// Third Label
lblThree = new Label("Encrypted Text", Label.CENTER);
gbc.gridy = 7;
gbl.setConstraints(lblThree, gbc);
this.add(lblThree);

// Add the crypt text box:
taCryptText = new TextArea("", 6, 80); // 6 rows and 80 columns.
taCryptText.setEditable(true);
gbc.gridy = 8;
gbl.setConstraints(taCryptText, gbc);
this.add(taCryptText);

} // End of init()

// Execute this code after initialization
public void start()
{
System.out.println("\n" + this.getAppletInfo()); // Identify self to the Java console-aware user
taPlainText.requestFocus();
} // End of start()

// Implements double buffering
public void update(Graphics g)
{
if (imOffScreen == null)
{
// Make sure the offscreen and graphics exist:
imOffScreen = this.createImage(dApplet.width, dApplet.height);
grOffScreen = imOffScreen.getGraphics();
grOffScreen.clearRect(0, 0, dApplet.width, dApplet.height);
}
this.paint(grOffScreen);
g.drawImage(imOffScreen, 0, 0, null);
}

// The applet frame painting function
public void paint(Graphics g)
{
// Code for displaying images or drawing in the applet frame (called by the OS).
g.clearRect(0, 0, dApplet.width, dApplet.height); // Needed for double buffering.
this.setBackground(Color.lightGray); // Ditto.

drawFrame(g); // Draw the frame abound the applet.
} // End of function paint()

private void drawFrame(Graphics g)
{
// Draw a recessed frame around the applet border. Designed for gray-on-gray browser background.
g.setColor(Color.black);
g.drawLine(0, 0, dApplet.width - 1, 0);
g.drawLine(0, 0, 0, dApplet.height - 1);
g.setColor(Color.white);
g.drawLine(0, dApplet.height - 1, dApplet.width - 1, dApplet.height - 1);
g.drawLine(dApplet.width - 1, 1, dApplet.width - 1, dApplet.height - 1);
}

public boolean action(Event e, Object o)
{
if (e.target == btnEncrypt)
{
btnEncrypt.disable();
if (taPlainText.getText().length() == 0)
{
System.out.println("Plaintext Empty");
lblMessage.setText("Plaintext Empty");
}
else
{
if (taKeyText.getText().length() == 0)
{
System.out.println("Keytext Empty");
lblMessage.setText("Keytext Empty");
}
else
{
// We can do the key computation
encrypt();
}
}
btnEncrypt.enable();
}

if (e.target == btnRandomKey)
{
int i = 0;
int iTemp = 0;
int iPlainLength = 0;
StringBuffer sbTemp = null;

sbTemp = new StringBuffer();
iPlainLength = taPlainText.getText().length();
if (iPlainLength > 0)
{
for (i = 0; i < iPlainLength; i++)
{
iTemp = (int) (128 * Math.random());
sbTemp.append(iTemp);
if (i < iPlainLength - 1)
{
sbTemp.append(' ');
}
}
taKeyText.setText(sbTemp.toString());
lblMessage.setText("Generated random key of " + iPlainLength + " characters.");
}
else
{
taKeyText.setText("");
lblMessage.setText("Need some plaintext to generate a key.");
}
}
return true; // Absorb the event;
} // End of action().

public boolean handleEvent(Event e)
{
int iLength = 0;

if (e.target == btnEncrypt && e.id == Event.ACTION_EVENT)
{
btnEncrypt.disable();
if (taPlainText.getText().length() == 0)
{
System.out.println("Plaintext Empty");
lblMessage.setText("Plaintext Empty");
}
else
{
if (taKeyText.getText().length() == 0)
{
System.out.println("Keytext Empty");
lblMessage.setText("Keytext Empty");
}
else
{
// We can do the key computation
encrypt();
}
}
btnEncrypt.enable();
return true;
}

if (e.target == btnRandomKey && e.id == Event.ACTION_EVENT)
{
int i = 0;
int iTemp = 0;
int iPlainLength = 0;
StringBuffer sbTemp = null;

sbTemp = new StringBuffer();
iPlainLength = taPlainText.getText().length();
if (iPlainLength > 0)
{
for (i = 0; i < iPlainLength; i++)
{
iTemp = (int) (128 * Math.random());
sbTemp.append(iTemp);
if (i < iPlainLength - 1)
{
sbTemp.append(' ');
}
}
taKeyText.setText(sbTemp.toString());
lblMessage.setText("Generated random key of " + iPlainLength + " characters.");
}
else
{
taKeyText.setText("");
lblMessage.setText("Need some plaintext to generate a key.");
}
return true;
}

if (e.target == taPlainText)
{
if (e.id != Event.GOT_FOCUS)
{
iLength = taPlainText.getText().length();
lblMessage.setText("Plain text is " + iLength + " characters long.");
}
}

if (e.target == taKeyText)
{
if (e.id != Event.GOT_FOCUS)
{
iLength = taKeyText.getText().length();
lblMessage.setText("Key text is " + iLength + " characters long.");
}
}
return false;
}

private void encrypt()
{
int i = 0;
int iPlainLength = 0;
int iKeyLength = 0;
int iKeyTextLength = 0;
int iDelta = 0;
int iTemp = 0;
char cTemp = 0;
String sTemp = null;
String sPlainText = null;
String sKeyText = null;
String sCryptText = null;
StringBuffer sbTemp = null;
int iKey[] = null;
int iKeyBuffer[] = null;
boolean bNumericKey = false;

sPlainText = taPlainText.getText();
sKeyText = taKeyText.getText();

iPlainLength = sPlainText.length();
iKeyTextLength = sKeyText.length();
System.out.println("\n");

// Find out if the key is numeric:
if (numericP(iKeyTextLength, sKeyText))
{
bNumericKey = true;
System.out.println("The key is numeric.");

// Fill the key array:
iKey = new int[iKeyTextLength];

while (i < iKeyTextLength)
{
cTemp = 0;
sbTemp = new StringBuffer();
while (cTemp != ' ' && i < iKeyTextLength)
{
try
{
cTemp = sKeyText.charAt(i);
}
catch (StringIndexOutOfBoundsException e1)
{
System.out.println("Caught StringIndexOutOfBoundsException in decrypt() (key).");
}
if (cTemp != ' ')
{
sbTemp.append(cTemp);
}
i++;
}
try
{
iTemp = Integer.parseInt(sbTemp.toString());
}
catch (NumberFormatException e2)
{
System.out.println("Caught NumberFormatException in decrypt() (key).");
break;
}
iKey[iKeyLength] = iTemp;
iKeyLength++;
}
}
else
{
System.out.println("The key is not numeric.");
iKeyLength = iKeyTextLength;
}

if (iPlainLength < iKeyLength)
{
// Pad out the crypttext.
System.out.println("Key is longer than plaintext: padding plaintext with spaces.");
lblMessage.setText("Key is longer than plaintext: padding plaintext with spaces.");
sbTemp = new StringBuffer();
iDelta = iKeyLength - iPlainLength;
for (i = 0; i < iDelta; i++)
{
sbTemp.append(" ");
}
sPlainText = sPlainText + sbTemp.toString();
taPlainText.setText(sPlainText);
iPlainLength = sPlainText.length();
}
else
{
if (iKeyLength < iPlainLength)
{
// Repeat the key as necessary to make it long enough.
System.out.println("Key is shorter than plaintext: repeating the key.");
lblMessage.setText("Key is shorter than plaintext: repeating the key.");

if (bNumericKey)
{
iKeyBuffer = new int[iKeyLength]; // Need to stash the key characters someplace
for (i = 0; i < iKeyLength; i++) // while we get more memory for the array.
{
iKeyBuffer[i] = iKey[i];
}
iKey = new int[iPlainLength]; // Expand the key array.
for (i = 0; i < iKeyLength; i++)
{
iKey[i] = iKeyBuffer[i];
}
for (i = iKeyLength; i < iPlainLength; i++)
{
iKey[i] = iKey[i - iKeyLength];
}
}
else
{
// The key is printable text:
sbTemp = new StringBuffer(sKeyText);
for (i = iKeyLength; i < iPlainLength; i++)
{
sbTemp.append(sbTemp.charAt(i - iKeyLength));
}
sKeyText = sbTemp.toString();
iKeyLength = sKeyText.length();
}
}
else
{
// They are equal in length, do nothing.
}
}
// Begin the encryption.
sbTemp = new StringBuffer();
for (i = 0; i < iPlainLength; i++)
{
if (bNumericKey)
{
iTemp = sPlainText.charAt(i) ^ iKey[i]; // XOR encryption
}
else
{
iTemp = sPlainText.charAt(i) ^ sKeyText.charAt(i); // XOR encryption
}
sbTemp.append((char) iTemp);
}
sTemp = sbTemp.toString();
if (unprintableP(iPlainLength, sTemp))
{
System.out.println("Unprintable encrypted text, writing " + iPlainLength + " characters as numeric code.");
lblMessage.setText("Unprintable encrypted text, writing " + iPlainLength + " characters as numeric code.");
sbTemp = new StringBuffer();
for (i = 0; i < iPlainLength; i++)
{
iTemp = sTemp.charAt(i);

if (i == iPlainLength - 1)
{
sbTemp.append(iTemp);
}
else
{
sbTemp.append(iTemp + " ");
}
}
taCryptText.setText(sbTemp.toString());
}
else
{
System.out.println("Printable encrypted text, writing " + iPlainLength + " characters.");
lblMessage.setText("Printable encrypted text, writing " + iPlainLength + " characters.");
taCryptText.setText(sTemp);
}
} // End of function encrypt().

boolean numericP(int iTextLength, String sText)
{
boolean r = true;
int i = 0;
char cTemp = 0;
int iRunLength = 0;
int iMaxRunLength = 0;

for (i = 0; i < iTextLength; i++)
{
try
{
cTemp = sText.charAt(i);
}
catch (StringIndexOutOfBoundsException e7)
{
System.out.println("Caught StringIndexOutOfBoundsException in decrypt() (e7).");
}
if (cTemp != ' ')
{
if (cTemp <> 57)
{
r = false;
break; // Break out of for loop.
}
else
{
if (cTemp > 47 && cTemp < 58)
{
// We have a decimal digit.
iRunLength++;
}
}
}
else
{
// We have a space: // Detect delimiting spaces
if (iRunLength > iMaxRunLength)
{
iMaxRunLength = iRunLength;
}
iRunLength = 0;
}
} // End of for.

if (iRunLength > iMaxRunLength) // Catch the case where the space
{ // comes at the end of the string.
iMaxRunLength = iRunLength;
}

if (iMaxRunLength > 3) // It's not text that happens to to be
{ // a string (or strings) of digits.
r = false;
}

return r;
}
boolean numericKeyP(int iKeyTextLength, String sKeyText)
{
boolean r = true;
int i = 0;
char cTemp = 0;

for (i = 0; i < iKeyTextLength; i++)
{
cTemp = sKeyText.charAt(i);
if (cTemp != ' ')
{
if (cTemp <> 57)
{
r = false;
break; // Break out of for loop.
}
}
}

return r;
}

boolean unprintableP(int iLength, String sText)
{
boolean r = false;
int i = 0;
char cTemp = 0;

for (i = 0; i < iLength; i++)
{
cTemp = sText.charAt(i);
if (cTemp < 32 || cTemp == 127)
{
if (cTemp != 9 && cTemp != 10 && cTemp != 12 && cTemp != 13)
{
r = true;
break; // Break out of for loop.
}
}
}

return r;
}

} // End of Applet class Encrypt





Monday, February 22, 2010

Pentingnya Validasi Penginputan Data pada Form....

Setelah mencoba sekian banyak website yang memiliki login page, akhirnya ketemu juga website dengan login yang berpotensi error.
Nih
Screenshoot nya :




url : http://sat.maranatha.edu/Login.aspx?ReturnUrl=%2fDefault.aspx

Error ini terjadi ketika saya memasukkan data berikut :
user login : bschjbdshcbhjdfuieuhr8uy4389yrhuiewbfjkdcbhsgfyg4grifjewpok,mc';

password : boleh diisi boleh tidak

Mengapa hal ini bisa terjadi???
Sebenarnya ini disebabkan lemahnya penerapan validasi input data pada suatu form. Mungkin hal yang tidak sulit bagi seorang programmer untuk melakukan validasi, tetapi sering terabaikan...

23509316
Muhammad Zuhri


Monday, February 15, 2010

Single Vendor atau Multi Vendor ???

Pertumbuhan teknologi informasi yang semakin pesat, ternyata diikuti juga dengan pertumbuhan penyedia jasa (vendor) teknologi informasi, baik penyedia jasa software maupun hardware. Ini merupakan hal yang positive, kita sebagai user disuguhi berbagai macam pilihan teknologi dengan segala kelebihan dan kekurangannya.

Bagi user perorangan, hal ini tidak terlalu menjadi masalah, karena kita dapat dengan mudah menentukan pilihan, teknologi mana yang paling cocok untuk kita, baik dari segi biaya maupun teknologi yang ditawarkan. Namun bagi perusahaan, pemilihan vendor merupakan hal yang penting dan memerlukan perhatian yang serius karena hal ini akan menentukan kinerja perusahaan. Perusahaan harus memetakan kebutuhan teknologi di perusahaannya dan menentukan teknologi apa yang akan dipakai dan dari mana teknologi itu didapat. Tidak hanya itu, perusahaan jug harus menentukan apakah teknologi yang akan digunakan berasal dari satu vendor atau gabungan dari beberapa vendor. Kali ini saya akan mencoba memberikan pandangan saya mengenai, mana yang lebih baik bagi perusahaan, single vendor atau multi vendor.

Beberapa hal yang menjadi pertimbangan perusahaan dalam menentukan penggunaan multi vendor atau single vendor diantaranya : biaya, keamanan, dan kesesuaian / kemudahaan penerapannya.

Penerapan multi vendor berarti perusahaan harus bekerjasama dengan beberapa perusahaan lain sebagai penyedia jasa teknologi, sehingga biaya yang diperlukan lebih tinggi dibanding dengan penerapan single vendor yang hanya bekerjasama dengan satu perusahaan penyedia jasa teknologi. Selain biaya untuk mendapatkannya lebih tinggi, dengan penerapan multi vendor juga akan menimbulkan biaya lain yang tidak akan timbul jika menerapkan single vendor, yaitu biaya sinkronisasi teknologi / system. perusahaan pasti menginginkan sebuah teknologi atau system yang terpadu, sehingga mau tidak mau perusahaan harus melakukan sinkronisasi system yang berjalan di perusahaan. Hal ini tentu memerlukan biaya yang tidak sedikit. Biaya perawatan system juga akan lebih besar karma melibatkan lebih banyak perusahaan. Sehingga dari segi biaya, single vendor lebih menguntungkan dibanding multi vendor.

Multi vendor berarti banyak perusahaan, hal ini dapat diasumsi lebih banyak orang yang terlibat dalam penerapan teknologi / system di suatu perusahaan. Dari segi keamanan, semakin banyak orang yang terlibat maka risiko keamanan akan semakin tingi. Hal tersebut dapat diartikan ancaman dari dalam atau dari orang yang terlibat di dalam system akan meningkat. Sebaliknya, ancaman dari luar akan berkurang, karena dengan penerapan multi vendor tentu akan lebih banyak “pintu” yang harus dilewati untuk masuk ke dalam system secara keseluruhan. Sehingga system akan lebih sulit ditembus. Keamanan juga berkaitan dengan kelangsungan system, jika menggunakan single vendor berarti kita bergantung dengan satu perusahaan. Jika terjadi sesuatu yang buruk terhadap vendor yang kita gunakan, maka kelangsungan system kita juga akan terancam. Dari segi keamanan, penggunaan multi vendor atau single vendor sama – sama memiliki kelebihan dan kekurangan. Sehingga dalam hal keaman, penentuan single vendor atau multi vendor sangat bergantung pada kebutuhan perusahaan.

Setiap system pasti memiliki kelebihan dan kekurangan. pendekan multi vendor berarti perusahaan dapat mengkombinasikan berbagai keunggulan teknologi dari banyak vendor.. Sementar dengan single vendor, perusahan mengikuti teknologi atau system yang disediakan oleh suatu vendor. Dengan kata lain, jika menggunakan single vendor, perusahaan tidak hanya mendapatkan keunggulan yang di miliki oleh vendor tertentu, tetapi juga harus menerima kekurangan yang dimiliki oleh vendor tersebut.dengan demikian, untuk perusahaan dengan tingkat kompleksitas yang tinggi tentu akan lebih menguntungkan jika menggunakan multi vendor, namun bagi perusahaan dengan tingkat kompleksitas rendah, akan lebih menguntungkan jika menggunakan single vendor.

Dari uraian di atas dapat disimpulkan bahwa penggunaan single vendor lebih menguntungkan dari segi biaya, namun dari segi keamanan dan kesesuai kebutuhan system, tidak dapat ditentukan mana yang lebih baik, karma bergantung pada kebutuhan dan keadaan perusahaan yang akan menggunakannya.


23509316

Muhammad Zuhri Infusi