472,965 Members | 2,387 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,965 software developers and data experts.

how to encrypt with a string as input,but not a key object

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 there is a
method which can generate a same with the same input string?
There is a need to transfer file between to site,and the customer
wish to encrypt these files during transfering,and they want to store
a string into each database at each site,when sending and receiving
files,get the string from the database to encrypt or decrypt the
file,they alse want to have a control of the string,they can change
the string randomly if they wish as long as long keep each string is
equal,how to realize this?
Jul 17 '05 #1
1 7889
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

wqhdebian wrote:
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 there is a method which can generate a same with the
same input string?
There is a need to transfer file between to site,and the customer
wish to encrypt these files during transfering,and they want to
store a string into each database at each site,when sending and
receiving files,get the string from the database to encrypt or
decrypt the file,they alse want to have a control of the string,they
can change the string randomly if they wish as long as long keep
each string is equal,how to realize this?


Hi,
What you're looking for is called Password Based Encryption. Here's
how it works:

TO ENCRYPT:
1. Take the String and create a PBEKeySpec.
2. Use a SecretKeyFactory to produce a SecretKey.
3. Select an iteration count (probably a constant in code).
4. Generate a random salt.
5. Create a PBEParameterSpec from the iteration count and salt.
6. Create a Cipher from the SecretKey and PBEParameterSpec.
7. Encrypt the data with the Cipher.
8. Send the salt, unencrypted, and the output of the Cipher.

TO DECRYPT:
1. Take the String and create a PBEKeySpec.
2. Use a SecretKeyFactory to produce a SecretKey.
3. Use *THE SAME* iteration count.
4. Take the salt from the received block of bytes.
5. Create a PBEParameterSpec from the constant iteration count and the
received salt.
6. Create a Cipher from the SecretKey and PBEParameterSpec.
7. Decrypt the data with the Cipher.
8. Process the data.

Here's code for two classes that encrypt and decrypt, respectively,
files on disk. This code uses only algorithms that are part of Sun's
JCE provider. If your customer wants to use more modern encryption
algorithms, like AES, you will need to find and install another
provider.

CUT BELOW HERE FOR ENCRYPT.JAVA
import java.io.*;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;

public class Encrypt {
public static final void main(final String[] args) throws Exception {
if (args.length != 3) {
System.out.println("Syntax:");
System.out.println("java Encrypt <infile> <outfile> <password>");
return;
}

PBEKeySpec pbeks = new PBEKeySpec(args[2].toCharArray());
byte[] salt = new byte[8];
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
sr.nextBytes(salt);
PBEParameterSpec pbeps = new PBEParameterSpec(salt, 100);
SecretKeyFactory skf =
SecretKeyFactory.getInstance("PBEWithMD5AndTripleD ES");
SecretKey key = skf.generateSecret(pbeks);
Cipher c =
Cipher.getInstance("PBEWithMD5AndTripleDES/CBC/PKCS5Padding");
c.init(Cipher.ENCRYPT_MODE, key, pbeps);

FileInputStream fIn = new FileInputStream(args[0]);
FileOutputStream fOut = new FileOutputStream(args[1]);
fOut.write(salt);
byte[] buffer = new byte[4096];
int i;
while ((i = fIn.read(buffer)) != -1) {
fOut.write(c.update(buffer, 0, i));
}
fOut.write(c.doFinal());
fOut.close();
fIn.close();
}
}
CUT ABOVE HERE FOR ENCRYPT.JAVA

CUT BELOW HERE FOR DECRYPT.JAVA
import java.io.*;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;

public class Decrypt {
public static final void main(final String[] args) throws Exception {
if (args.length != 3) {
System.out.println("Syntax:");
System.out.println("java Decrypt <infile> <outfile> <password>");
return;
}

PBEKeySpec pbeks = new PBEKeySpec(args[2].toCharArray());

FileInputStream fIn = new FileInputStream(args[0]);
int i, total = 0;
byte[] salt = new byte[8];
while (total < salt.length) {
i = fIn.read(salt, total, salt.length - total);
if (i == -1) {
System.out.println("The file does not contain a full salt.");
return;
}
total += i;
}

PBEParameterSpec pbeps = new PBEParameterSpec(salt, 100);
SecretKeyFactory skf =
SecretKeyFactory.getInstance("PBEWithMD5AndTripleD ES");
SecretKey key = skf.generateSecret(pbeks);
Cipher c =
Cipher.getInstance("PBEWithMD5AndTripleDES/CBC/PKCS5Padding");
c.init(Cipher.DECRYPT_MODE, key, pbeps);

FileOutputStream fOut = new FileOutputStream(args[1]);
byte[] buffer = new byte[4096];
while ((i = fIn.read(buffer)) != -1) {
fOut.write(c.update(buffer, 0, i));
}
fOut.write(c.doFinal());
fOut.close();
fIn.close();
}
}
CUT ABOVE HERE FOR DECRYPT.JAVA

I think it should be trivial for you to integrate this code (which I
have tested) into your system.

- --
Chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQE/v3AVwxczzJRavJYRAijkAKC54pEX9nMaXCLraBmRKeFZ0T8LkA CeNOIq
SJ1SoLeOoDjhh2kZq416YMA=
=Avxc
-----END PGP SIGNATURE-----
Jul 17 '05 #2

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

Similar topics

10
by: Javier Gomez | last post by:
I have a table with 15.000 records. How can encrypt all information if after will shown in a form (text box)decryted ????? Thanks in advance Javier Gomez
14
by: msnews.microsoft.com | last post by:
How can I encrypt and decrypt string?
20
by: Drebin | last post by:
It's a long story really, but the bottom line is we need to encrypt or obfuscate a clear-text 9-digit SSN/taxpayer ID into something less than 21 characters. It doesn't need to be super-secure,...
2
by: Greg R | last post by:
Where can I find a code example that will demonstrate the encryption and decryption of a string in C# I am familiar with "System.Security.Cryptography" encryption, but need a system that will both...
1
by: DazedAndConfused | last post by:
Can you encrpt a serialized object? Or am I trying to do something that just doesn't work that way? I am trying to encrypt a serialized object. I can read and write the object to a file...
2
by: rino100 | last post by:
can anyone tell me why this c++ code works encrypting simple filenames but instead if you try to encrypt a filename like "video - 833 12_ ..avi" it doesn't rename the file?????? #include...
2
by: fineman | last post by:
Hi all, I want to get a 64bit(8 bytes) Encrypt result use DES class in the VS2005. Though I encrypt data is 64bit(8 bytes), but DES return encrypt result that always is 128bit(16 bytes), I don't...
3
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
by: lildiapaz | last post by:
Hi, everyone I'm developing a c# windows application that allows the user to encrypt any file type. i would like to encrypt the file using a powerful encrypting algorithm. I've tried to use the...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.