AESECB.java
package bsr;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
class AESECB
{
public KeyGenerator keygenerator;
public SecretKey myDesKey;
Cipher c;
public AESECB() throws Exception
{
// Genrate the Key
keygenerator = KeyGenerator.getInstance("AES");
myDesKey = keygenerator.generateKey();
// Create the cipher
c = Cipher.getInstance("AES/ECB/PKCS5Padding");
}
public byte[] doEncryption(String s) throws Exception
{
// Initialize the cipher for encryption
c.init(Cipher.ENCRYPT_MODE, myDesKey);
//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);
// Decrypt the text
byte[] textDecrypted = c.doFinal(s);
return(new String(textDecrypted));
}
}
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
class AESECB
{
public KeyGenerator keygenerator;
public SecretKey myDesKey;
Cipher c;
public AESECB() throws Exception
{
// Genrate the Key
keygenerator = KeyGenerator.getInstance("AES");
myDesKey = keygenerator.generateKey();
// Create the cipher
c = Cipher.getInstance("AES/ECB/PKCS5Padding");
}
public byte[] doEncryption(String s) throws Exception
{
// Initialize the cipher for encryption
c.init(Cipher.ENCRYPT_MODE, myDesKey);
//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);
// Decrypt the text
byte[] textDecrypted = c.doFinal(s);
return(new String(textDecrypted));
}
}
AES.java
import bsr.*;
public class AES
{
public static void main(String[] argv) throws Exception
{
AESECB d=new AESECB();
byte[] str=d.doEncryption("BipinRupadiya");
System.out.println("Encrypted String : "+str);
System.out.println("Encrypted String : "+d.doDecryption(str));
}
}
public class AES
{
public static void main(String[] argv) throws Exception
{
AESECB d=new AESECB();
byte[] str=d.doEncryption("BipinRupadiya");
System.out.println("Encrypted String : "+str);
System.out.println("Encrypted String : "+d.doDecryption(str));
}
}
To execute this code
- Open command prompt
- Compile bsr package
- go to bsr directory
- javac *.java
- Compile AES.java
- javac AES.java
- Execute AES.java
- java AES
No comments:
Post a Comment