0

I want to encrypt the final grades of students in PHP and decrypt it in Android Java. I referred my codes here but it returns wrong value.

This is my PHP encryption function

function encode5t($value1){
      for($i=0;$i<3;$i++)
    {
        $value1=base64_encode(strrev($value1));
    }
    return $value1;
    }

Call the function:

    foreach ($rows as $row){
        $post["cSemester"] = $row["cSemester"];
        $post["cSchoolYear"] = $row["cSchoolYear"];
        $post["cSubjectCode"] = $row["cSubjectCode"];
        $post["cDescription"] = $row["cDescription"];
        $post["nFGrade"] = encode5t($row["nFGrade"]);
        $post["nCGrade"] = $row["nCGrade"];
        $post["nCredit"] = $row["nCredit"];

        //update our response JSON data
        array_push($response["posts"], $post);
    }
    echo json_encode($response);

And this is my Java code.

    vGrades = json.getJSONArray(TAG_POSTS);
    for (int i = 0; i < vGrades.length(); i++) {
    JSONObject c = vGrades.getJSONObject(i);

    String cSemester = c.getString(TAG_SEMESTER);
    String cSchoolYear = c.getString(TAG_SCHOOLYEAR);
    String cSubjectCode = c.getString(TAG_SUBJECTCODE);
    String cDescription = c.getString(TAG_DESCRIPTION);
    String encrypted_string = c.getString(TAG_FINALGRADE);
    String nCGrade = c.getString(TAG_COMPLETIONGRADE);
    String nCredit = c.getString(TAG_CREDIT);
    HashMap<String, String> map = new HashMap<String, String>();

    try{
    byteArray = Base64.decode(encrypted_string, Base64.DEFAULT);
    decrypt = new String(byteArray, "UTF-8");
    }catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }  // this is where I want to decrypt it.

    nFGrade = decrypt;
    map.put(TAG_SEMESTER, cSemester);
    map.put(TAG_SCHOOLYEAR, cSchoolYear);
    map.put(TAG_SUBJECTCODE, cSubjectCode);
    map.put(TAG_DESCRIPTION, cDescription);
    map.put(TAG_FINALGRADE, nFGrade);
    map.put(TAG_COMPLETIONGRADE, nCGrade);
    map.put(TAG_CREDIT, nCredit);
    ViewGrades.add(map);
    }

The PHP encryption is running .. but when i decrypt it the system returns another encrypted value .. for example the fGrade is 1.0.

the PHP encypted String value is: "PT1RVERSRGU="

the Java decrypted value is: "==QTDRDe"

where did I go wrong? I need help please ..thanks guys!

5
  • 1
    You are "encrypting" the string 3 times in the PHP code. Each time reversing it before "encryption". In your java code your are "decrypting" just one time and not reversing it. Beware that your are encoding the string at best, and base64 is an easy spot on. Commented Feb 27, 2016 at 13:40
  • 1
    base64 is not encryption, only encoding. Anyone can decode it. Commented Feb 27, 2016 at 14:44
  • Base64 is an encoding, not encryption. Encryption uses a key to encrypt and decrypt and that key must be kept secret. Commented Feb 27, 2016 at 15:24
  • @MargaretBloom - thanks for the advice .. i have decoded the string on Java .. Commented Feb 27, 2016 at 15:42
  • @zaph - does this Base64 is not safe ??would you please give me a link for the encryption and decryption in java and PHP ?thanks in advance! Commented Feb 27, 2016 at 15:44

1 Answer 1

0

Base64 has no key, anyone can decode it, change it and replace it. But that may be all you need if you just want to thwart the casual user. You need to define who you are protecting against knowing that every scheme can be compromised.

To encrypt data use AES. Encryption is not easy to get correct. The PHP mcrypt encryption function is flawed, do not use it. Consider RNCryptor-php, it provides a full solution including authentication and key derivation.

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

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.