您好,欢迎访问一九零五行业门户网

php 和 java共用的加密方法

http://www.php.net/manual/en/ref.mcrypt.php to have the same result in java and php with the bouncycastle library : php function encrypt($str, $operation, $key) { $ret = ''; $key = pack('h*', $key); if (strtoupper($operation) == 'decode')
http://www.php.net/manual/en/ref.mcrypt.php
to have the same result in java and php with the bouncycastle library :
php
function encrypt($str, $operation, $key) { $ret = ''; $key = pack('h*', $key); if (strtoupper($operation) == 'decode') { $str = pack('h*', $str); $ret = mcrypt_ecb(mcrypt_blowfish, $key, $str, mcrypt_decrypt); } else { $ret = mcrypt_ecb(mcrypt_blowfish, $key, $str, mcrypt_encrypt); $ret = bin2hex($ret); } return $ret; }
java
import java.security.*;import javax.crypto.*;import javax.crypto.spec.secretkeyspec;public class tester { public static void crypter(string password) { try { // -- install jar bcprov-jdk14-135.jar in /jre/lib/ext/ // security.addprovider(new org.bouncycastle.jce.provider.bouncycastleprovider()); cipher cipher = cipher.getinstance(blowfish/ecb/zerobytepadding); // -- substring is used to have no problem with key length secretkeyspec keyspec = new secretkeyspec(password.substring(0,8).getbytes(), blowfish); cipher.init(cipher.encrypt_mode, keyspec); byte[] outtext = cipher.dofinal(password.getbytes()); system.out.println(ashex(outtext)); } catch (exception e) { e.printstacktrace(); } } public static string ashex (byte buf[]) { stringbuffer strbuf = new stringbuffer(buf.length * 2); int i; for (i = 0; i
其它类似信息

推荐信息