Encryption

This is a discussion on Encryption within the C++ Programming forums, part of the General Programming Boards category; Are there any tutorials on how to encrypt files in C++ And if so...can some one give me a link. ...

  1. #1
    Registered User quiksilver9531's Avatar
    Join Date
    Nov 2001
    Posts
    36

    Encryption

    Are there any tutorials on how to encrypt files in C++
    And if so...can some one give me a link. Thanks

    -Nick

  2. #2
    Unregistered
    Guest

    Here's the idea

    Have ur data inputted as an array of characters. From there, use a for loop to go through each letter and use a function inside the loop to encrypt it. The same goes for decoding but with the opposite of the encoding function.

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Dont know any tuts, but Applied Cryptography by Bruce Schnider is a good book and has examples in C.......

    You might get it at your local library....whaich is where I found it originally....

  4. #4
    Registered User quiksilver9531's Avatar
    Join Date
    Nov 2001
    Posts
    36
    thanks for the replys already...what would help is if some one could please insert like a sample code so i can try it and see how it works...Thanks in advance

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Funny thing about Applied Chryptography.... I heard the recent edition was to include source code, but but the NSA blocked it..... You can get the disk for $40 (I think) in the USA only......

    There are copies floating around the web though

  6. #6
    Fingerstyle Guitarist taylorguitarman's Avatar
    Join Date
    Aug 2001
    Posts
    564

  7. #7
    Unregistered
    Guest
    This code will encrypt .txt files. Was written on a dos compiler so u might have a few problems in windows.

    #include<string.h>
    #include<stdio.h>
    #include<fstream.h>
    #include<conio.h>
    int main()
    {
    clrscr();
    int choice,num;
    char ifname[20],ofname[20],Key[20],ch;
    cout<<"\t\t\t*-------------------*\n"<<"\t\t\t| 1) Encode. |\n"<<"\t\t\t| 2) Decode. |\n"<<"\t\t\t| 3) Exit. |\n"<<"\t\t\t*-------------------*\n";
    cout<<"\nEnter your choice: ";
    cin>>choice;
    if(choice==3){clrscr();
    cout<<"This program was written by Shahab Faruqi.\n\n";
    cout<<"It is distributed under the GNU General Public License\n";
    cout<<"For more information write to the Free Software Foundation\n";
    cout<<"Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.\n";
    cout<<"\nThank you!!";
    return 0;}
    clrscr();
    cout<<"Enter input filename : ";gets(ifname);fflush(stdin);cout<<"\n";
    int i;
    cout<<"Enter output filename: ";gets(ofname);fflush(stdin);cout<<"\n";
    ifstream infile;
    ofstream outfile;
    infile.open(ifname);
    outfile.open(ofname);
    switch(choice)
    {
    case 1:cout<<"Enter key: ";gets(Key);fflush(stdin);
    num=strlen(Key);
    while(infile)
    {
    i=0;
    while((i<num)&&(infile)){
    infile.get(ch);if(!infile){cout<<"\nThe file was successfully encrypted.";getch();goto end;}
    ch=char(char(ch)+char(Key[i]));//add ASCII values of key
    outfile.put(ch);//characters in the file & copy to new file
    i++;
    }
    //inner while
    }//outer while
    break;
    case 2:cout<<"Enter key: ";gets(Key);fflush(stdin);
    num=strlen(Key);
    while(infile)
    {
    i=0;
    while((i<num)&&(infile)){
    infile.get(ch);if(!infile){cout<<"\nThe file was successfully decrypted.";getch();goto end;}
    ch=char(char(ch)-char(Key[i]));//subtract ASCII values of key
    outfile.put(ch);//characters in the file & copy to new file
    i++;
    }
    //inner while
    }//outer while
    break;
    }//end switch
    end:
    infile.close();
    outfile.close();
    return 0;
    }

  8. #8
    Registered User quiksilver9531's Avatar
    Join Date
    Nov 2001
    Posts
    36
    Thanks that what I was looking for.

  9. #9
    Registered User ski6ski's Avatar
    Join Date
    Aug 2001
    Posts
    133
    I remember in calulus class that you can use a matrix to multilply the ascii value of each letter and encript it that way. Using a matrix is harder to decifer if someone else gets your message. Although I do not remember how to do this (been 6 years), but I do think I will get into this before too long.
    C++ Is Powerful
    I use C++
    There fore I am Powerful

  10. #10
    Banned frenchfry164's Avatar
    Join Date
    Oct 2001
    Posts
    1,552
    Can you guys give an in-program decrypt / encrypt source code? That way when you need a file you decrypt it, read it, then encrypt it again? I know I can pick apart that dudes code, but I am lazy

  11. #11
    /dev/null
    Guest
    /* RC4 Stream Cipher algorithm designed by RSA Inc.
    Written by Graham Richter (grichter@mweb.co.za)
    */

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

    unsigned char S[256],K[256];
    char Key[]="123456789";
    unsigned char data,tmp,t,i,j=0;


    void prepkey(void)
    {
    for (int z=0;z<256;z++) {
    S[z]=z;
    K[z]=Key[z%strlen(Key)];
    }

    for (int z=0;z<256;z++) {
    j=j+S[z]+K[z];
    tmp=S[z];
    S[z]=S[j];
    S[j]=tmp;
    }
    j=0;
    }

    unsigned char RC4(void)
    {
    i=i+1;
    j=j+S[i];
    tmp=S[i];
    S[i]=S[j];
    S[j]=tmp;
    t=S[i]+S[j];
    return(S[t]);
    }

    void main(void){
    prepkey();
    /* Each call to RC4() returns the next keystream byte. Data is encrypted by XORing the data byte with the next keystream byte.*/

    // encrypted_data = plain_data ^ RC4();
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  2. help needed with edit control & encryption
    By willc0de4food in forum Windows Programming
    Replies: 2
    Last Post: 03-16-2006, 07:21 PM
  3. abt encryption algorithm
    By purIn in forum C Programming
    Replies: 9
    Last Post: 12-22-2003, 09:16 PM
  4. What's wrong with my Stream Cipher Encryption?
    By Davros in forum C++ Programming
    Replies: 3
    Last Post: 04-18-2002, 09:51 PM
  5. File Encryption & Read/Write in Binary Mode
    By kuphryn in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 05:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21