robdog
Well-known member
Java:
import java.security.MessageDigest;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
/**
* Method to decrypt encrypted String text using secret password
* @param password, secret key to decrypt the payload string
* @param payloadJsonString, Student marking error/success details json in String format
* @return decrypted payload json in String format
* @throws Exception
*/
public static String decryptText(String password, String payloadjsonstring JsonString) throws Exception {
//Extract base64 ivParams
String ivParamsAsBase64 = payloadJsonString.substring(0, 24);
//Extract base64 salt
String saltAsBase64 = payloadJsonString.substring(24, 52);
//Extract base64 text (student marking success/error details)
String cipherTextAsBase64 = payloadJsonString.substring(52);
//convert salt from base64 format to byte array
byte[] salt = Base64.decodeBase64(saltAsBase64);
//convert password key from string to byte array
byte[] pass = password.getBytes();
//construct secret key for text decryption
byte[] key = new byte[salt.length + pass.length];
System.arraycopy(pass, 0, key, 0, pass.length);
System.arraycopy(salt, 0, key, pass.length, salt.length);
byte[] secretKey = Arrays.copyOf(MessageDigest.getInstance("SHA-256").digest(key), 16);
SecretKeySpec skeySpec = new SecretKeySpec(secretKey, "AES");
//convert ivParams from base64 format to byte array
byte[] ivParams = Base64.decodeBase64(ivParamsAsBase64);
//create IvParameterSpec object using ivParams byte array
IvParameterSpec ivParameterSpec = new IvParameterSpec(ivParams);
//create and initialise Cipher object
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivParameterSpec);
//convert cipher text from base64 format to byte array
byte[] cipherText = Base64.decodeBase64(cipherTextAsBase64);
//decrypt text and return output string
byte[] dycryptedTextByte = cipher.doFinal(cipherText);
final String dycryptedTextString = new String(dycryptedTextByte);
return dycryptedTextString;
}
Let me know your price to get this converted. Thank you.