Advertisement
anta40

play

Sep 27th, 2012
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.29 KB | None | 0 0
  1. public static byte[] AES_decrypt(byte[] inputData) throws CryptoException, IOException {
  2.        
  3.         byte[] keyData = "Hello world".getBytes();
  4.         AESKey key = new AESKey(keyData, 0, 256);
  5.  
  6.         AESDecryptorEngine engine = new AESDecryptorEngine(key);
  7.         PKCS5UnformatterEngine uengine = new PKCS5UnformatterEngine(engine);
  8.  
  9.         ByteArrayInputStream input = new ByteArrayInputStream(inputData);
  10.         BlockDecryptor decryptor = new BlockDecryptor(uengine, input);
  11.         byte[] plaintextAndHash = new byte[1024];
  12.         ByteArrayOutputStream output = new ByteArrayOutputStream();
  13.         int bytesRead=0;
  14.        
  15.         do {
  16.             bytesRead =decryptor.read(plaintextAndHash);
  17.             if(bytesRead!=-1){
  18.                 output.write(plaintextAndHash,0,bytesRead);
  19.             }
  20.         } while ( bytesRead != -1 );
  21.  
  22.         return output.toByteArray();
  23.     }
  24.    
  25.     public static byte[] AES_decrypt2(byte[] inputData) throws CryptoException, IOException {
  26.         byte[] keyData = calculateMD5("Hello world");
  27.         AESKey aesKey = new AESKey(keyData);
  28.        
  29.         AESDecryptorEngine engine = new AESDecryptorEngine(aesKey);
  30.         PKCS5UnformatterEngine uengine = new PKCS5UnformatterEngine(engine);
  31.        
  32.         ByteArrayInputStream istream = new ByteArrayInputStream(inputData);
  33.         BlockDecryptor decryptor = new BlockDecryptor(uengine, istream);
  34.        
  35.         byte[] temp = new byte[100];
  36.         DataBuffer dbuff = new DataBuffer();
  37.         int bytesRead = 0;
  38.        
  39.         for (;;){
  40.             bytesRead = decryptor.read(temp);
  41.             dbuff.write(temp, 0, bytesRead);
  42.             if (bytesRead < 100) break;
  43.         }
  44.        
  45.         byte[] plainData = dbuff.getArray();
  46.         int plainDataLength = plainData.length - SHA1Digest.DIGEST_LENGTH;
  47.         byte[] decryptedData = new byte[plainDataLength];
  48.         byte[] hash = new byte[SHA1Digest.DIGEST_LENGTH];
  49.        
  50.         System.arraycopy(plainData, 0, decryptedData, 0, plainDataLength);
  51.         System.arraycopy(plainData, plainDataLength, hash, 0, SHA1Digest.DIGEST_LENGTH);
  52.        
  53.         SHA1Digest digest = new SHA1Digest();
  54.         digest.update(plainData);
  55.         byte[] hash2 = digest.getDigest();
  56.        
  57.         if (!Arrays.equals(hash, hash2)) throw new RuntimeException();
  58.        
  59.         return decryptedData;
  60.     }
  61.    
  62.     private static byte[] calculateMD5(String input){
  63.         byte[] plain = input.getBytes();
  64.         MD5 md5 = new MD5(plain);
  65.         return md5.doFinal();
  66.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement