I've written this code to encrypt all the course resources (video content on server) in PHP.
class MCrypt {
private $iv = 'MYKEYHERE'; #Same as in JAVA
private $key = 'MYKEYHERE'; #Same as in JAVA
function __construct() {
}
function encrypt($str) {
//$key = $this->hex2bin($key);
$iv = $this->iv;
$td = mcrypt_module_open('rijndael-128', '', 'cbc', $iv);
mcrypt_generic_init($td, $this->key, $iv);
$encrypted = mcrypt_generic($td, $str);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return bin2hex($encrypted);
}
}
and corresponding to this I have written this code to decrypt the video content.
FileInputStream fis = new FileInputStream("path to file");
byte[] data = new byte[fis.available()];
fis.read(data);
String encryptedData = new String(data, "UTF-8");
fis.close();
System.out.println(encryptedData);
THE PROBLEM ARISE HERE I'M NOT GETTING A PROPER HEXADECIMAL ENCRYPTED FROM OF VIDEO BECAUSE OF THIS WRONG ENCRYPTED DATA THE hexToBytes(String str) CONTINUOUSLY NUMBER FORMAT EXCEPTION
private String iv = "MYKEYHERE";//Dummy iv (CHANGE IT!)
private String SecretKey = "MYKEYHERE";//Dummy secretKey (CHANGE IT!)
private byte[] decrypt(String code)
{
byte[] decrypted = null;
try {
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());
SecretKeySpec keyspec = new SecretKeySpec(SecretKey.getBytes(), "AES");
if(code == null || code.length() == 0)
throw new Exception("Empty string");
cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
decrypted = cipher.doFinal(hexToBytes(code));
} catch (Exception e) {
e.printStackTrace();
}
return decrypted;
}
private static byte[] hexToBytes(String str) {
if (str==null) {
return null;
} else if (str.length() < 2) {
return null;
} else {
int len = str.length() / 2;
byte[] buffer = new byte[len];
for (int i=0; i<len; i++) {
try {
buffer[i] = (byte) Integer.parseInt(str.substring(i*2,i*2+2),16);
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return buffer;
}
}
I'm also attaching two encrypted resource using same method in PHP (One is image other is that video). you can see that image resource returns correct HexaDecimal equivalent of encrypted data but same is not the case with video. I can also post the error log if anybody wants
0 comments:
Post a Comment