Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static byte[] AES_decrypt(byte[] inputData) throws CryptoException, IOException {
- byte[] keyData = "Hello world".getBytes();
- AESKey key = new AESKey(keyData, 0, 256);
- AESDecryptorEngine engine = new AESDecryptorEngine(key);
- PKCS5UnformatterEngine uengine = new PKCS5UnformatterEngine(engine);
- ByteArrayInputStream input = new ByteArrayInputStream(inputData);
- BlockDecryptor decryptor = new BlockDecryptor(uengine, input);
- byte[] plaintextAndHash = new byte[1024];
- ByteArrayOutputStream output = new ByteArrayOutputStream();
- int bytesRead=0;
- do {
- bytesRead =decryptor.read(plaintextAndHash);
- if(bytesRead!=-1){
- output.write(plaintextAndHash,0,bytesRead);
- }
- } while ( bytesRead != -1 );
- return output.toByteArray();
- }
- public static byte[] AES_decrypt2(byte[] inputData) throws CryptoException, IOException {
- byte[] keyData = calculateMD5("Hello world");
- AESKey aesKey = new AESKey(keyData);
- AESDecryptorEngine engine = new AESDecryptorEngine(aesKey);
- PKCS5UnformatterEngine uengine = new PKCS5UnformatterEngine(engine);
- ByteArrayInputStream istream = new ByteArrayInputStream(inputData);
- BlockDecryptor decryptor = new BlockDecryptor(uengine, istream);
- byte[] temp = new byte[100];
- DataBuffer dbuff = new DataBuffer();
- int bytesRead = 0;
- for (;;){
- bytesRead = decryptor.read(temp);
- dbuff.write(temp, 0, bytesRead);
- if (bytesRead < 100) break;
- }
- byte[] plainData = dbuff.getArray();
- int plainDataLength = plainData.length - SHA1Digest.DIGEST_LENGTH;
- byte[] decryptedData = new byte[plainDataLength];
- byte[] hash = new byte[SHA1Digest.DIGEST_LENGTH];
- System.arraycopy(plainData, 0, decryptedData, 0, plainDataLength);
- System.arraycopy(plainData, plainDataLength, hash, 0, SHA1Digest.DIGEST_LENGTH);
- SHA1Digest digest = new SHA1Digest();
- digest.update(plainData);
- byte[] hash2 = digest.getDigest();
- if (!Arrays.equals(hash, hash2)) throw new RuntimeException();
- return decryptedData;
- }
- private static byte[] calculateMD5(String input){
- byte[] plain = input.getBytes();
- MD5 md5 = new MD5(plain);
- return md5.doFinal();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement