Tugas Keamanan Jaringan Algoritma Kriptografi

Kemaren ditugasin bikin program buat algoritma kriptografi. Sebenernya ditugasin cuma milih satu. Tapi gw bikin semuanya aja.

* Horizontal – Horizontal

* Horizontal – Vertikal

* Horizontal – Diagonal

* Horizontal – Spiral

* Zigzag – HOrizontal

* Piramid – Vertikal  

 

#include <stdio.h>
#include <string.h>

/*
** author : Deni Zakya
** Dilarang merubah atau tidak mencantumkan komentar ini. Diperbolehkan menggunakan
** atau mempelajari algoritma dari source code ini. Mengutip sebagian atau seluruh
** source code ini diperbolehkan selama mencantumkan nama asli pembuatnya. Segala
** bentuk program yang memuat source code ini harus membuka source codenya untuk
** dibaca siapa saja. Sama seperti lisensi source code ini. Happy Coding….
*/
char alfabet[] = {//pembatasan karakter 26 alfabet saja
     ‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘G’, ‘H’, ‘I’, ‘J’, ‘K’, ‘L’, ‘M’, ‘N’, ‘O’,
     ‘P’, ‘Q’, ‘R’, ‘S’, ‘T’, ‘U’, ‘V’, ‘W’, ‘X’, ‘Y’, ‘Z’
     };
    
char pesan [] = “\n\nauthor : Deni Zakya\n\
Dilarang merubah atau tidak mencantumkan komentar ini. Diperbolehkan menggunakan\
atau mempelajari algoritma dari source code ini. Mengutip sebagian atau seluruh\
source code ini diperbolehkan selama mencantumkan nama asli pembuatnya. Segala\
 bentuk program yang memuat source code ini harus membuka source codenya untuk\
 dibaca siapa saja. Sama seperti lisensi source code ini. Happy Coding….\n\n\n”;    

int flag[26];//flag untuk karakter

char *keygen(char key[]);//fungsi pembangkit cipher key
char *encrypt(char plain[], char key[]);//fungsi enkripsi dengan cipher key tertentu
char *HVer(char key[]);//transposisi Horizontal Vertikal
char *HSpir(char key[]);//transposisi Horizontal Spiral
char *HDiag(char key[]);//transposisi Horizontal Diagonal
char *ZigH(char key[]);//transposisi Horizontal Zig-Zag
char *PirV(char key[]);//transposisi Piramid Vertikal

main()
{
    char plain[255];
    char *temp;
    char cipher[255];
    char key[27], tkey[27];
   
    puts(“Masukkan key : “);
    gets(key);//input key yang digunakan
    puts(“Masukkan plain text : “);
    gets(plain);//input string yang akan dienkrip
   
    strupr(key);//konversi huruf ke bentuk kapital
    strupr(plain);//konversi huruf ke bentuk kapital
   
    keygen(key);
    printf(“\nHorizontal-Horizontal\n\”%s\”\n”, plain);
    printf(“Plain Text  : %s\n”, alfabet);
    printf(“Cipher Text : %s\n”, key);
   
    strcpy(cipher, plain);
    encrypt(cipher, key);//process enkripsi
       
   
    puts(“Hasil : “);
    puts(cipher);//output hasil enkripsi
   
    printf(“\nHorizontal-Vertikal\n\”%s\”\n”, plain);
    strcpy(tkey, key);
    temp = tkey;
    HVer(temp);//merubah key ke fungsi transposisi Horizontal Vertikal
   
    printf(“Plain Text  : %s\n”, alfabet);
    printf(“Cipher Text : %s\n”, tkey);
   
   
    strcpy(cipher, plain);
    encrypt(cipher, tkey);//process enkripsi
   
    puts(“Hasil : “);
    puts(cipher);//output hasil enkripsi
   
    printf(“\nHorizontal-Spiral\n\”%s\”\n”, plain);
    strcpy(tkey, key);
    temp = tkey;
    HSpir(temp);//merubah key ke fungsi transposisi Horizontal Spiral
   
    printf(“Plain Text  : %s\n”, alfabet);
    printf(“Cipher Text : %s\n”, tkey);
   
   
    strcpy(cipher, plain);
    encrypt(cipher, tkey);//process enkripsi
   
    puts(“Hasil : “);
    puts(cipher);//output hasil enkripsi
   
    printf(“\nHorizontal-Diagonal\n\”%s\”\n”, plain);
    strcpy(tkey, key);
    temp = tkey;
    HDiag(temp);//merubah key ke fungsi transposisi Horizontal Diagonal
   
    printf(“Plain Text  : %s\n”, alfabet);
    printf(“Cipher Text : %s\n”, tkey);
   
   
    strcpy(cipher, plain);
    encrypt(cipher, tkey);//process enkripsi
   
    puts(“Hasil : “);
    puts(cipher);//output hasil enkripsi
   
    printf(“\nHorizontal-ZigZag\n\”%s\”\n”, plain);
    strcpy(tkey, key);
    temp = tkey;
    ZigH(temp);//merubah key ke fungsi transposisi Horizontal ZigZag
   
    printf(“Plain Text  : %s\n”, alfabet);
    printf(“Cipher Text : %s\n”, tkey);
   
   
    strcpy(cipher, plain);
    encrypt(cipher, tkey);//process enkripsi
   
    puts(“Hasil : “);
    puts(cipher);//output hasil enkripsi
   
    printf(“\nPiramid-Vertikal\n\”%s\”\n”, plain);
    strcpy(tkey, key);
    temp = tkey;
    PirV(temp);//merubah key ke fungsi transposisi Piramid Vertikal
   
    printf(“Plain Text  : %s\n”, alfabet);
    printf(“Cipher Text : %s\n”, tkey);
   
   
    strcpy(cipher, plain);
    encrypt(cipher, tkey);//process enkripsi
   
    puts(“Hasil : “);
    puts(cipher);//output hasil enkripsi
   
    puts(pesan);
   
    getchar();
    }

char *keygen(char key[])//fungsi untuk membuat key proses enkripsi/dekripsi
{
    int len;
   
    len = strlen(key);//hitung panjang key awal
   
    int idx_a, idx_b;
   
    for(idx_a = 0;idx_a < len;idx_a++)//pembuangan duplikasi dan spasi
    {
              idx_b = idx_a + 1;
             
              while(idx_b < len)
              {
                          if(key[idx_a] == key[idx_b] || key[idx_b] == ‘ ‘)
                          {
                                        int idx_temp = idx_b;
                                       
                                        while(idx_temp < len)
                                        {
                                                     key[idx_temp] = key[idx_temp + 1];
                                                     idx_temp++;                                             
                                                     } 
                                        len–;
                          }
                          else
                          {
                              idx_b++;
                              }
                                                                                           
              }
             
                 
    }
        
    int temp = 0;
    while(temp < 26)
    {
               flag[temp] = 0;
               temp++;
               }
        
    for(idx_a = idx_b = 0;idx_a < 26;idx_a++)//penambahan karakter yang belum terpakai
    {
              if(idx_a < len)
              {
                       while(idx_b < 26)
                       {
                                   if(key[idx_a] == alfabet[idx_b] && flag[idx_b] == 0)
                                   {
                                                 flag[idx_b] = 1;
                                                 break;                                     
                                   }
                                   idx_b++;
                       }
              }
             
              if(idx_a >= len)
              {
                       while(idx_b < 26)
                       {
                                   if(flag[idx_b] == 0)
                                   {
                                            key[idx_a] = alfabet[idx_b];
                                            flag[idx_b] = 1;
                                            break;
                                   }
                                   idx_b++;
                       }
              }
              idx_b = 0;
    }
       
    key[idx_a] = ”;
        
    return key;
   
}

char *encrypt(char plain[], char key[])//fungsi enkripsi menggunakan key yang sudah dibuat
{
     int len = strlen(plain);
     int i,j;
     for(i=0;i<len;i++)
     {
             for(j=0;j<26;j++)
             {
                     if(plain[i] == alfabet[j])
                     {
                                 plain[i] = key[j];
                                 break;
                                 }                    
                     }
            
             }
    
    
     return plain;
}

char *HVer(char key[])//merubah key dengan fungsi transposisi Horizontal Vertikal
{
     char temp[6][5];//matriks sementara
     int len = strlen(key);
     int i, j, k;
        
     for(i=k=0;i<6;i++)//loop untuk memindahkan keyy ke matriks
     {
                 for(j=0;j<5;j++)
                 {
                                 temp[i][j] = ”;
                                 temp[i][j] = key[k++];
                                
                                 if(k>=len)
                                           break;
                 }
                 if(k>=len)
                           break;
     }
   
     for(j = k = 0;j<5;j++)//proses loop untuk merubah key
     {
           for(i = 0;i<6;i++)
           {
                
                 if(temp[i][j] != ”)
                 {
                               if(i != 5 || j == 0)
                               key[k++] = temp[i][j];
                 }
           }
     }
    
     key[k] = ”;
    
     return key;
}

char *HSpir(char key[])//membuat key untuk spiral
{
     int i,j,k;
     char temp[6][5];
     int len = strlen(key);
    
    
     for(i=k=0;i<6;i++)//loop untuk memindahkan key ke matriks
     {
                 for(j=0;j<5;j++)
                 {
                                 temp[i][j] = key[k++];
                                 if(k==len)
                                           break;
                 }
                 if(k==len)
                           break;
     }
    
     i = j = k = 0;
     i = 5;
     j = 0;
     int ni = 6, nj = 5;
    
     while(k<len)//proses yang melelahkan disini. pergerakan spiral
     {
                 for(;i>=0;i–)
                 {
                               key[k++] = temp[i][j];
                               }
                               i++;
                               j++;
                 for(;j<=4;j++)
                 {
                               key[k++] = temp[i][j];
                               }
                               j–;
                               i++;
                 for(;i<=4;i++)
                 {
                               key[k++] = temp[i][j];
                              
                               }
                               i–;
                               j–;
                 for(;j>=1;j–)
                 {
                               key[k++] = temp[i][j];
                              
                               }
                               j++;
                               i–;
                 for(;i>=1;i–)
                 {
                               key[k++] = temp[i][j];
                              
                               }
                               i++;
                               j++;
                 for(;j<=3;j++)
                 {
                               key[k++] = temp[i][j];
                              
                               }
                               j–;
                               i++;
                 for(;i<=3;i++)
                 {
                               key[k++] = temp[i][j];
                               }
                               i–;
                               j–;
                 for(;j>=2;j–)
                 {
                               key[k++] = temp[i][j];
                               }
                               j++;
                               i–;
                 for(;i>=2;i–)
                 {
                               key[k++] = temp[i][j];
                               }
                                                                              
     } 
       
     key[len] = ”;
    
     return key;
}

char *HDiag(char key[])
{
     int i,j,k;
     char temp[6][5];
     int len = strlen(key);
     int bar = 6;
     int kol = 5;
    
     for(i=k=0;i<6;i++)//loop untuk memindahkan key ke matriks
     {
                 for(j=0;j<5;j++)
                 {
                                 temp[i][j] = key[k++];
                                 if(k==len)
                                           break;
                 }
                 if(k==len)
                           break;
     }
    
     k = 0;
    
     while(k<len)//proses pemindahan secara diagonal
     {
                 for(i=0;i<bar;i++)
                 {
                                int n=i;
                               
                                for(j=0;j<kol;j++)
                                {
                                               key[k++] = temp[n--][j];
                                               if(n < 0)
                                                    break;                                                                                                                                                                                  
                                }                               
                 }
                 key[k++] = temp[4][2];
                 key[k++] = temp[3][3];
                 key[k++] = temp[2][4];
                 key[k++] = temp[4][3];
                 key[k++] = temp[3][4];
                 key[k++] = temp[4][4];                                
     }
    
     key[len] = ”;
    
     return key;
}

char *ZigH(char key[])
{
     char temp[5][26];
    
     int len = strlen(key);
     int j,k,l;
     l = 0;
    
     for(j=0;j<5;j++)//masukkan karakter untuk mengisi matriks awal
     {
                     for(k=0;k<26;k++)
                     {
                                      temp[j][k] = ‘#’;
                     }
     }
    
     j = 4;
     while(l<len)//lakukan zigzag key pada matriks
     {
                 if(j>=3)
                 {
                         for(;j>=0;j–)
                         {
                                          temp[j][l] = key[l++];
                         }
                         j = 0;
                         j++;
                 }
                 else if(j>0)
                 {
                      for(;j<=4;j++)
                      {
                                       temp[j][l] = key[l++];
                      }
                      j = 4;
                      j–;
                 }
     }
    
     l = 0;
     for(j=0;j<5;j++)//ambil key secara horizontal dari matriks
     {
                     for(k=0;k<26;k++)
                     {
                                      if(temp[j][k] != ‘#’ && temp[j][k] != ”)
                                      {
                                                    key[l++] = temp[j][k];
                                      }
                     }
     }
    
     key[len] = ”;
    
     return key;
}

char *PirV(char key[])
{
     int bar = 6;
     int kol = 10;
     char temp[bar][kol];
     int j,k,l;
     int len = strlen(key);
     l = 0;
    
     for(j=0;j<bar;j++)//masukkan karakter untuk mengisi matriks awal
     {
                     for(k=0;k<kol;k++)
                     {
                                      temp[j][k] = ‘#’;
                     }
     }
    
     for(j=0;j<6;j++)//proses memindahkan key ke matriks secara piramid
     {
                     int k_;
                     k_ = k = (kol/2) – j;
                     for(;k<((j*2)+1)+k_;k++)
                     {
                                        temp[j][k] = key[l++];                     
                                        if(l==len)
                                        {
                                                  break;
                                        }
                                                                                       
                     }
                     if(l==len)
                     {
                               break;
                     }
     }
    
     for(l = j = 0;j<kol;j++)//proses loop untuk merubah key
     {
           for(k = 0;k<bar;k++)
           {
                 if(temp[k][j] != ” && temp[k][j] != ‘#’)
                 {
                               key[l++] = temp[k][j];
                 }
           }
     }
    
     key[len] = ”;
    
     return key;              
}

5 Komentar (+add yours?)

  1. mirza
    Des 05, 2008 @ 09:05:16

    wah, terima kasih banyak source code-nya…
    tp gw blm bisa, ajarin gw dunkz

    #include
    #include

    Balas

  2. mirza
    Des 05, 2008 @ 09:09:09

    tambahan, ntar lagi klo nulis souce code, diapit tag code y… biar jelas yang mana yang code, yang mana yang tulisan… :)

    Balas

  3. denizak
    Des 11, 2008 @ 12:40:18

    nanti gw bakal posting gmana caranya sampe program ini jadi. Dari awal analisis sampe programming…

    Balas

  4. Safar
    Des 22, 2008 @ 19:53:06

    ditunggu lanjutannya

    Balas

  5. noerone
    Mei 02, 2011 @ 00:42:28

    mantabbb… dah,, thk’s atas posting’n…
    cz cukup membantu nh…. :)

    Balas

Tinggalkan Balasan

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 / Ubah )

Twitter picture

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

Facebook photo

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

Connecting to %s

Ikuti

Get every new post delivered to your Inbox.