Hi,
Thanks for your early reply. Yes, I want to store my password in database.
while retriving from the database it'll come from the decrypt mode and compare with current password. if both are equals then it will goes to the next page.
but initially I want to encrypt a
string and stored in database and decrypt it.
please find my code here.
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import javax.crypto.Cipher;
public class EncryptionRsa {
String userName ="Hello";
String password = "hello123";
byte [] encPassword = null;
String decPassword = null;
String pass = null;
byte[] ciphertextBytes = null;
byte[] textBytes = null;
public void encryptPass(String userName, String password, PublicKey pubKey) {
try {
password = userName+password;
Cipher encCipher = Cipher.getInstance("RSA");
encCipher.init(Cipher.ENCRYPT_MODE, pubKey);
encPassword = encCipher.doFinal(password.getBytes());
System.out.println("Encrypt Password: "+encPassword);
} catch(Exception e) {
e.printStackTrace();
}
}
public void decryptPass(String userName, String password, PrivateKey priKey) {
try {
textBytes ="hari".getBytes();
Cipher decCipher = Cipher.getInstance("RSA");
decCipher.init(Cipher.ENCRYPT_MODE, priKey);
textBytes = decCipher.doFinal(pass.getBytes());
System.out.println("Decrypt Password: "+decPassword);
} catch(Exception e) {
e.printStackTrace();
}
}
public KeyPair getPrivateKey() throws NoSuchAlgorithmException {
KeyPairGenerator keygen = KeyPairGenerator.getInstance("RSA");
KeyPair keypair = keygen.generateKeyPair();
PublicKey publicKey = keypair.getPublic();
PrivateKey privatekey = keypair.getPrivate();
return new KeyPair(publicKey, privatekey);
}
public static void main(String[] args) throws Exception {
EncryptionRsa encryptionRsa = new EncryptionRsa();
KeyPair kp=encryptionRsa.getPrivateKey();
encryptionRsa.encryptPass("Hello", "hari", kp.getPublic());
encryptionRsa.decryptPass("Hello", "hari", kp.getPrivate());
}
}
Please reply ASAP.
Thanks in advance.