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"); } } |
QUOTE (Zod @ Dec 10 2003, 02:28 PM) |
@halcyon_lll: ai nevoie de algoritmul cu parola sau fara? |
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; } |