Ron Code 5 RC5 [java]


berdasarkan link berikut https://community.oracle.com/thread/2606350?start=0&tstart=0 kita dapat memanfaatkan script java untuk kriptografi block RC5 sebagai berikut:

script berikut memiliki panjang ukuran block 64 bit, round sebanyak 12 kali dan panjang kunci 128 bit

package roncode5;

/*************************RC5 2 bytes Encryption Decryption**********************************/  
 
class Main_RC5{  
  public static final int w = 32;  //ukuran blok = 64
  public static final int r = 12;  //jumlah putaran
  public static final int b = 16;  //panjang kunci external 16 bytes = 128 bit
  public static final int c = 4;  
  public static final int t = 26;  
  
  public int[] S = new int[t]; // expanded key table  
  public int P = (int)0xb7e15163; // magic constants  
  public int Q = (int)0x9e3779b9;  
  
  void RC5_ENCRYPT(int[] pt, int[] ct){ // 2 WORD input pt/output ct  
  int i;  
  int A = pt[0] + S[0];  
  int B = pt[1] + S[1];  
  for (i = 1; i <= r; i++){  
  A = (((A ^ B) << (B & (w - 1))) | ((A ^ B)>>>(w - (B & (w - 1))))) + S[2 * i];  
  B = (((B ^ A) << (A & (w - 1))) | ((B ^ A)>>>(w - (A & (w - 1))))) + S[2 * i + 1];  
  }  
  ct[0] = A;  
  ct[1] = B;  
  }  
  
  
  void RC5_DECRYPT(int[] ct, int[] pt){ // 2 WORD input ct/output pt  
  int i;  
  int B = ct[1];  
  int A = ct[0];  
  for (i = r; i > 0; i--){  
  B = (((B - S[2 * i + 1])>>>(A & (w - 1))) | ((B - S[2 * i + 1]) << (w - (A & (w - 1))))) ^ A;  
  A = (((A - S[2 * i])>>>(B & (w - 1))) | ((A - S[2 * i]) << (w - (B & (w - 1))))) ^ B;  
  }  
  pt[1] = B - S[1];  
  pt[0] = A - S[0];  
  }  
  
  
  void RC5_SETUP(byte[] K){ // secret input key K[0...b-1]  
  int i;  
  int j;  
  int k;  
  int u = w / 8;  
  int A;  
  int B;  
  int[] L = new int[c];  
  /* Initialize L, then S, then mix key into S */  
  for (i = b - 1,L[c - 1] = 0; i != -1; i--){  
  L[i / u] = (L[i / u] << 8) + K[i];  
  }  
  for (S[0] = P,i = 1; i < t; i++) {  
  S[i] = S[i - 1] + Q;  
  }  
  for (A = B = i = j = k = 0; k < 3 * t; k++,i = (i + 1) % t,j = (j + 1) % c) {// 3*t > 3*c  
  A = S[i] = (((S[i] + (A + B)) << (3 & (w - 1))) | ((S[i] + (A + B))>>>(w - (3 & (w - 1)))));  
  B = L[j] = (((L[j] + (A + B)) << ((A + B) & (w - 1))) | ((L[j] + (A + B))>>>(w - ((A + B) & (w - 1)))));  
  }  
  }  
  
  public static String hex(int n) {  
     // call toUpperCase() if that's required  
     //return String.format("0x%8s", Integer.toHexString(n)).replace(' ', '0');  
   return String.format("0x%s", Integer.toHexString(n)).replace(' ', '0');  
  }  
  
  
  public static void main(String[] args){  
  int i;  
  int j;  
  int[] pt1 = new int[2];  
  int[] pt2 = new int[2];  
  int[] ct = {9,7};  
  byte[] key = new byte[b];  
  
  //time_t t0, t1;  
  Main_RC5 h=new  Main_RC5();  
  
  System.out.print("RC5-32/12/16 examples:\n");  
  for (i = 1;i < 6;i++){ // Initialize pt1 and key pseudorandomly based on previous ct  
   pt1[0] = ct[0];  
   pt1[1] = ct[1];  
  
  
   for (j = 0;j < b;j++){  
   key[j] = (byte) (ct[0] % (255 - j));  
   }  
   /* Setup, encrypt, and decrypt */  
   h.RC5_SETUP(key);  
   h.RC5_ENCRYPT(pt1, ct);  
   h.RC5_DECRYPT(ct, pt2);  
  
  
   /* Print out results, checking for decryption failure */  
   System.out.printf("\n%d. key = ",i);  
   for (j = 0; j < b; j++){  
   System.out.print((key[j])+" ");  
   }  
   System.out.println("\n   Plaintext:"  + pt1[0]  + pt1[1] +   " --->" +  "ciphertext:" + ct[0]+ct[1]);  
   System.out.println("   Decypted:"  + pt2[0]  + pt2[1] );  
  
  
   if (pt1[0] != pt2[0] || pt1[1] != pt2[1]){  
   System.out.print("Decryption Error!");  
   }  
  }  
  }  
}  


Advertisements

Author: ridhobustami

orang ngak jelas

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s