473,503 Members | 1,638 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PHP Encrypt/Decrypt whith asymetrics keys

Hi,
I have generated two keys :
"C:>openssl req -nodes -new -x509 -keyout ben.key -out ben.crt -days
3650"
I try to encrypt/decrypt a string like "JOHN" with these asymetrics
keys. With the following code, it works.
I encrypt with the public key which is in the certificate.
I decrypt with the private key.
But why, the crypted message is different every time I start the
programm...?
__________________________________________________ _______
<?php
echo "---CRYPT---<BR>";
$source="JOHN";
echo "Message : $source<BR>";
$fp=fopen("./ben.crt","r");
$pub_key=fread ($fp,8192);
fclose($fp);
//echo $pub_key;
openssl_get_publickey($pub_key);
openssl_public_encrypt ($source,$sourcecrypt,$pub_key);
echo "Crypted message : ".$sourcecrypt."<BR><BR>";
$source="";
echo "---DECRYPT---<BR>";
echo "Crypted message : ".$sourcecrypt."<BR>";
$fp=fopen("./ben.key","r");
$priv_key=fread ($fp,8192);
fclose($fp);
$res=openssl_get_privatekey($priv_key);
openssl_private_decrypt ($sourcecrypt,$newsource,$res);
echo "Source decryptée : $newsource<BR><BR>";
?>
__________________________________________________ _________

Now here is my second question :
In fact I encrypt with a java programm where is my certificate and I
decrypt with a PHP programm like I've just explane before.

__________________________________________________ _________
public String crypt(String message) {

//Cert is in LDAP
Certificate cert =
userProvider.getUserCertificate(getCurrentUsername ());

PublicKey publicKey = cert.getPublicKey();

try{
Provider secProvider = Security.getProvider("BC");
if (secProvider == null) {
secProvider = new BouncyCastleProvider();
Security.addProvider(secProvider);
}
Cipher encryptCipher = Cipher.getInstance("RSA", secProvider);
encryptCipher.init(Cipher.ENCRYPT_MODE, publicKey);

//Crypt...
String resultCrypt = new String();
byte[] messageBytes = message.getBytes();
byte[] resultCryptBytes = encryptCipher.doFinal(messageBytes);
resultCrypt = arr2str(resultCryptBytes);

return resultCrypt ;

}catch(Exception e){
//throw ...
}
}
__________________________________________________ ______________

Why my programm PHP can't decrypt the message? I use evidently the
correct private key which corresponds with the public key.

Thanks for your answers...

Sep 19 '05 #1
1 3947
Benoît wrote:
Hi,
I have generated two keys :
"C:>openssl req -nodes -new -x509 -keyout ben.key -out ben.crt -days
3650"
I try to encrypt/decrypt a string like "JOHN" with these asymetrics
keys. With the following code, it works.
I encrypt with the public key which is in the certificate.
I decrypt with the private key.
But why, the crypted message is different every time I start the
programm...?
Sounds like a good thing, particularly with short strings - the system is
applying some reversible modification of the data before encoding to
specifically avoid repetition, e.g. instead of:

$encrypted=encrypt($data, $private_key);

the system is might be doing something like:

$modifier=rand(0,10000) . time();
$data=base64_encode($data) . ":" . base64_encode($modifier);
$encrypted=encrypt($data);

(actually even I could come up with something better if I spent some time
thinking about it - no doubt the openssl people did already).
....so the data is always recoverable but the encrypted message contains
random junk which is discarded.

Now here is my second question :
In fact I encrypt with a java programm where is my certificate and I
decrypt with a PHP programm like I've just explane before.


<snip>
In addition to the reason cited above, openSSL may do all sorts of strange
things to package up the encrypted data.

I would suggest that you start by meking sure you can implement compatable
encryption frm the command line using openSSL.exe (which I suspect will be
straightforward), then try to reproduce the behaviour in Java (I'm sure the
Java newsgroups can better advise you on your Java code).

HTH

C.

Sep 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
7938
by: wqhdebian | last post by:
As far as I know,when encrypt or decrypt ,a key must first be got,and the key is first generate by a tool or from SecurityRandom,that means I can not generate the same key with the same input.Does...
4
28263
by: Spikinsson | last post by:
I'm looking for a good decrypt/encrypt function, all I want is a function in this form: char* encrypt(char* normal) { ... return encrypted; } and
0
1396
by: Mark Hanford | last post by:
I've been setting up a new MySQL/PHP site which will contain store some CC details, and have been wondering how to pass the keys. CC's are written in a similar way to: INSERT INTO cc (ccName,...
4
9067
by: Hrvoje Voda | last post by:
Does anyone knows a good example of how to encrypt/decrypt a string? Hrcko
8
8151
by: Gidi | last post by:
Hi, Is there Buid-In fuction in C# that Encrypt and Decrypt strings? i have a textbox which i'm writing into file, and i want to encrypt it before writing, i'm not looking for something fancy,...
0
1184
by: Randall Parker | last post by:
I'm just learning web security and so this might be a naive question: I'm looking here:...
4
4194
by: Islamegy® | last post by:
I give up.. I tried everything to encrypt querystring and decrypt it back but this never success.. i use RSA encryption. I always get excption when Convert fromBase64String so i tried...
4
2997
by: Steph | last post by:
Hi Everybody, Does anybody know whether it is possible to encrypt AUTOMATICALLY in PHP (at the server side) an Adobe PDF document (also stored at the server side) by using a public key coming...
3
8210
by: JDeats | last post by:
I have some .NET 1.1 code that utilizes this technique for encrypting and decrypting a file. http://support.microsoft.com/kb/307010 In .NET 2.0 this approach is not fully supported (a .NET 2.0...
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7086
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7280
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7332
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7462
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
382
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.