Page 2 of 2 FirstFirst 12
Results 11 to 12 of 12

Thread: Simple Encryption

  1. #11
    Senior Member
    Join Date
    Mar 2004
    Posts
    557
    Hi


    A question difficult to answer. A while ago, I wrote a post[1]
    which tries to answer that. In my opinion, cryptography
    (the encoding of messages) is simpler than cryptanalysis, in terms
    of mathematical skills needed. And also simpler, in terms of
    persistency needed...


    For convenience, a few definitions:


    Def: cryptography
    the study of means of converting information from its normal, comprehensible form
    into an incomprehensible format, rendering it unreadable without secret knowledge —
    the art of encryption[2]

    Def: cryptanalysis
    The study of how to circumvent the use of cryptography is called cryptanalysis[3]

    Def: cryptology
    cryptography and cryptanalysis are grouped together under the umbrella term cryptology

    Cheers.

    [1] http://www.antionline.com/showthread...hreadid=264224
    [2] http://en.wikipedia.org/wiki/Cryptography
    [3] http://en.wikipedia.org/wiki/Cryptanalysis
    If the only tool you have is a hammer, you tend to see every problem as a nail.
    (Abraham Maslow, Psychologist, 1908-70)

  2. #12
    Senior Member
    Join Date
    May 2003
    Posts
    226
    Code:
    /*
     * Created on Mar 2, 2005
     */
    import java.io.*;
    
    
    public class Encryption
    {
        public void encrypt(String str, String key)
        {
            char[] message = str.toCharArray();
            int len = message.length;
            char[] encrypt = new char[len];
    
            int key_ = Integer.parseInt(key);
    
            if (key_ < 0)
            {
                key_ = (key_ * -1);
            }
    
            for (int x = 0; x < len; ++x)
            {
                encrypt[x] = message[(x + key_) % len];
            }
    
            for (int y = 0; y < len; ++y)
            {
                encrypt[y] = (char) (((int) encrypt[y] + key_) % 128);
            }
    
            write(new String(encrypt));
    
            get_input();
        }
    
        public void decrypt(String key)
        {
            char[] decrypt = read().toCharArray();
            int len = decrypt.length;
            char[] message = new char[len];
    
            int key_ = Integer.parseInt(key);
    
            if (key_ < 0)
            {
                key_ = (key_ * -1);
            }
    
            int shift = 0;
    
            for (int x = 0; x < len; ++x)
            {
                shift = ((int) decrypt[x] - key_) % 128;
    
                if (shift < 0)
                {
                    shift += 128;
                }
    
                decrypt[x] = (char) (shift);
            }
    
            int mov = 0;
    
            for (int y = 0; y < len; ++y)
            {
                mov = (y - key_) % len;
    
                if (mov < 0)
                {
                    mov += len;
                }
    
                message[y] = decrypt[mov];
            }
    
            System.out.println("Message: " + new String(message));
    
            get_input();
        }
    
        public void get_input()
        {
            System.out.println("1. Encrypt.");
            System.out.println("2. Decrypt.");
            System.out.println("3. Exit.");
            System.out.print("Your choice: ");
    
            try
            {
                BufferedReader in = new BufferedReader(new InputStreamReader(
                            System.in));
                String str = "";
    
                str = in.readLine();
    
                int option = Integer.parseInt(str);
    
                switch (option)
                {
                    case 1:
                        System.out.print("Enter message: ");
    
                        String message = in.readLine();
    
                        System.out.print("Enter key to encrypt: ");
    
                        String key = in.readLine();
    
                        encrypt(message, key);
    
                        break;
    
                    case 2:
                        System.out.print("Enter key to decrypt: ");
    
                        String key_ = in.readLine();
                        decrypt(key_);
    
                        break;
    
                    case 3:
                        System.exit(0);
    
                        break;
    
                    default:
                        System.out.println("Invalid option.");
                        get_input();
                }
            }
            catch (IOException e)
            {
            }
        }
    
        public String read()
        {
            String str = "";
    
            try
            {
                BufferedReader in = new BufferedReader(new FileReader("message.txt"));
                str = in.readLine();
                in.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
    
            return str;
        }
    
        public void write(String str)
        {
            try
            {
                BufferedWriter out = new BufferedWriter(new FileWriter(
                            "message.txt"));
                out.write(str);
                out.close();
    
                System.out.println("message.txt created.");
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    
        public static void main(String[] agrs)
        {
            Encryption en = new Encryption();
            en.get_input();
        }
    }
    My first attemp on cryptograhy program. pardon me for the bad programming.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •