3DES with CBC mode example in Java
D3ESCBC.java
package bsr;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
class D3ESCBC
{
public KeyGenerator keygenerator;
public SecretKey myDesKey;
Cipher c;
public IvParameterSpec iv;
public D3ESCBC() throws Exception
{
// Genrate the Key
keygenerator = KeyGenerator.getInstance("DESede");
keygenerator.init(new SecureRandom());
myDesKey = keygenerator.generateKey();
// Create the cipher
c = Cipher.getInstance("DESede/CBC/PKCS5Padding");
iv = new IvParameterSpec(new byte[8]);
}
public byte[] doEncryption(String s) throws Exception
{
// Initialize the cipher for encryption
c.init(Cipher.ENCRYPT_MODE, myDesKey,iv);
//sensitive information
byte[] text = s.getBytes();
// Encrypt the text
byte[] textEncrypted = c.doFinal(text);
return(textEncrypted);
}
public String doDecryption(byte[] s)throws Exception
{
// Initialize the same cipher for decryption
c.init(Cipher.DECRYPT_MODE, myDesKey,iv);
// Decrypt the text
byte[] textDecrypted = c.doFinal(s);
return(new String(textDecrypted));
}
}
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
class D3ESCBC
{
public KeyGenerator keygenerator;
public SecretKey myDesKey;
Cipher c;
public IvParameterSpec iv;
public D3ESCBC() throws Exception
{
// Genrate the Key
keygenerator = KeyGenerator.getInstance("DESede");
keygenerator.init(new SecureRandom());
myDesKey = keygenerator.generateKey();
// Create the cipher
c = Cipher.getInstance("DESede/CBC/PKCS5Padding");
iv = new IvParameterSpec(new byte[8]);
}
public byte[] doEncryption(String s) throws Exception
{
// Initialize the cipher for encryption
c.init(Cipher.ENCRYPT_MODE, myDesKey,iv);
//sensitive information
byte[] text = s.getBytes();
// Encrypt the text
byte[] textEncrypted = c.doFinal(text);
return(textEncrypted);
}
public String doDecryption(byte[] s)throws Exception
{
// Initialize the same cipher for decryption
c.init(Cipher.DECRYPT_MODE, myDesKey,iv);
// Decrypt the text
byte[] textDecrypted = c.doFinal(s);
return(new String(textDecrypted));
}
}
D3ES.java
import bsr.*;
public class D3ES
{
public static void main(String[] argv) throws Exception
{
D3ESCBC d=new D3ESCBC();
String str=d.doEncryption("BipinRupadiya");;
System.out.println("Encrypted String : "+str);
System.out.println("Encrypted String : "+d.doDecryption(str));
}
}
public class D3ES
{
public static void main(String[] argv) throws Exception
{
D3ESCBC d=new D3ESCBC();
String str=d.doEncryption("BipinRupadiya");;
System.out.println("Encrypted String : "+str);
System.out.println("Encrypted String : "+d.doDecryption(str));
}
}
To execute this code
- open two command prompt
- compile bsr package
- compile D3ES.java
- execute D3ES.java
Subscribe to:
Post Comments (Atom)
Subjects
- WordPress
- Mobile Computing-4649303 Practical Solution
- Android Programming New Syllabus Theory
- PHP LAMP Question Bank
- PHP LAMP Theory
- Step by Step Android Example
- Android Practical
- Android Theory
- Android Question Bank
- Networking FON Practical
- Networking FON Theory
- OS Practical
- OS Theory
- HTML
- JavaScript
- J2EE WTAD Theory
- J2EE WTAD Question Bank
- J2EE WTAD Quick Guide
- J2EE WTAD GTU Papers
- J2EE WTAD Practical
- Python
- JAVA Theory
- JAVA Practical
- MIS
Categories
- Android (55)
- c (11)
- Configure Tomcat7 (2)
- CSS (3)
- Decryption (16)
- Difference (1)
- Encryption (16)
- Error Detection and Correction Techniques (3)
- FON (27)
- Framing Technic (2)
- install Tomcat (2)
- J2EE (29)
- JAVA (13)
- JavaScript (19)
- linux (8)
- OS (17)
- PHP (11)
- Protocol (3)
- SERVER SOCKET PROGRAMING (7)
- Servlet (13)
- shell script (33)
- unix (22)
- WTAD (34)
Blog Archive
-
▼
2013
(64)
-
▼
June
(25)
- Example of command line argument in C on Linux
- Example of command line argument in Java
- AES with CBC mode example in Java
- AES with CFM mode example in Java
- AES with ECB mode example in Java
- 3DES with CFM mode example in Java
- 3DES with CBC mode example in Java
- 3DES with ECB mode example in Java
- RSA Encryption Example in Java
- JSP JDBC Example
- example of preparedstatement in java
- example of page directive in jsp
- example of jsp scripting elements
- Example of requestdispatcher
- JSTL standard tag library Example
- DES with CBC mode example in Java
- DES with CFM mode example in Java
- DES with ECB mode example in Java
- One Time Pad Encryption Example using Java code
- S-Box Example using Java
- P-Box Example using Java
- Transposition cipher example using Java
- Mono-alphabetic Substitution Cipher example using ...
- Generalized Caesar Cipher example using java
- java socket programming example
-
▼
June
(25)
Total Pageviews
© BipinRupadiya.com. Powered by Blogger.
3 comments:
thank you very much! Help me understand M3ES a lot.
String str=d.doEncryption("BipinRupadiya");;
Besides the two ;
Yo don't have any method that recieve String and returns String.
The same here:
System.out.println("Encrypted String : "+d.doDecryption(str));
Thanks for posting this information it really useful for everyone.
French Classes in Chennai
french courses in chennai
Spoken English Classes in Chennai
TOEFL Coaching in Chennai
pearson vue test center in chennai
German Language Course in Chennai
French Classes in T Nagar
French Classes in Anna Nagar
Post a Comment