mardi 26 avril 2016

My encryption method uses too many if statements

For a java 1 project we have to take an image file and encrypt its RGB codes. I am doing this by assigning each number in the file to a letter. The user of the code also has a key that changes what each letter replaces. The way i'm doing this is making if statements to first show which "set" of letters you will be using. then I use if statements to find and write the code into a file. If I do it this way, It will come out to 1040 if statements, and I am unsure if there is a better way to do this. Code:

public static void encryptthatshitFile()
{
    System.out.println("Please select an image file to convert to RGB numbers");
    String fileNameIn=FileChooser.pickAFile();
    Picture picFile=new Picture(fileNameIn);
    String fileNameOut=fileNameIn.substring(0,fileNameIn.length()-3)+"txt";
    System.out.println("Please enter your encryption key");
    Scanner scan=new Scanner(System.in);
    int key;
    key=scan.nextInt();
    int a=0;
    a=(a+key)%26;
    try{
        FileWriter writeRGB= new FileWriter(fileNameOut);
        writeRGB.write(picFile.getWidth()+" "+picFile.getHeight()+" ");
        for(int row=0; row<picFile.getHeight(); row++){
            for(int col=0; col<picFile.getWidth(); col++)
                {
                    Pixel pix=picFile.getPixel(col,row);
                    if(a=0){
                        int red=pix.getRed();
                        String rednum=""+red;
                        String newRed="";
                        String newBlue"";
                        String newGreen"";
                        String newAlpha"";
                        for(int i=0; i<rednum.length(); i++){
                            if(full.charAt(i)=="0"){
                                newRed="a";
                            }
                        }
                        else if(a=1){
                        }
                        else if(a=2){
                        }
                        else if(a=3){
                        }
                        else if(a=4){
                        }
                        else if(a=5){
                        }
                        else if(a=6){
                        }
                        else if(a=7){
                        }
                        else if(a=8){
                        }
                        else if(a=9){
                        }
                        else if(a=10){
                        }
                        else if(a=11){
                        }
                        else if(a=12){
                        }
                        else if(a=13){
                        }
                        else if(a=14){
                        }
                        else if(a=15){
                        }
                        else if(a=16){
                        }
                        else if(a=17){
                        }
                        else if(a=18){
                        }
                        else if(a=19){
                        }
                        else if(a=20){
                        }
                        else if(a=21){
                        }
                        else if(a=22){
                        }
                        else if(a=23){
                        }
                        else if(a=24){
                        }
                        else if(a=25){
                        }
                        writeRGB.write(pix.getRed()+" "+pix.getGreen()+" "+pix.getBlue()+" "+pix.getAlpha()+" ");
                    }
                }
            writeRGB.close();
        }
        catch(IOException e){
            System.out.println("General I/O exception: "+e.getMessage());
            e.printStackTrace();
        }
        System.out.println("RGB number file created:" +fileNameOut);
    }

Also, I wrote the first part for the red codes only. It should work for all 4 codes with minor corrections.

Thanks in advance

import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import java.text.*;
import java.util.*;
import java.util.List; // resolves problem with java.awt.List and java.util.List

import java.util.Scanner;
import java.io.File;
import java.io.FileWriter;
import java.io.FileNotFoundException;
import java.io.IOException;

This is my list of imports.

Aucun commentaire:

Enregistrer un commentaire