Advertisement
anta40

AESDemo

Sep 27th, 2012
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.67 KB | None | 0 0
  1. /*
  2. Taken from: http://stackoverflow.com/questions/8731671/aesdecryption-in-blackberry
  3. */
  4.  
  5. import javax.crypto.*;
  6. import javax.crypto.spec.SecretKeySpec;
  7. import java.io.*;
  8. import java.security.NoSuchAlgorithmException;
  9. import java.security.MessageDigest;
  10. import java.security.*;
  11.  
  12. class AESDemo {
  13.     private static Cipher eCipher, dCipher;
  14.     private static byte[] buf = new byte[1024];
  15.    
  16.     private static byte[] getMD5(String input){
  17.         try {
  18.             byte[] bytesOfMessage = input.getBytes("UTF-8");
  19.             MessageDigest md = MessageDigest.getInstance("MD5");
  20.             return md.digest(bytesOfMessage);
  21.         }
  22.         catch (Exception e){
  23.             return null;
  24.         }
  25.     }
  26.    
  27.     private static void init_crypto(String key){
  28.         SecretKeySpec skey = new SecretKeySpec(getMD5(key), "AES");
  29.  
  30.         try {    
  31.             Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
  32.             eCipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
  33.             dCipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
  34.  
  35.             eCipher.init(Cipher.ENCRYPT_MODE, skey);
  36.             dCipher.init(Cipher.DECRYPT_MODE, skey);
  37.         }
  38.         catch (Exception e){
  39.             e.printStackTrace();
  40.         }
  41.     }
  42.  
  43.    
  44.  
  45.     public static void encrypt(File fin, File fout){
  46.        
  47.         try {
  48.                     InputStream in = new FileInputStream(fin);
  49.             OutputStream out = new FileOutputStream(fout);     
  50.             out = new CipherOutputStream(out, eCipher);
  51.  
  52.             int numRead = 0;
  53.             while ((numRead = in.read(buf)) >= 0){
  54.                 out.write(buf, 0, numRead);
  55.             }
  56.             out.close();
  57.         }
  58.         catch (IOException e){
  59.             e.printStackTrace();
  60.         }
  61.     }
  62.  
  63.  
  64.     public static void decrypt(File fin, File fout){
  65.  
  66.         try {
  67.             InputStream in = new FileInputStream(fin);
  68.             OutputStream out = new FileOutputStream(fout);
  69.             in = new CipherInputStream(in, dCipher);
  70.  
  71.             int numRead = 0;
  72.             while ((numRead = in.read(buf)) >= 0) {
  73.                 out.write(buf, 0, numRead);
  74.             }
  75.             out.close();
  76.         }
  77.         catch (IOException e) {
  78.             e.printStackTrace();
  79.         }
  80.     }
  81.  
  82.     public static byte[] readFile(File file) throws IOException {
  83.  
  84.         byte []buffer = new byte[(int) file.length()];
  85.         InputStream ios = null;
  86.         try {
  87.             ios = new FileInputStream(file);
  88.             if ( ios.read(buffer) == -1 ) {
  89.                 throw new IOException("EOF reached while trying to read the whole file");
  90.             }        
  91.         }
  92.         finally {
  93.             try {
  94.                 if ( ios != null ) ios.close();
  95.             }
  96.             catch ( IOException e) {
  97.             }
  98.         }
  99.  
  100.         return buffer;
  101.     }
  102.  
  103.     public static void main(String args[]) throws IOException, Exception{
  104.         String mode = args[0];
  105.         init_crypto("Hello world");
  106.    
  107.         if (mode.equals("-e")) encrypt(new File(args[1]), new File(args[2]));
  108.         else if (mode.equals("-d")) decrypt(new File(args[1]), new File(args[2]));
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement