Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

Please comment link or provide sample code file encryption and decryption in java net beans.

Thanks in advance.
Posted
Comments
Kornfeld Eliyahu Peter 22-Jun-14 5:29am    
www.google.com
normalsoft 22-Jun-14 5:34am    
lol. but getting only eclipse. not found any net beans code.
Kornfeld Eliyahu Peter 22-Jun-14 5:36am    
What the difference - both are Java!!!
normalsoft 22-Jun-14 5:47am    
Sorry im new in java. this line got error in my application.
byte [] base64Bytes = Base64.encodeBase64(buf);

encodeBase64 is underlined by red mark.

You probably missing some import of the library contains the Base64 functionality...one option is...
Java
import org.apache.commons.codec.binary.Base64;

...and alter in your code...
Java
byte [] base64Bytes = Base64.encodeBase64(buf); 
 
Share this answer
 
Comments
normalsoft 22-Jun-14 6:30am    
import org.apache.commons.codec.binary.Base64;

This libray is not getting. im using netbeans.
Kornfeld Eliyahu Peter 22-Jun-14 6:40am    
You have to add that library...
Download it here: http://commons.apache.org/
Than open NetBeans documentation to see how to add external JAR to your project...
normalsoft 22-Jun-14 6:55am    
Thanks bro. I do the same. jar file added to project and build. Now showing
"can not find symbol." Method encodeBase64(..)
Kornfeld Eliyahu Peter 22-Jun-14 7:11am    
What are you passing to encodeBase64 method? Let see us the code in context...
normalsoft 22-Jun-14 7:16am    
private String _encrypt(String message, String secretKey) throws Exception {

MessageDigest md = MessageDigest.getInstance("SHA-1");
byte[] digestOfPassword = md.digest(secretKey.getBytes("utf-8"));
byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);

SecretKey key = new SecretKeySpec(keyBytes, "DESede");
Cipher cipher = Cipher.getInstance("DESede");
cipher.init(Cipher.ENCRYPT_MODE, key);

byte[] plainTextBytes = message.getBytes("utf-8");
byte[] buf = cipher.doFinal(plainTextBytes);
byte[] base64Bytes = Base64.encodeBase64(buf);
// byte [] base64Bytes = Base64.;
String base64EncryptedString = new String(base64Bytes);

return base64EncryptedString;
}

private String _decrypt(String encryptedText, String secretKey) throws Exception {

byte[] message = Base64.decodeBase64(encryptedText.getBytes("utf-8"));

MessageDigest md = MessageDigest.getInstance("SHA-1");
byte[] digestOfPassword = md.digest(secretKey.getBytes("utf-8"));
byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
SecretKey key = new SecretKeySpec(keyBytes, "DESede");

Cipher decipher = Cipher.getInstance("DESede");
decipher.init(Cipher.DECRYPT_MODE, key);

byte[] plainText = decipher.doFinal(message);

return new String(plainText, "UTF-8");
}
Google is your friend: Be nice and visit him often. He can answer questions a lot more quickly than posting them here...

A very quick search gave over 60,000 hits: Google[^]

In future, please try to do at least basic research yourself, and not waste your time or ours.
 
Share this answer
 
Comments
Kornfeld Eliyahu Peter 22-Jun-14 5:55am    
Do you have a filter on your Google? I got 70,700 with your link :-). Beat you!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900