java和php对等的3DES加密算法,ECB的加密模式没有CBC安全,iv是初始向量相当于种子。
参考文章:http://www.cnblogs.com/happyhippy/archive/2006/12/23/601353.html
?
class="java" name="code">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")); } }?
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(); */ ?>