· 8 years ago · Dec 28, 2017, 12:28 PM
1String algorithm = "PBEWITHSHAAND3-KEYTRIPLEDES-CBC";
2String password = "eKhfdPKO54OddrfgghuBGHsA5BGTYHon";
3byte[] salt = {-87, -101, -56, 50, 86, 52, -29, 3};
4int iterations = 19;
5
6String text = "foobar";
7
8Provider bouncy = new org.bouncycastle.jce.provider.BouncyCastleProvider();
9Security.addProvider(bouncy);
10
11KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, iterations);
12SecretKey key = SecretKeyFactory.getInstance(algorithm, bouncy).generateSecret(keySpec);
13
14Cipher cipher = Cipher.getInstance(algorithm, bouncy);
15cipher.init(1, key, new PBEParameterSpec(salt, iterations));
16
17System.out.println(new String(Base64.getEncoder().encode(cipher.doFinal(text.getBytes("UTF8")))));
18// Output: kaxAiR1Qb9s=
19
20$hash = 'sha1';
21$kdf = 'pkcs12';
22$password = 'eKhfdPKO54OddrfgghuBGHsA5BGTYHon';
23$salt = chr(-87) . chr(-101) . chr(-56) . chr(50) . chr(86) . chr(52) . chr(-29) . chr(3);
24$iterations = 19;
25
26$text = "foobar";
27
28$cipher = new phpseclibCryptTripleDES('cbc');
29$cipher->setPassword($password, $kdf, $hash, $salt, $iterations);
30
31echo base64_encode($cipher->encrypt($text));
32// Output: daAlVF+JjNg=