In my Computer Science AP class, my team and I are attempting to translate a PHP encryption function of mine into java for our project.

I haven't really done any translating from computer language to computer language, and it would be really great if any of you guys can help me convert the script.

I'll post the original encryption function, followed by what I have so far.

Any help is greatly appreciated Very Happy

Original PHP encryption program

Code:


   function simdec($link) {
      $seedString = 'password';
      $value = '3';
      $orgArray = getOrgArray();
      $suffledArray = getSuffledArray($seedString);
      $arr  = getCodeTranslator($orgArray, $suffledArray);
      $arr2 = getCodeTranslator($suffledArray, $orgArray);
      $thingdec = strtr(base64_decode(strtr($link,$arr2)),$arr2);
      $decode = substr($thingdec, -$value);
      $dectext = substr($thingdec, 0, -$value);
      return simdecdouble($dectext, $decode);
   }
   function simdecdouble($link, $seedString) {
      $orgArray = getOrgArray();
      $suffledArray = getSuffledArray($seedString);
      $arr  = getCodeTranslator($orgArray, $suffledArray);
      $arr2 = getCodeTranslator($suffledArray, $orgArray);
      $thingdec = strtr(base64_decode(strtr($link,$arr2)),$arr2);
      return $thingdec;
   }
   function getCodeTranslator($array1, $array2) {
      $array = array();
      
      for($index = 0; $index < sizeof($array1); $index += 1) {
         $array[$array1[$index]] = $array2[$index];
      }
      
      return $array;
   }
   function getOrgArray() {
      return array(
         'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
         'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
         'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',
         'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
         'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
         'Y', 'Z', '1', '2', '3', '4', '5', '6', '7', '8',
         '9', '0');
   }
   function getSuffledArray($seed) {
      $array = getorgArray();
      fyshuffle($array, $seed);
      return $array;
   }
   function fyshuffle(&$items,$seed) {
      if (!$seed) {
         $seedval = time();
      } else {
         if (is_numeric($seed)) {
            $seedval = $seed;
         } else {
            for($i=0;$i<=strlen($seed);$i++) {
               $seedval += ord($seed[$i]);
            }
         }
      }
      srand($seedval);
      for ($i = count($items) - 1; $i > 0; $i--) {
         $j = @rand(0, $i);
         $tmp = $items[$i];
         $items[$i] = $items[$j];
         $items[$j] = $tmp;
      }
   }


Failed java translation so far

Code:

// Required for method random
import java.util.Random;

public class encrypt
{   
   public static void main(String args[])
   {
      
      Random rand = new Random();
      System.out.println(random(100,300));
      String orgArray = getOrgArray();
      System.out.println(orgArray);
      
      
   }
   
   // Main encryption method.
   public encrypt(String t)
   {
      // Text to be encrypted
      String text = t;
      // Password that is implemented into the encrypt script
      String password = "password";
      // Random value between 100 - 300 to provide more security.  Implemented in encryption code below.
      int randomValue = random(100,300);
      String orgArray = getOrgArray();
      
   }
   
   public static String getOrgArray()
   {
      return "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
   }

   // Random number generator method
   public static int random(int min, int max)
   {
      Random randfunc = new Random();
      return randfunc.nextInt(max)+min;
   }
}


I know that it is asking a lot, but if anyone can lead me in the right direction or even translate the whole thing if they feel very generous today, I would be very grateful.

Thank you.
What's the failure of the translation? Is it a lack-of-knowledge or a glitch?
...why would you ever want to do this.
Well, I am not as experienced in java as I am in PHP. Am I doing ok so far? It compiles without any problems, but there are somethings that I don't know how to do in java which I know how to do in PHP.

For example, the function getOrgArray(). How would I return an array in java instead of making it a String like it is right now?

Basically, I am running into small problems like that.
allynfolksjr wrote:
...why would you ever want to do this.


I don't like to do a lot of things in school, but I do like to get good grades.
I the issue is that you're somewhat inexperienced in Java, you're welcome to glance through some of the Powerpoint presentations that I made when I was teaching a Java class, and I'll try to explain anything with which you're having difficulty. Here are the presentations: I will also address your code in a separate post.

http://www.ee.cooper.edu/java/
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement