Social Icons

Cara mendapatkan bitcoin gratis

Thursday, 20 October 2016

C Source Code Example for Ceaser Cipher

Example Program

#include <stdio.h>
#include <ctype.h>

#define MAXSIZE 1024

void encrypt(char*);
void decrypt(char*);

int menu();

int
main(void)
{

char c,
     choice[2],
     s[MAXSIZE];

 while(1)
 {
 menu();

 gets(choice);

 if((choice[0]=='e')||(choice[0]=='E'))
 {
  puts("Input text to encrypt->");
  gets(s);
  encrypt(s);
 }
 else if((choice[0]=='d')||(choice[0]=='D'))
 {
  puts("Input text to decrypt->");
  gets(s);
  decrypt(s);
 }
 else
    break;
 }

 return 0;
}

void encrypt(char*str)
{
 int n=0;
 char *p=str,
   q[MAXSIZE];

 while(*p)
 {
  if(islower(*p))
  {
   if((*p>='a')&&(*p<'x'))
    q[n]=toupper(*p + (char)3);
   else if(*p=='x')
    q[n]='A';
   else if(*p=='y')
    q[n]='B';
   else
    q[n]='C';
  }
  else
  {
   q[n]=*p;
  }
  n++; p++;
 }
 q[n++]='\0';
 puts(q);
}

void decrypt(char*str)
{
 int   n=0;
 char *p=str,
   q[MAXSIZE];

 while(*p)
 {
  if(isupper(*p))
  {
   if((*p>='D')&&(*p<='Z'))
    q[n]=tolower(*p - (char)3);
   else if(*p=='A')
    q[n]='x';
   else if(*p=='B')
    q[n]='y';
   else
    q[n]='z';
  }
  else
  {
   q[n]=*p;
  }
  n++; p++;
 }
 q[n++]='\0';
 puts(q);
}

int menu()
{
 puts("To encrypt, input e or E\n");
 puts("To decrypt, input d or D\n");
 puts("To exit, input any other letter\n");
 puts("Your choice:->\n");
 return 0;
}

Code Analysis

The main function does the following:
  • First we include the stdio.h and ctype.h
  • Then we create a macro for maximum sentence size. In this example, it is 1024.
  • There are a few declarations to reserve place for things that we use in our code.
  • While loop will repeat until user inputs proper letter to stop the program.
  • In the while loop, we call the function menu(), which will display the menu to the user.
Next, it does the following:
  • When you input the letter, function gets() reads your choice. According to the user input appropriate function would be called.
  • One function encrypts the text, and the other function decrypts it.
  • First function gets one string into it, and modifies it. After that, we are changing each letter according to the rule we need to apply.
  • The pointer q is a helper to read the original string, and the q is used to store the output.
  • tolower() will transform the letter into lower case. toupper() will transform the letter into upper case.
  • Function gets() is used to read the input string from user.
Now, to the function encrypt:
  • To encrypt, this code will move letters to a different offset by 3 spaces in ASCII table. Also, at the end of alphabet you wrap around and replace: x, y and z, with: a, b and c.
  • Instead of char type, use wcahr_t symbols that could be good for languages other than English. There are usually similar functions that will work with two byte letters. Sometimes it is enough to use one additional w.
As an additional exercise, modify the above C sample code to include different offsets in one sentence itself.
When we talk about breaking Caesars cipher, first algorithm that could be applied is statistical decryption. For each language, there are usual frequencies of each letter and they could be used to figure out the encrypted text without getting the key. On a related subject, you should also explore how Vigener’s cipher works.
Again, it is very easy to break the encrypted text generated by this example. The above code is given only for learning purpose to understand how this works.
Selengkapnya...

Script Cryptography di SQL

Cryptography is the oldest technique, we were using in the past (before computer born) to secure our communication. Encryption is a methodology to convert the original readable text to unreadable format. Decryption is the reverse engineering of encryption.
In modern world, the cryptography has evolved drastically with mathematics, computer science, and electrical engineering. It help the encryption process more complex. So, It cannot (may be)  broken by the third-party.
In this blog post, I am going to introduce two stored procedures that encrypt the plain text and other procedure decrypt the encrypted text. I am using caesar’s ciper technique. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet -Wikipedia.

Encryption Stored procedure: CCEncrypt
encrypt
— =============================================
— Author:        Ayyappan Thangaraj, SQLServerRider.com
— Create date: 1/22/2013
— Description:    Caesar Encryption algorithm
— =============================================
Create PROCEDURE SP_CCEncrypt
@Ptext as varchar(500)
AS
BEGIN
SET NOCOUNT ON;
declare @SHIFTNO as tinyint = 3
declare @Etext as varchar(500) =”
declare @pc as varchar(1)
Declare @i as smallint = 1
Declare @n as smallint
set @n = len(@Ptext)
set @Ptext = upper(@Ptext)
while @i < = @n
BEGIN
set @pc = SUBSTRING(@Ptext, @i, 1)
if ascii(@pc) between 65 and 90
if ascii(@pc)+@SHIFTNO > 90
set @pc = char((ascii(@pc)+@SHIFTNO)-90+64)
else
set @pc = char((ascii(@pc)+@SHIFTNO))
set @Etext = @Etext + @pc
Set @i = @i + 1
END
select @Etext
END
GO
 
Example:
example1
Decryption Stored Procedure: CCDecrypt
decrypt— =============================================
— Author:        Ayyappan Thangaraj, SQLServerRider.com
— Create date: 1/22/2013
— Description:    Caesar Decryption algorithm
— =============================================
Create PROCEDURE SP_CCDecrypt
@Etext as varchar(500)
AS
BEGIN
SET NOCOUNT ON
declare @SHIFTNO as smallint = -3
declare @Ptext as varchar(500) =”
declare @Ec as varchar(1)
Declare @i as smallint = 1
Declare @n as smallint
set @n = len(@Etext)
set @Etext = upper(@Etext)
while @i < = @n
BEGIN
set @Ec = SUBSTRING(@Etext, @i, 1)
if ascii(@Ec) between 65 and 90
if ascii(@Ec)+@SHIFTNO < 65
set @Ec = char(91-(65-(ascii(@Ec)+@SHIFTNO)))
else
set @Ec = char((ascii(@Ec)+@SHIFTNO))
set @Ptext = @Ptext + @Ec
Set @i = @i + 1
END
select lower(@Ptext)
END
GO
 
Example:
dtext
Selengkapnya...

Contoh Program Kriptografi di C++


Betikur ini adalah contoh sederhana menggunakan kriptografi AES di C++.

Contoh Program :

Source Code C++

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

/*
 * MCrypt API available online:
 * http://linux.die.net/man/3/mcrypt
 */
#include <mcrypt.h>

#include <math.h>
#include <stdint.h>
#include <stdlib.h>

int encrypt(
    void* buffer,
    int buffer_len, /* Because the plaintext could include null bytes*/
    char* IV, 
    char* key,
    int key_len 
){
  MCRYPT td = mcrypt_module_open("rijndael-128", NULL, "cbc", NULL);
  int blocksize = mcrypt_enc_get_block_size(td);
  if( buffer_len % blocksize != 0 ){return 1;}

  mcrypt_generic_init(td, key, key_len, IV);
  mcrypt_generic(td, buffer, buffer_len);
  mcrypt_generic_deinit (td);
  mcrypt_module_close(td);
  
  return 0;
}

int decrypt(
    void* buffer,
    int buffer_len,
    char* IV, 
    char* key,
    int key_len 
){
  MCRYPT td = mcrypt_module_open("rijndael-128", NULL, "cbc", NULL);
  int blocksize = mcrypt_enc_get_block_size(td);
  if( buffer_len % blocksize != 0 ){return 1;}
  
  mcrypt_generic_init(td, key, key_len, IV);
  mdecrypt_generic(td, buffer, buffer_len);
  mcrypt_generic_deinit (td);
  mcrypt_module_close(td);
  
  return 0;
}

void display(char* ciphertext, int len){
  int v;
  for (v=0; v<len; v++){
    printf("%d ", ciphertext[v]);
  }
  printf("\n");
}

int main()
{
  MCRYPT td, td2;
  char * plaintext = "test text 123";
  char* IV = "AAAAAAAAAAAAAAAA";
  char *key = "0123456789abcdef";
  int keysize = 16; /* 128 bits */
  char* buffer;
  int buffer_len = 16;

  buffer = calloc(1, buffer_len);
  strncpy(buffer, plaintext, buffer_len);

  printf("==C==\n");
  printf("plain:   %s\n", plaintext);
  encrypt(buffer, buffer_len, IV, key, keysize); 
  printf("cipher:  "); display(buffer , buffer_len);
  decrypt(buffer, buffer_len, IV, key, keysize);
  printf("decrypt: %s\n", buffer);
  
  return 0;
}

Selengkapnya...

Contoh Program Kriptografi di Java

Contoh Program


Betikur ini adalah contoh sederhana menggunakan kriptografi AES di Java. Contoh Program


Source Code Java

import java.security.MessageDigest;
import java.util.Arrays;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.spec.IvParameterSpec;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class AES {
  static String IV = "AAAAAAAAAAAAAAAA";
  static String plaintext = "test text 123\0\0\0"; /*Note null padding*/
  static String encryptionKey = "0123456789abcdef";
  public static void main(String [] args) {
    try {
      
      System.out.println("==Java==");
      System.out.println("plain:   " + plaintext);

      byte[] cipher = encrypt(plaintext, encryptionKey);

      System.out.print("cipher:  ");
      for (int i=0; i<cipher.length; i++)
        System.out.print(new Integer(cipher[i])+" ");
      System.out.println("");

      String decrypted = decrypt(cipher, encryptionKey);

      System.out.println("decrypt: " + decrypted);

    } catch (Exception e) {
      e.printStackTrace();
    } 
  }

  public static byte[] encrypt(String plainText, String encryptionKey) throws Exception {
    Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding", "SunJCE");
    SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES");
    cipher.init(Cipher.ENCRYPT_MODE, key,new IvParameterSpec(IV.getBytes("UTF-8")));
    return cipher.doFinal(plainText.getBytes("UTF-8"));
  }

  public static String decrypt(byte[] cipherText, String encryptionKey) throws Exception{
    Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding", "SunJCE");
    SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES");
    cipher.init(Cipher.DECRYPT_MODE, key,new IvParameterSpec(IV.getBytes("UTF-8")));
    return new String(cipher.doFinal(cipherText),"UTF-8");
  }
}




Selengkapnya...

jago Photoshop