Beranda > Computer > Encrypt dan Decrypt dengan Metode XOR

Encrypt dan Decrypt dengan Metode XOR

public class CEncrypt {
   
    static final String key = "Encrypt";
   
    private static String encryptString(String str)
   {
      StringBuffer sb = new StringBuffer (str);

      int lenStr = str.length();
      int lenKey = key.length();
     
      //
      // For each character in our string, encrypt it…
      for ( int i = 0, j = 0; i < lenStr; i++, j++ )
      {
         if ( j >= lenKey ) j = 0;  // Wrap 'round to beginning of key string.

         //
         // XOR the chars together. Must cast back to char to avoid compile error.
         //
         sb.setCharAt(i, (char)(str.charAt(i) ^ key.charAt(j)));
      }

      return sb.toString();
    }
   
    private static String decryptString(String str)
   {
      //
      // To 'decrypt' the string, simply apply the same technique.
      return encryptString(str);
   }
   
    /** Creates a new instance of CEncrypt */
    public CEncrypt() {
    }
   
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        String s1 = "password";

      String s2 = encryptString(s1);

      String s3 = decryptString(s2);

      System.out.println("Original string:  " + s1);

      System.out.println("Encrypted string: " + s2);

      System.out.println("Decrypted string: " + s3);
    }
   
}

Iklan
Kategori:Computer
  1. Februari 22, 2007 pukul 8:33 am

    this one post very good. i enjoy very much!

  1. No trackbacks yet.

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: