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
-
1you can check this out if it helps : docs.oracle.com/javase/8/docs/api/java/util/Base64.htmlGoro– Goro2016年11月22日 08:27:13 +00:00Commented 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.Margaret Bloom– Margaret Bloom2016年11月22日 08:28:42 +00:00Commented Nov 22, 2016 at 8:28
-
@MargaretBloom I don't even have the Java code, as I have no idea how to do so.Jason– Jason2016年11月22日 08:29:33 +00:00Commented Nov 22, 2016 at 8:29
-
2In 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)Margaret Bloom– Margaret Bloom2016年11月22日 08:31:28 +00:00Commented Nov 22, 2016 at 8:31
2 Answers 2
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.
3 Comments
This GitHub repository solved it: Android PHP Encrypt Decrypt
Comments
Explore related questions
See similar questions with these tags.