|
—
本帖被 云天河 从 java技术交流 移动到本区(2014-07-24)
—
网上找了好几个,都不能用。 发现php在加密的时候,有个$key,还有个$iv, 很多java的3DES加密算法里面没有$iv的。 最终找到了一个版本 http://jamie-wang.iteye.com/blog/1604596 java代码: - package org.jamie.demo;
-
- import java.security.NoSuchAlgorithmException;
- import java.security.Security;
- import java.util.Random;
-
- import javax.crypto.Cipher;
- import javax.crypto.KeyGenerator;
- import javax.crypto.SecretKey;
- import javax.crypto.SecretKeyFactory;
- import javax.crypto.spec.DESedeKeySpec;
- import javax.crypto.spec.IvParameterSpec;
-
- @SuppressWarnings("restriction")
- public class TripleDES {
- static {
- Security.addProvider(new com.sun.crypto.provider.SunJCE());
- }
-
- private static final String MCRYPT_TRIPLEDES = "DESede";
- private static final String TRANSFORMATION = "DESede/CBC/PKCS5Padding";
-
- public static byte[] decrypt(byte[] data, byte[] key, byte[] iv) throws Exception {
- DESedeKeySpec spec = new DESedeKeySpec(key);
- SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(MCRYPT_TRIPLEDES);
- SecretKey sec = keyFactory.generateSecret(spec);
- Cipher cipher = Cipher.getInstance(TRANSFORMATION);
- IvParameterSpec IvParameters = new IvParameterSpec(iv);
- cipher.init(Cipher.DECRYPT_MODE, sec, IvParameters);
- return cipher.doFinal(data);
- }
-
- public static byte[] encrypt(byte[] data, byte[] key, byte[] iv) throws Exception {
- DESedeKeySpec spec = new DESedeKeySpec(key);
- SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
- SecretKey sec = keyFactory.generateSecret(spec);
- Cipher cipher = Cipher.getInstance(TRANSFORMATION);
- IvParameterSpec IvParameters = new IvParameterSpec(iv);
- cipher.init(Cipher.ENCRYPT_MODE, sec, IvParameters);
- return cipher.doFinal(data);
- }
-
- public static byte[] generateSecretKey() throws NoSuchAlgorithmException {
- KeyGenerator keygen = KeyGenerator.getInstance(MCRYPT_TRIPLEDES);
- return keygen.generateKey().getEncoded();
- }
-
- public static byte[] randomIVBytes() {
- Random ran = new Random();
- byte[] bytes = new byte[8];
- for (int i = 0; i < bytes.length; ++i) {
- bytes<i> = (byte) ran.nextInt(Byte.MAX_VALUE + 1);
- }
- return bytes;
- }
-
- public static void main(String args[]) throws Exception {
- String plainText = "a12*&1c中文";
- final byte[] secretBytes = TripleDES.generateSecretKey();
-
- final byte[] ivbytes = TripleDES.randomIVBytes();
- System.out.println("plain text: " + plainText);
- byte[] encrypt = TripleDES.encrypt(plainText.getBytes(), secretBytes, ivbytes);
- System.out.println("cipher text: " + encrypt);
- System.out.println("decrypt text: " + new String(TripleDES.decrypt(encrypt, secretBytes, ivbytes), "UTF-8"));
- }
-
- }</i>
php代码- <?php
- class TripleDES {
- public static function genIvParameter() {
- return mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_TRIPLEDES,MCRYPT_MODE_CBC), MCRYPT_RAND);
- }
-
- private static function pkcs5Pad($text, $blocksize) {
- $pad = $blocksize - (strlen($text) % $blocksize); // in php, strlen returns the bytes of $text
- return $text . str_repeat(chr($pad), $pad);
- }
-
- private static function pkcs5Unpad($text) {
- $pad = ord($text{strlen($text)-1});
- if ($pad > strlen($text)) return false;
- if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) return false;
- return substr($text, 0, -1 * $pad);
- }
-
- public static function encryptText($plain_text, $key, $iv) {
- $padded = TripleDES::pkcs5Pad($plain_text, mcrypt_get_block_size(MCRYPT_TRIPLEDES, MCRYPT_MODE_CBC));
- return mcrypt_encrypt(MCRYPT_TRIPLEDES, $key, $padded, MCRYPT_MODE_CBC, $iv);
- }
-
- public static function decryptText($cipher_text, $key, $iv) {
- $plain_text = mcrypt_decrypt(MCRYPT_TRIPLEDES, $key, $cipher_text, MCRYPT_MODE_CBC, $iv);
- return TripleDES::pkcs5Unpad($plain_text);
- }
- };
-
- /*
- function main() {
- $iv = TripleDES::genIvParameter();
- print "\$iv=$iv\n";
- $plain_text="this is a test,包括中文";
- $key="ABCDEFGHIJ0123456789ABCD";
- $cipher_text = TripleDES::encryptText($plain_text, $key, $iv);
- print "\$cipher_text=$cipher_text\n";
- $plain_text = TripleDES::decryptText($cipher_text, $key, $iv);
- print "\$plain_text=$plain_text\n";
- }
-
- main();
- */
- ?>
经测试:php使用类里面的encryptText函数加密和,和java的一样。
|