HanuAncutei.com - ARTA de a conversa!
Haine Dama designer roman

Bine ati venit ca musafir! ( Logare | Inregistrare )

 
Reply to this topicStart new topic
> Stie Cineva C/c++ Bine?
halcyon_lll
mesaj 9 Dec 2003, 11:26 PM
Mesaj #1


Hypnotic
******

Grup: Membri
Mesaje: 1.145
Inscris: 21 November 03
Forumist Nr.: 1.263



Am o rugaminte la cineva care stie sa programeze in c/c++. Stiti cumva cum ar trebui implementat un program care sa cripteze si sa decripteze un text prin substitutie? Sau macar sa-mi explice cum functioneaza criptarea prin substitutie? Daca stiti si ma ajutati, raman dator! smile.gif


--------------------
When we are forgotten, we cease to exist.
Go to the top of the page
 
+Quote Post
E.B.E.
mesaj 10 Dec 2003, 01:14 AM
Mesaj #2


Domnitor
******

Grup: Membri
Mesaje: 1.292
Inscris: 20 November 03
Din: Delft, NL
Forumist Nr.: 1.248



Daca cumva este vorba de substitutia simpla (simple substitution cipher/crypting) atunci problema este simpla, iar algoritmul este urmatorul: fiecare caracter se inlocuieste intotdeauna printr-un (acelasi) alt caracter. Spre exemplu:
a=X
A=;
b=J
B=%
et caetera... Implementarea este foarte simpla: faci o tabela de transformare, daca folosesti un cod aiurea (care nu are formula matematica - daca da, explic mai incolo). Parcurgi stringul de intrare si inlocuiesti fiecare caracter cu corespondentul lui, preluat din tabela de transformare; in cazul in care corespondentul este dat de o formula matematica (spre exemplu, codul rotit cu 13 pozitii - asta este un algoritm clasic numit rot13), pur si simplu aplici formula pe caracter. In ambele cazuri, pui in stringul de iesire caracterul nou.

Decriptarea este la fel de simpla: faci tabela inversa, respectiv implementezi formula inversa.

De notat ca un astfel de cod este relativ simplu de spart, daca ai nevoie de criptare pentru ceva serios, gandeste-te la un alt algoritm.
Iti dau niste linkuri pe care le-am gasit cu un search pe google dupa [substitution crypting] sau [substitution cipher].
http://www.cs.nps.navy.mil/curricula/track...s/chap04_6.html
http://www.ksp.sk/mop/archiv/1998/en-1998-2-zad.html#prikl1

Ca exemplu, pot sa iti dau un cod scris de mine care este similar rot13, si anume roteste caracterele in ASCII cu un anumit deplasament, dar substitutia in acest caz nu este simpla, fiindca nu se roteste fiecare caracter cu o cantitate constanta, ci cu un deplasament dat de codul unuio caracter din parola, si te rotesti pe parola preluand circular caracterele care sunt folosite pe post de coduri de criptare. In acest fel, cineva care nu stie parola nu poate decripta fisierul. "Spargerea" codului este de asemenea de o dificultate mult crescuta. Implementarea este facuta in java, dar algoritmul este relativ simplu de translatat in c/c++.

CODE

import java.io.*;


/**
* A simple password based encoder-decoder. Rotates on the password
* characters using them to rotate each ASCII character of the text.
*/
public final class TextEncoderDecoder {
   
   /** Failure code to use with {@link java.lang.System#exit(int status)}. */
   private static final int FAILURE = 1;
   
   /** The lowermost ASCII character - blank, <code>0x20</code>. */
   private static final int ASCII_BOTTOM = 0x20;
   /** The uppermost ASCII character - <code>0x80</code>. */
   private static final int ASCII_TOP = 0x80;
   /** The number of characters in the ASCII range. */
   private static final int ASCII_SPAN = ASCII_TOP - ASCII_BOTTOM;
   
   
   /**
    * Encodes or decodes a text file.
    * @param encode <code>true</code> for an encoding operation,
    * <code>false</code> for a decoding one.
    * @param srcFile the name of the source file.
    * @param destFile the name of the destination file.
    * @param passwd the coding password.
    */
   private static void encdec(
       boolean encode,
       String srcFile,
       String destFile,
       String passwd) {
       
       Reader reader = null;
       try {
           reader = new BufferedReader(new FileReader(srcFile));
       } catch(FileNotFoundException fnfe) {
           System.out.println("[" + srcFile + "] was not found.\n");
           System.exit(FAILURE);
       } catch(IOException ioe) {
           System.out.println("I/O error while opening [" + srcFile
                   + "] for reading.\n");
           System.exit(FAILURE);
       }

       Writer writer = null;
       try {
           writer = new BufferedWriter(new FileWriter(destFile));
       } catch(IOException ioe) {
           System.out.println("I/O error while opening [" + destFile
                   + "] for writing.\n");
           System.exit(FAILURE);
       }
       
       System.out.println((encode ? "Encoding" : "Decoding")
           + " [" + srcFile + "] to [" + destFile +"].");
       System.out.print("Processing... ");
       
       //the algorythm is self-explanatory, and I shall not spoil it
       //with useless comments :)
       try {
           int passwdIter = 0;
           int c;
           while ((c = reader.read()) != -1) {
               
               //skip non-ascii chars
               if ((c < ASCII_BOTTOM) || (c >= ASCII_TOP)) {
                   writer.write(c);
                   continue;
               }
               
               int rotator = passwd.charAt(passwdIter) - ASCII_BOTTOM;
               passwdIter = (passwdIter + 1) % passwd.length();
               
               c -= ASCII_BOTTOM;
               if (encode)
                   c = (c + rotator) % ASCII_SPAN;
               else
                   c = c >= rotator ? c - rotator : c - rotator + ASCII_SPAN;
               c += ASCII_BOTTOM;
               writer.write(c);
           }
       } catch (IOException ioe)  {
           System.out.println("I/O error while enc/decoding [" + srcFile
                   + "] to [" + destFile + "].\n");
           System.exit(FAILURE);
       }
       
       try {
           reader.close();
           writer.close();
       } catch (IOException ioe) {
           System.out.println("\nI/O error while closing file(s).\n");
           System.exit(FAILURE);
       }
       
       System.out.println("done.\n");
   }
   

   /**
    * Application entry point.
    * @param args the call arguments.
    */
   public static void main(String[] args) {
       if (args.length != 4) {
           usage();
           System.exit(FAILURE);
       }
       
       if (!args[0].equalsIgnoreCase("enc")
           && !args[0].equalsIgnoreCase("dec")) {
           usage();
           System.exit(FAILURE);
       }
       
       encdec(args[0].equalsIgnoreCase("enc"), args[1], args[2], args[3]);
   }
   
   
   /** Displays application usage information to the console. */
   private static void usage() {
       System.out.println("Usage:\njava TextEncoderDecoder enc|dec "
           + "<in-txt-filename> <out-txt-filename> <password>\n");
   }
}


--------------------

I spend my time thinking of Angel... praying she ain't thinking of me...

Go to the top of the page
 
+Quote Post
halcyon_lll
mesaj 10 Dec 2003, 10:55 AM
Mesaj #3


Hypnotic
******

Grup: Membri
Mesaje: 1.145
Inscris: 21 November 03
Forumist Nr.: 1.263



De substitutia simpla am nevoie!! Dar trebuie implementata in c++ si cu pointeri, si eu nu prea imi dau seama cum as putea sa fac asta! cry.gif Mersi mult pentru informatii EBE si o sa mai imi storc capul putin!!! Hmm... ziceai ca: "faci o tabela de transformare, daca folosesti un cod aiurea (care nu are formula matematica - daca da, explic mai incolo). Parcurgi stringul de intrare si inlocuiesti fiecare caracter cu corespondentul lui, preluat din tabela de transformare; in cazul in care corespondentul este dat de o formula matematica (spre exemplu, codul rotit cu 13 pozitii - asta este un algoritm clasic numit rot13), pur si simplu aplici formula pe caracter. In ambele cazuri, pui in stringul de iesire caracterul nou" Faza asta am inteles-o da cum o scriu in c++ ca eu nu am facut programare in C++ in liceu.... De fapt pot zice ca nu am facut nici un fel de programare in liceu, ca nici la Pascal nu stau prea bine! hh.gif


--------------------
When we are forgotten, we cease to exist.
Go to the top of the page
 
+Quote Post
Promo Contextual
mesaj 10 Dec 2003, 10:55 AM
Mesaj #


ContextuALL









Go to the top of the page
 
Quote Post
halcyon_lll
mesaj 10 Dec 2003, 10:57 AM
Mesaj #4


Hypnotic
******

Grup: Membri
Mesaje: 1.145
Inscris: 21 November 03
Forumist Nr.: 1.263



Oau EBE... ce faza tare! Tocmai am vazut ca esti din Cluj obs... pai si eu sunt din Cj tot din obs!!! laugh.gif Daca ma ajuti va fi mult mai usor sa iti dau o bere!!! laugh.gif


--------------------
When we are forgotten, we cease to exist.
Go to the top of the page
 
+Quote Post
E.B.E.
mesaj 10 Dec 2003, 11:02 AM
Mesaj #5


Domnitor
******

Grup: Membri
Mesaje: 1.292
Inscris: 20 November 03
Din: Delft, NL
Forumist Nr.: 1.248



Anu-ntai la ce? biggrin.gif

Mah pe cuvant ca as scrie-o eu, si nu pentru bere doar for the beauty of it, da nu am vreme acuma sad.gif...

Nu vad de ce ar trebui exact C++ ca poate fi facuta extrem de usor si in C... Ma gandesc ca tot asa pe fisiere... In C++ numa daca pui un wrapper peste o clasa de I/O care scrie in/citeste din fluxuri I/O (daca te-am spart cu faza asta, scuze... smile.gif )

Dar daca esti anul intai la ceva nu cred sa trebuiasca asa de complex... Iti ajung fisiere si cam atat... Pointeri... pai pointerii ii folosesti numa unde iti trebuie, nu cred ca ei sunt un scop in sine, desi ar putea fi pentru profu respectiv (ceea ce mie mi se pare cam indoielnic... da ma rog... in scop didactic smile.gif )


--------------------

I spend my time thinking of Angel... praying she ain't thinking of me...

Go to the top of the page
 
+Quote Post
halcyon_lll
mesaj 10 Dec 2003, 11:07 AM
Mesaj #6


Hypnotic
******

Grup: Membri
Mesaje: 1.145
Inscris: 21 November 03
Forumist Nr.: 1.263



E bine si in C si nu neaparat azi. Saptamana asta sa fie pana vineri!!!
Anu intai la automatica.... u?
Si nu trebuie ceva complex!!! Numa sa faca substitutia!
Si m-ai spart la faza aia! rofl.gif


--------------------
When we are forgotten, we cease to exist.
Go to the top of the page
 
+Quote Post
E.B.E.
mesaj 10 Dec 2003, 12:52 PM
Mesaj #7


Domnitor
******

Grup: Membri
Mesaje: 1.292
Inscris: 20 November 03
Din: Delft, NL
Forumist Nr.: 1.248



Io is anu 6 la automatica... Si nu am vreme deloc saptamana asta ca tre sa fac research papers la Isoc asa ca... sorry...

In ce camera stai (in VII banuiesc....)

Edit: ce fel de c? borland?

Acest topic a fost editat de E.B.E.: 10 Dec 2003, 01:17 PM


--------------------

I spend my time thinking of Angel... praying she ain't thinking of me...

Go to the top of the page
 
+Quote Post
halcyon_lll
mesaj 10 Dec 2003, 02:14 PM
Mesaj #8


Hypnotic
******

Grup: Membri
Mesaje: 1.145
Inscris: 21 November 03
Forumist Nr.: 1.263



Nu-i nimik... da' nu stii pe altii care stiu c si care au timp liber?
Anu 6? Si stai in camin??? Da, eu stau in 7 la etajul 4. Tu?
Cred ca acel C in care fereastra e alba iar "include" apare cu verde (ce explicatii demne de lauda rofl.gif )


--------------------
When we are forgotten, we cease to exist.
Go to the top of the page
 
+Quote Post
Zod
mesaj 10 Dec 2003, 03:18 PM
Mesaj #9


Haiduc
**

Grup: Musterii
Mesaje: 81
Inscris: 23 April 03
Forumist Nr.: 225



@halcyon_lll: ai nevoie de algoritmul cu parola sau fara?

Acest topic a fost editat de Zod: 10 Dec 2003, 03:19 PM


--------------------
The future starts from the past.
Go to the top of the page
 
+Quote Post
E.B.E.
mesaj 10 Dec 2003, 03:49 PM
Mesaj #10


Domnitor
******

Grup: Membri
Mesaje: 1.292
Inscris: 20 November 03
Din: Delft, NL
Forumist Nr.: 1.248



Io la 3... si nu stiu pe nimeni care sa aiba chef de C...

Iar culorile la text... cu explicatia asta m=ai spart tu pe mine biggrin.gif


--------------------

I spend my time thinking of Angel... praying she ain't thinking of me...

Go to the top of the page
 
+Quote Post
halcyon_lll
mesaj 10 Dec 2003, 05:14 PM
Mesaj #11


Hypnotic
******

Grup: Membri
Mesaje: 1.145
Inscris: 21 November 03
Forumist Nr.: 1.263



QUOTE (Zod @ Dec 10 2003, 02:28 PM)
@halcyon_lll: ai nevoie de algoritmul cu parola sau fara?

Cum adica cu parola sau fara???
Trebuie sa fac un program care sa imi cripteze si sa imi decripteze un text introdus de la tastatura prin metoda substitutiei. Mai multe nu stiu ce sa va zic! Am inteles ca e destul de simplu, dar eu stau cam prost la capitolul C ....

@EBE: Altcumva nu stiu cum sa-ti explic de care C e vorba. In orice caz interfata nu e aia care arata ca si Pascalul. Adica ecranul nu e albastru ci e alb. (revin iar la culori! laugh.gif )

Altcineva mai stie C???


--------------------
When we are forgotten, we cease to exist.
Go to the top of the page
 
+Quote Post
E.B.E.
mesaj 12 Dec 2003, 12:37 PM
Mesaj #12


Domnitor
******

Grup: Membri
Mesaje: 1.292
Inscris: 20 November 03
Din: Delft, NL
Forumist Nr.: 1.248



In caz ca poate vreodata cineva mai are nevoie de asa ceva, si cum tot e freeware si open-source biggrin.gif, dau si rezolvarea la problema pe care a pus-o halcyon.

rot13.c
CODE
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

const unsigned char
  /* Lowest printable ASCII code - 0x20, BLANK. */
  ASCII_BOTTOM = 0x20,
  /* Highest ASCII code - 0x7F, DEL. */
  ASCII_TOP = 0x7f,
  /* Length of ASCII codes span, equal to ASCII_TOP - ASCII_BOTTOM. */
  ASCII_SPAN = 0x5f,
  /**
   * The offset used in characters substitution.
   * This program implements simple substitution based on characters
   * rotation by a constant offset. Since the chosen code is rot13,
   * the offset is (naturally) equal to 13.
   */
  OFFSET = 13;


/**
* Encodes a string using the rot13 simple substitution cipher.
* Parameters: str - the string that must be encoded.
* Returns: the encoded string.
*/
char *encode(char *str) {

  //the encoded string will hold as many characters as the original
  //one, plus one - which is the C strings terminator, ASCII code 0, NULL
  char *enc = (char *)malloc(strlen(str) + 1);

  //iterate over the characters in the input string
  int i;
  for (i = 0; i < strlen(str); i++) {
      //retrieve the character
      unsigned char c = str[i];
      //encode only printable ASCII characters
      if ( (c >= ASCII_BOTTOM) && (c <= ASCII_TOP) ) {
          //remove the offset from the char code, bringing it into a
          //0-based span of printable ASCII characters - the formula
          //is easier applied in this form
          c -= ASCII_BOTTOM;
          //rotate the character - add the offset and apply modulo
          //in order to rotate to the beginning of the span if
          //the addition exceeded it
          c = (c + OFFSET) % ASCII_SPAN;
          //back to normal range; this way, the output will _always_
          //be a printable ASCII code
          c += ASCII_BOTTOM;
      }
      //put the encoded character in the output string
      enc[i] = c;
  }

  //place the terminator and return
  enc[i] = 0;
  return enc;
}


/**
* Decodes a string encoded by the rot13 simple substitution cipher.
* Parameters: str - the encoded string.
* Returns: the decoded string.
*/
char *decode(char *str) {

  //same scheme as above - as many chars as the input string plus one
  //for the NULL terminator
  char *dec = (char *)malloc(strlen(str) + 1);

  //iterate over the chars in the encoded string
  int i;
  for (i = 0; i < strlen(str); i++) {
      //retrieve the character
      unsigned char c = str[i];
      //remember, we only encoded printable characters, so we need to
      //decode only those as well
      if ( (c >= ASCII_BOTTOM) && (c <= ASCII_TOP) ) {
          //translate to 0-based printable ASCII span
          c -= ASCII_BOTTOM;
          //decode just as above, only this time we subtract the offset
          //and, if the result would be negative, we rotate it into the
          //upper area of the span (by adding ASCII_SPAN)
          c = c >= OFFSET
          ? c - OFFSET : c - OFFSET + ASCII_SPAN;
          //back to normal range
          c += ASCII_BOTTOM;
      }
      //place the decoded character in the output string
      dec[i] = c;
  }

  //place the terminator and return
  dec[i] = 0;
  return dec;
}


//how many characters the strings used throughout the program may contain
const size_t MAX_STRING = 64;

//constant signifying that the program has terminated with success
const int SUCCESS = 0;


int main() {

  //declare and allocate memory for input string, declare pointers
  //towards encoded and decoded strings that will be used later
  char *instr = (char *)malloc(MAX_STRING),
   *encstr, *decstr;

  //retrieve the original string from the user
  printf("\nEnter a text, terminated with CR: ");
  gets(instr);

  //encode the string and display the encoded data
  encstr = encode(instr);
  printf("\nThe encoded text is: %s\n", encstr);

  //decode the encoded string to complete the process; display it
  decstr = decode(encstr);
  printf("\nThe (re)decoded text is: %s\n", decstr);

  //all done - release the memory used by the strings...
  free(instr);
  free(encstr);
  free(decstr);

  //...and terminate with success
  return SUCCESS;
}


--------------------

I spend my time thinking of Angel... praying she ain't thinking of me...

Go to the top of the page
 
+Quote Post
halcyon_lll
mesaj 14 Dec 2003, 06:52 PM
Mesaj #13


Hypnotic
******

Grup: Membri
Mesaje: 1.145
Inscris: 21 November 03
Forumist Nr.: 1.263



Good for you!!!! thumb_yello.gif


--------------------
When we are forgotten, we cease to exist.
Go to the top of the page
 
+Quote Post
E.B.E.
mesaj 15 Dec 2003, 03:29 PM
Mesaj #14


Domnitor
******

Grup: Membri
Mesaje: 1.292
Inscris: 20 November 03
Din: Delft, NL
Forumist Nr.: 1.248



No... totul e clar? Inseamna ca, chiar am facut un program bun, daca un novice intelege tot ce se intampla smile.gif

Nu de alta, da spuneai ca mai ai intrebari.


--------------------

I spend my time thinking of Angel... praying she ain't thinking of me...

Go to the top of the page
 
+Quote Post
halcyon_lll
mesaj 15 Dec 2003, 05:28 PM
Mesaj #15


Hypnotic
******

Grup: Membri
Mesaje: 1.145
Inscris: 21 November 03
Forumist Nr.: 1.263



Pai inca programul e pe han... Inca nu am avut timp de el sorry.gif . Promit ca maine dupa cursuri il pun in C sa vad daca merge si ce anume nu inteleg, fii sigur ca voi intreba! Va trebui sa mai astepti o zi pana sa vina intrebarile mele stupide! rofl.gif


--------------------
When we are forgotten, we cease to exist.
Go to the top of the page
 
+Quote Post
E.B.E.
mesaj 15 Dec 2003, 06:41 PM
Mesaj #16


Domnitor
******

Grup: Membri
Mesaje: 1.292
Inscris: 20 November 03
Din: Delft, NL
Forumist Nr.: 1.248



Mah daca nu mere io il mananc bit cu bit biggrin.gif Si la aia de 1 ma scobesc intre dinti.

Edit: da de fapt la ce materie iti trebe tie asta? Cum era in primu an... TP? Acuma se face in C++? Noi am facut pascal... ce nashpa... Io in locu lor as pune direct Java... Si C++ mere, atata timp cat nu e Microsoft...

Whoops! Reclama nu e permisa pe han... da antireclama? Cred ca am facut-o de oaie...

Edit #2: [deleted]... totusi smile.gif

Acest topic a fost editat de E.B.E.: 15 Dec 2003, 06:43 PM


--------------------

I spend my time thinking of Angel... praying she ain't thinking of me...

Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

 



RSS Versiune Text-Only Data este acum: 19 April 2024 - 01:40 AM
Ceaiuri Medicinale Haine Dama Designer Roman