I am new to cryptography. I wish to learn how to encrypt and decrypt the text in a file... when I refer the related articles in net. I had a doubt that whether the encrypted text will be same for single text when encryption is done multiple times on the same text? Can anyone please clear my doubt?
-
3possible duplicate of [How to encrypt String in Java](http://stackoverflow.com/questions/1205135/how-to-encrypt-string-in-java) – jmj Dec 20 '10 at 07:03
-
http://www.rgagnon.com/javadetails/java-0400.html – jmj Dec 20 '10 at 07:04
3 Answers
public String encrypt(String str) {
try {
// Encode the string into bytes using utf-8
byte[] utf8 = str.getBytes("UTF8");
// Encrypt
byte[] enc = ecipher.doFinal(utf8);
// Encode bytes to base64 to get a string
return new sun.misc.BASE64Encoder().encode(enc);
} catch (javax.crypto.BadPaddingException e) {
} catch (IllegalBlockSizeException e) {
} catch (UnsupportedEncodingException e) {
} catch (java.io.IOException e) {
}
return null;
}
public String decrypt(String str) {
try {
// Decode base64 to get bytes
byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
// Decrypt
byte[] utf8 = dcipher.doFinal(dec);
// Decode using utf-8
return new String(utf8, "UTF8");
} catch (javax.crypto.BadPaddingException e) {
} catch (IllegalBlockSizeException e) {
} catch (UnsupportedEncodingException e) {
} catch (java.io.IOException e) {
}
return null;
}
}
Here's an example that uses the class:
try {
// Generate a temporary key. In practice, you would save this key.
// See also Encrypting with DES Using a Pass Phrase.
SecretKey key = KeyGenerator.getInstance("DES").generateKey();
// Create encrypter/decrypter class
DesEncrypter encrypter = new DesEncrypter(key);
// Encrypt
String encrypted = encrypter.encrypt("Don't tell anybody!");
// Decrypt
String decrypted = encrypter.decrypt(encrypted);
} catch (Exception e) {
}

- 11,534
- 22
- 69
- 99
-
1
-
@user775 : the method getInstance(String algorithm) of the javax.crypto.KeyGenerator class, returns javax.crypto.KeyGenerator objects for the algorithm. And a java.security.Key that is a valid DES algorithm encryption key. – Pratik Dec 08 '11 at 08:38
-
12
-
6
-
1ecipher and dcipher appear to be instances of javax.crypto.Cipher: http://docs.oracle.com/javase/8/docs/api/javax/crypto/Cipher.html – Bill Horvath Jul 09 '15 at 18:38
-
What values ecipher and dciphers are supposed to hold? At least explain it's not so much of a bother, – Anurag Awasthi Oct 22 '17 at 08:45
-
-
This answer is very unclear. Please tell what are the classes that need to be imported. Also add a fully working code. – Prasanth Ganesan Sep 10 '19 at 11:18
I had a doubt that whether the encrypted text will be same for single text when encryption done by multiple times on a same text??
This depends strongly on the crypto algorithm you use:
- One goal of some/most (mature) algorithms is that the encrypted text is different when encryption done twice. One reason to do this is, that an attacker how known the plain and the encrypted text is not able to calculate the key.
- Other algorithm (mainly one way crypto hashes) like MD5 or SHA based on the fact, that the hashed text is the same for each encryption/hash.

- 118,862
- 56
- 287
- 383
Whether encrypted be the same when plain text is encrypted with the same key depends of algorithm and protocol. In cryptography there is initialization vector IV: http://en.wikipedia.org/wiki/Initialization_vector that used with various ciphers makes that the same plain text encrypted with the same key gives various cipher texts.
I advice you to read more about cryptography on Wikipedia, Bruce Schneier http://www.schneier.com/books.html and "Beginning Cryptography with Java" by David Hook. The last book is full of examples of usage of http://www.bouncycastle.org library.
If you are interested in cryptography the there is CrypTool: http://www.cryptool.org/ CrypTool is a free, open-source e-learning application, used worldwide in the implementation and analysis of cryptographic algorithms.

- 53,067
- 18
- 70
- 114
-
Thank you sir very much for your reply and guidance sir.. i ill read those specific book.. – deepa Dec 20 '10 at 11:53