encryption and decryption

Ads
 

encryption and decryption

View Answers

October 26, 2009 at 10:01 AM

Hi Friend,

Try the following code:

import java.io.*;
import java.security.*;
import javax.crypto.*;

class EncryptAndDecrypt {

public static void main (String[] args) throws Exception{
KeyPairGenerator keygenerator = KeyPairGenerator.getInstance("RSA");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");
keygenerator.initialize(1024, random);

KeyPair keypair = keygenerator.generateKeyPair();
PrivateKey privateKey = keypair.getPrivate();
PublicKey publicKey = keypair.getPublic();
Cipher cipher = Cipher.getInstance("RSA");

cipher.init(Cipher.ENCRYPT_MODE, publicKey);
String st= "roseindia";
byte[] cleartext = null;
cleartext = st.getBytes();
byte[] ciphertext = null;
ciphertext = cipher.doFinal(cleartext);
System.out.println("the encrypted text is: " + ciphertext.toString());

cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] cleartext1 = cipher.doFinal(ciphertext);
System.out.println("the decrypted cleartext is: " + new String(cleartext1));
}
}

Thanks

September 22, 2011 at 4:09 PM

import java.security.*; import javax.crypto.*; import javax.crypto.spec.DESKeySpec;

class EncryptAndDecrypt {

public static void main (String[] args) throws Exception{ //KeyPairGenerator keygenerator = KeyPairGenerator.getInstance("RC4"); SecretKeyFactory key = SecretKeyFactory.getInstance("DES"); SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN"); //keygenerator.initialize(1024, random); byte[] desKeyData = { (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04, (byte)0x05, (byte)0x06, (byte)0x07, (byte)0x08 }; DESKeySpec desKeySpec = new DESKeySpec(desKeyData);

//KeyPair keypair = keygenerator.generateKeyPair(); Key keys = key.generateSecret(desKeySpec); //PrivateKey privateKey = keypair.getPrivate(); //PublicKey publicKey = keypair.getPublic();

Cipher cipher = Cipher.getInstance("DES");

System.out.println("publicKey="+keys.toString()); //System.out.println("privateKey="+privateKey.toString());

cipher.init(Cipher.ENCRYPT_MODE, keys); String st= "Welcome"; byte[] cleartext = null; cleartext = st.getBytes(); byte[] ciphertext = null; ciphertext = cipher.doFinal(cleartext); System.out.println("the encrypted text is: " + ciphertext.toString());

cipher.init(Cipher.DECRYPT_MODE, keys); byte[] cleartext1 = cipher.doFinal(ciphertext); System.out.println("the decrypted cleartext is: " + new String(cleartext1)); } }


May 22, 2012 at 12:30 PM

http://www.roseindia.net/answers/viewqa/Java-Beginners/26017-Java-Encryption-using-AES-with-key-password-.html

http://www.roseindia.net/answers/viewqa/Java-Beginners/26020-AES-Decryption-using-key-password-.html

Ads









Related Tutorials/Questions & Answers:
encryption and decryption
encryption and decryption  how to encrypt and decrypt data in text file
encryption and decryption
encryption and decryption   ASCII encryptor I want to develop a program that encrypt and decrypt. The messages stored in a file should.... This is called encryption. When one wants to read the contents of the file
Advertisements
encryption and decryption
encryption and decryption   ASCII encryptor I want to develop a program that encrypt and decrypt. The messages stored in a file should.... This is called encryption. When one wants to read the contents of the file
encryption and decryption
encryption and decryption   ASCII encryptor I want to develop a program that encrypt and decrypt. The messages stored in a file should.... This is called encryption. When one wants to read the contents of the file
encryption and decryption
encryption and decryption   ASCII encryptor I want to develop a program that encrypt and decrypt. The messages stored in a file should.... This is called encryption. When one wants to read the contents of the file
Encryption Decryption
Encryption Decryption  I want to store a type string to my database, specifically MSSQL, I want the stored data in the database to be encrypted and I want to retrieve it in it's decrypted format. I am using JSP's, I want
encryption and decryption of files
encryption and decryption of files   Please can any one provide me with the code for encryption and decryption of files using RSA algorithm. I want to use a browse option to search for the file which is needed for encryption
servelt -jsp Encryption Decryption Code
servelt -jsp Encryption Decryption Code  Encryption code&Decryption code Iam developing a web security application for that reason i need a code of encryption &Decryption in servlet. Can u please send it to me.Its very
Encryption Decryption In Scriptlets/Java
Encryption Decryption In Scriptlets/Java  I have this code in my scriptlet, when I use the Encryption code to encrypt my data it executes... where the program stopped working and it stopped in the decryption part and I get
Password encryption and decryption
Password encryption and decryption  Hello, I'm developing a system that requires user to login to enter the system. so I wanted to store encrypted users' password in the database so that I wouldnt know their password
Sample code for An ASCII Encryption and Decryption code in C-language only.
Sample code for An ASCII Encryption and Decryption code in C-language only... code for this project including both the encryption and decryption aspects... stored in a file. This is called encryption. When one wants to read the contents
AES decryption in Java
AES decryption in Java   AES decryption in Java with password...-Decryption-using-key-password-.html    http://www.roseindia.net/answers/viewqa/Java-Beginners/26020-AES-Decryption-using-key-password-.html   
sound encryption
sound encryption  i want to ask if there is a simple code to perform encryption on sound file
AES encryption in J2ME
AES encryption in J2ME  Is it possible to do aes encryption in j2me
ModuleNotFoundError: No module named 'Encryption'
ModuleNotFoundError: No module named 'Encryption'  Hi, My Python... 'Encryption' How to remove the ModuleNotFoundError: No module named 'Encryption' error? Thanks   Hi, In your python environment you
Version of com.braintreepayments>encryption dependency
List of Version of com.braintreepayments>encryption dependency
AES Decryption using key password
AES Decryption using key password    /* Decrypt using AES with password */ /* developed by Nishanth Thomas - Insolutions Global Pvt Ltd Bangalore */ import java.security.spec.AlgorithmParameterSpec; import
ModuleNotFoundError: No module named 'aws-encryption'
ModuleNotFoundError: No module named 'aws-encryption'  Hi, My... named 'aws-encryption' How to remove the ModuleNotFoundError: No module named 'aws-encryption' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'aws-encryption'
ModuleNotFoundError: No module named 'aws-encryption'  Hi, My... named 'aws-encryption' How to remove the ModuleNotFoundError: No module named 'aws-encryption' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'aws-encryption'
ModuleNotFoundError: No module named 'aws-encryption'  Hi, My... named 'aws-encryption' How to remove the ModuleNotFoundError: No module named 'aws-encryption' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'cred-encryption'
ModuleNotFoundError: No module named 'cred-encryption'  Hi, My... named 'cred-encryption' How to remove the ModuleNotFoundError: No module named 'cred-encryption' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'cred-encryption'
ModuleNotFoundError: No module named 'cred-encryption'  Hi, My... named 'cred-encryption' How to remove the ModuleNotFoundError: No module named 'cred-encryption' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'cred-encryption'
ModuleNotFoundError: No module named 'cred-encryption'  Hi, My... named 'cred-encryption' How to remove the ModuleNotFoundError: No module named 'cred-encryption' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'dynamodb-encryption'
ModuleNotFoundError: No module named 'dynamodb-encryption'  Hi, My... named 'dynamodb-encryption' How to remove the ModuleNotFoundError: No module named 'dynamodb-encryption' error? Thanks   Hi
ModuleNotFoundError: No module named 'dynamodb-encryption'
ModuleNotFoundError: No module named 'dynamodb-encryption'  Hi, My... named 'dynamodb-encryption' How to remove the ModuleNotFoundError: No module named 'dynamodb-encryption' error? Thanks   Hi
ModuleNotFoundError: No module named 'py-encryption'
ModuleNotFoundError: No module named 'py-encryption'  Hi, My... 'py-encryption' How to remove the ModuleNotFoundError: No module named 'py-encryption' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'ska-encryption'
ModuleNotFoundError: No module named 'ska-encryption'  Hi, My... named 'ska-encryption' How to remove the ModuleNotFoundError: No module named 'ska-encryption' error? Thanks   Hi, In your python
Maven Dependency encryption >> 2.0.0
You should include the dependency code given in this page to add Maven Dependency of com.braintreepayments >> encryption version2.0.0 in your project
Maven Dependency encryption >> 2.1.0
You should include the dependency code given in this page to add Maven Dependency of com.braintreepayments >> encryption version2.1.0 in your project
ModuleNotFoundError: No module named 'aws-encryption-cli'
ModuleNotFoundError: No module named 'aws-encryption-cli'  Hi, My... named 'aws-encryption-cli' How to remove the ModuleNotFoundError: No module named 'aws-encryption-cli' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'aws-encryption-sdk'
ModuleNotFoundError: No module named 'aws-encryption-sdk'  Hi, My... named 'aws-encryption-sdk' How to remove the ModuleNotFoundError: No module named 'aws-encryption-sdk' error? Thanks   Hi, In your

Ads