note 72224 deleted from ref.mcrypt by danbrown
| From: | danbrown@php.net | Date: | Mon, 13 Dec 2010 16:19:30 +0000 |
| Subject: | note 72224 deleted from ref.mcrypt by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-174846@lists.php.net to get a copy of this message | ||
Note Submitter: terry200382 at gmail dot com
----
To have the same result in Java and PHP with the BouncyCastle library :
/*********
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 <install_jdk>/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 < buf.length; i++) {
if (((int) buf[i] & 0xff) < 0x10)
strbuf.append("0");
strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
}
return strbuf.toString();
}
public static void main(String[] args) {
try {
crypter("password");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
/********
PHP
*********/
<?php
header("Content-Type: text/plain");
$data = "password";
echo substr($data, 0,8)."\n";
$encrypted_data = mcrypt_ecb(MCRYPT_BLOWFISH, substr($data, 0,8), $data, MCRYPT_ENCRYPT);
echo bin2hex($encrypted_data);
?>