473,396 Members | 1,770 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 7927
-----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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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
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...
0
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,...

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.