- DESede (as algorithm)
- DESede/CBC/PKCS5Padding (as transformation method)
key point here is transformation method, because php does not support "PKCS5Padding". It just adds empty bytes at the end of plain text to be encrypted to make it's length multiply of 8. so you must use "DESede/CBC/NoPadding" transformation method and do the same padding php does with hand not to get "invalid length"[1] errors.
you can see php code and it's java equivalent below.
PHP:
$plaintext = "Some-plain-text-message-to-be-symetrically-encrypted";
$deskey = "secret word with 24 byte";
$ivkey = "12345678";
$td = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_CBC, '');
mcrypt_generic_init($td, $deskey, $ivkey);
$encrypted_data = mcrypt_generic($td, $plaintext);
mcrypt_generic_deinit($td);
echo strtoupper(bin2hex($encrypted_data));
mcrypt_module_close($td);
JAVA
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import org.apache.commons.codec.binary.Hex;
import java.util.Date;
import java.util.Arrays;
import java.text.SimpleDateFormat;
import java.text.ParsePosition;
public class Encrypt {
public static void main(String[] args) throws Exception {
String plainText = "Some-plain-text-message-to-be-symetrically-encrypted";
String desKey = "secret word with 24 byte";
String ivKey = "12345678";
String algorithm = "DESede";
// String transformation = "DESede/CBC/PKCS5Padding";
String transformation = "DESede/CBC/NoPadding";
byte[] keyValue = desKey.getBytes("UTF-8");
byte[] ivValue = ivKey.getBytes("UTF-8");
DESedeKeySpec keySpec = new DESedeKeySpec(keyValue);
IvParameterSpec iv = new IvParameterSpec(ivValue);
SecretKey key = SecretKeyFactory.getInstance(algorithm).generateSecret(keySpec);
Cipher encrypter = Cipher.getInstance(transformation);
encrypter.init(Cipher.ENCRYPT_MODE, key, iv);
// byte[] input = plainText.getBytes();
byte[] input = getPaddedBytes(plainText);
byte[] encrypted = encrypter.doFinal(input);
System.out.println(new String(Hex.encodeHex(encrypted)).toUpperCase());
}
public static byte[] getPaddedBytes(String s) throws java.io.UnsupportedEncodingException {
int n = s.length();
n = n + (8 - (n % 8));
byte[] src = s.getBytes("UTF-8");
byte[] dst = Arrays.copyOf(src, n);
return src;
}
}
[1] javax.crypto.IllegalBlockSizeException: Input length not multiple of 8 bytes
16 comments:
Hi,
Nice stuff, should help me a lot!
But could it be possible to get the Java & Php decryption functions too?
Thanks a lot.
Regards.
Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training in India . Nowadays Java has tons of job opportunities on various vertical industry.
or Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry.
Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
Best Devops Training in pune
Best Devops Training institute in Chennai
I have picked cheery a lot of useful clothes outdated of this amazing blog. I’d love to return greater than and over again. Thanks!
Selenium training in Chennai
Selenium training in Bangalore
Selenium training in Pune
Selenium Online training
It was worth visiting your blog and I have bookmarked your blog. Hope to visit again
python Training in Pune
python Training in Chennai
python Training in Bangalore
Have questions to enter the online casino? Come to our section of rock BGAOC. best real online gambling slots Come and do not get lost. win with us.
Thanks for the informative article. This is one of the best resources I have found in quite some time. Nicely written and great info. I really cannot thank you enough for sharing
microsoft azure training in bangalore
rpa training in bangalore
best rpa training in bangalore
rpa online training
Thanks for Sharing This Article.It is very so much valuable content. I h
DevOps Training | Certification in Chennai | DevOps Training | Certification in anna nagar | DevOps Training | Certification in omr | DevOps Training | Certification in porur | DevOps Training | Certification in tambaram | DevOps Training | Certification in velachery
ope these Commenting lists will help to my website
Amazing blog.Impressive updates.
Java training in Chennai
Java training in Bangalore
Java training in Hyderabad
Java Training in Coimbatore
Java Online Training
Thank you for your post. This is excellent information. It is amazing and wonderful to visit your site.
java training in chennai
java training in tambaram
aws training in chennai
aws training in tambaram
python training in chennai
python training in tambaram
selenium training in chennai
selenium training in tambaram
This is certainly one of the most valuable article. Great tips from beginning to till end.
data science training in chennai
data science training in annanagar
android training in chennai
android training in annanagar
devops training in chennai
devops training in annanagar
artificial intelligence training in chennai
artificial intelligence training in annanagar
I just recently discovered your blog and have now scrolled through the entire thing several times. I am very impressed and inspired by your skill and creativity, and your "style" is very much in line with mine. I hope you keep blogging and sharing your design idea
data science training in chennai
data science training in velachery
android training in chennai
android training in velachery
devops training in chennai
devops training in velachery
artificial intelligence training in chennai
artificial intelligence training in velachery
I just recently discovered your blog and have now scrolled through the entire thing several times.
angular js training in chennai
angular js training in omr
full stack training in chennai
full stack training in omr
php training in chennai
php training in omr
photoshop training in chennai
photoshop training in omr
I must appreciate you for providing such a valuable content for us. This is one amazing piece of article. Helped a lot in increasing my knowledge
angular js training in chennai
angular js training in porur
full stack training in chennai
full stack training in porur
php training in chennai
php training in porur
photoshop training in chennai
photoshop training in porur
Excellent blog found to be very impressive to come across such an awesome blog. I really appreciate the blogger for the efforts they have made to bring such an awesome for all the curious readers who are in very keen of learning an innovative content all the way. Eventually, this blog brings an amazing experience while going through this blog. Thanks a lot and keep sharing the content in further as well.
aws online training
Online AWS Certification Training
online aws Course
Thanks for sharing a very useful article. Am sure it helped to clear the doubts I had. Keep posting more. Also dont forget to check out our pages too.
data science training in chennai
ccna training in chennai
iot training in chennai
cyber security training in chennai
ethical hacking training in chennai
Post a Comment