Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Simple mcrypt et decript functions rijndeal-256 algorithme

843811Sep 7 2008 — edited Sep 10 2008
Hello every one, I'm new to the encryption and decription world and I have to do a very simple encription and decription functions from php to java.

Actualy it is some kind of translating code from PHP to Java. And I have been spending a lot of timsreading on the interernet without succeding anything...


function TripleDesDecryption($string, $key)
{
$iv = false;
// set mcrypt mode and cipher
$td = mcrypt_module_open('tripledes', '', 'ecb', '') ;

// Unix has better pseudo random number generator then mcrypt, so if it is available lets use it!
$random_seed = strstr(PHP_OS, "WIN") ? MCRYPT_RAND : MCRYPT_DEV_RANDOM;

// if initialization vector set in constructor use it else, generate from random seed
$iv = ($iv === false) ? mcrypt_create_iv(mcrypt_enc_get_iv_size($td), $random_seed) : substr($iv, 0, mcrypt_enc_get_iv_size($td));

// get the expected key size based on mode and cipher
$expected_key_size = mcrypt_enc_get_key_size($td);

// initialize mcrypt library with mode/cipher, encryption key, and random initialization vector
mcrypt_generic_init($td, $key, $iv);

$value = trim(mdecrypt_generic($td, base64_decode($string)));

// shutdown mcrypt
mcrypt_generic_deinit($td);

// close mcrypt cipher module
mcrypt_module_close($td);

return $value;
}







function TripleDesEncryption($string, $key)
{
$iv = false;

// set mcrypt mode and cipher
$td = mcrypt_module_open('tripledes', '', 'ecb', '') ;

// Unix has better pseudo random number generator then mcrypt, so if it is available lets use it!
$random_seed = strstr(PHP_OS, "WIN") ? MCRYPT_RAND : MCRYPT_DEV_RANDOM;

// if initialization vector set in constructor use it else, generate from random seed
$iv = ($iv === false) ? mcrypt_create_iv(mcrypt_enc_get_iv_size($td), $random_seed) : substr($iv, 0, mcrypt_enc_get_iv_size($td));

// get the expected key size based on mode and cipher
$expected_key_size = mcrypt_enc_get_key_size($td);

// initialize mcrypt library with mode/cipher, encryption key, and random initialization vector
mcrypt_generic_init($td, $key, $iv);

$value = base64_encode(mcrypt_generic($td, $string));

// shutdown mcrypt
mcrypt_generic_deinit($td);

// close mcrypt cipher module
mcrypt_module_close($td);

return $value;
}


I don't know why I don't find the equivalence in Java.




Here is some java code (I found the gnu.crypto.cipher.Rijndael to be the easiest one to use, but it doesn't work properly :P):

private
static void encrypt(String chain, String p_publicKey)
throws Exception {
byte[] keyBytes = new byte[]
{
(
byte)Integer.parseInt("06", 16),
(
byte)Integer.parseInt("47", 16),
(
byte)Integer.parseInt("7b", 16),
(
byte)Integer.parseInt("ba", 16),
(
byte)Integer.parseInt("f0", 16),
(
byte)Integer.parseInt("0b", 16),
(
byte)Integer.parseInt("f3", 16),
(
byte)Integer.parseInt("f8", 16),
(
byte)Integer.parseInt("e7", 16),
(
byte)Integer.parseInt("de", 16),
(
byte)Integer.parseInt("ac", 16),
(
byte)Integer.parseInt("72", 16)
};
for(int i = 0; i < keyBytes.length; i++)
{
System.
out.print(Integer.toHexString(keyBytes&0xff));
}
String plainTex =
"dsdsa";
//gnu.crypto.cipher.Rijndael tempRej = new Rijndael();
gnu.crypto.cipher.Rijndael tempRej =
new Rijndael();
String tempStr =
"06477bbaf00bf3f8e7deac72";
Object objSessionkey = tempRej.makeKey(tempStr.getBytes()
/*keyBytes*/, 16);
byte[] plainTexeBTE = new byte[16];
byte[] out = new byte[16];
byte[] tempStbte = plainTex.getBytes();
for (int i= 0; i < plainTexeBTE.length; i++)
{
if(i < tempStbte.length)
{
plainTexeBTE[i] = tempStbte[i];
}
else
{
plainTexeBTE[i] =
' ';
}
}
tempRej.encrypt(plainTexeBTE, 0, out, 0, objSessionkey, 16);
String tempString =
new String(plainTex.getBytes());
System.
out.println("\nThe string to encript :" + tempString);
System.
out.println("The encrypted string :" + new String(out));
return;
}


If you have another suggestion (or api), that is sure to do the trik it would be greatly appreciated. The worst case combine java and php as native code ? I tried other security module, but it craches completly :P

Edited by: ymazal on Sep 7, 2008 6:51 PM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 8 2008
Added on Sep 7 2008
8 comments
392 views