0

I've made this PHP encrypt script. Now I was wondering how this could be done in Java. I was considering giving this up, and coding it in Javascript so it could be used in both languages. This is unfortunately not what I want. How can I do so in Java?

<?php
  $encrypted = encrypt("Hello goodbye", "Pizza");
  $decrypted = decrypt($encrypted, "Pizza");

  echo $encrypted;
  echo "<br/>";
  echo $decrypted;

  function encrypt($string, $key) {
    $result = '';
    for($i=0; $i<strlen($string); $i++) {
      $char = substr($string, $i, 1);
      $keychar = substr($key, ($i % strlen($key))-1, 1);
      $char = chr(ord($char)+ord($keychar));
      $result.=$char;
    }

    return base64_encode($result);
  }

  function decrypt($string, $key) {
    $result = '';
    $string = base64_decode($string);

    for($i=0; $i<strlen($string); $i++) {
      $char = substr($string, $i, 1);
      $keychar = substr($key, ($i % strlen($key))-1, 1);
      $char = chr(ord($char)-ord($keychar));
      $result.=$char;
    }

    return $result;
  }
?>

I am not asking to write some code for me (but it would be great), but I would like to be helped or get a push in the right direction

EDIT: I do not want to post and get to the PHP file via Java

4
  • 1
    you can check this out if it helps : docs.oracle.com/javase/8/docs/api/java/util/Base64.html Commented Nov 22, 2016 at 8:27
  • This is not even encryption, just implementing some scrambling in Java. As this is not a code service, please post the java code. Commented Nov 22, 2016 at 8:28
  • @MargaretBloom I don't even have the Java code, as I have no idea how to do so. Commented Nov 22, 2016 at 8:29
  • 2
    In such case, the best course of actions is deleting this question and looking for help on some Java forum. Once you have the code you are welcome to ask a new question here, granted it satisfy the on topic guidelines (help center) Commented Nov 22, 2016 at 8:31

2 Answers 2

1
import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class AdvanceEncryptionSecurity {

    private static final String ALGORITHM = "AES";
    private static final int ITERATIONS = 2;
    private static final byte[] keyValue = new byte[] { 'P', 'R', 'S', 'a', 'n', 'd', 'A', 'P', 'F', 'A', 'A', 'l', 'l', 'i', 'e', 'd' };
    private static String salt = "prs and pfa";

    public static String encrypt(String value) throws Exception {
        Key key = generateKey();
        Cipher c = Cipher.getInstance(ALGORITHM);  
        c.init(Cipher.ENCRYPT_MODE, key);

        String valueToEnc = null;
        String eValue = value;
        for (int i = 0; i < ITERATIONS; i++) {
            valueToEnc = salt + eValue;
            byte[] encValue = c.doFinal(valueToEnc.getBytes());
            eValue = new BASE64Encoder().encode(encValue);
        }
        return eValue;
    }

    public static String decrypt(String value) throws Exception {
        Key key = generateKey();
        Cipher c = Cipher.getInstance(ALGORITHM);
        c.init(Cipher.DECRYPT_MODE, key);

        String dValue = null;
        String valueToDecrypt = value;
        for (int i = 0; i < ITERATIONS; i++) {
            byte[] decordedValue = new BASE64Decoder().decodeBuffer(valueToDecrypt);
            byte[] decValue = c.doFinal(decordedValue);
            dValue = new String(decValue).substring(salt.length());
            valueToDecrypt = dValue;
        }
        return dValue;
    }

    private static Key generateKey() throws Exception {
        Key key = new SecretKeySpec(keyValue, ALGORITHM);
        return key;
    }
}

Try this code. It can encrypt and decrypt. I'm assuming you know how to code in java.

Sign up to request clarification or add additional context in comments.

3 Comments

I am using it on android java so it can't import sun.misc.BASE64Decoder and encoder
I solved the import error. How can I provide a key myself or something like that? Because I'm not receiving the same encrypted phrase as when I'm encrypting it with PHP
We have different algo that's why we don't have the same encrypted phrase.
0

This GitHub repository solved it: Android PHP Encrypt Decrypt

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.