1 package org
.pmwiki
.cookbook
.aescrypt
;
4 import javax
.crypto
.Cipher
;
5 import javax
.crypto
.spec
.IvParameterSpec
;
6 import javax
.crypto
.spec
.SecretKeySpec
;
7 import org
.apache
.commons
.codec
.binary
.Base64
;
8 import org
.apache
.commons
.codec
.digest
.DigestUtils
;
11 * To change this template, choose Tools | Templates
12 * and open the template in the editor.
21 public class Decrypt
{
23 public static void main(String
[] args
)throws Exception
25 String password
= "TopSecret";
26 //String encrypted = "AAAAAAAAAABHtF/GUuR5r+R8gBpKzVQv3FT2osanEmZD1DvoR3m3sQ==";
27 String encrypted
= "AAAAAAAAAABHtF8=";
29 byte dataIn
[] = Base64
.decodeBase64(encrypted
);
30 byte nonce
[] = new byte[16];
31 byte enc
[] = new byte[dataIn
.length
- 8];
33 System
.arraycopy(dataIn
, 0, nonce
, 0, 8);
34 System
.arraycopy(dataIn
, 8, enc
, 0, dataIn
.length
- 8);
36 byte[] keyData
= DigestUtils
.sha256(password
);
37 SecretKeySpec key
= new SecretKeySpec(keyData
, "AES");
39 Cipher cipher
= Cipher
.getInstance("AES/CTR/NoPadding");
40 IvParameterSpec ivSpec
= new IvParameterSpec(nonce
);
41 cipher
.init(Cipher
.DECRYPT_MODE
, key
, ivSpec
);
42 byte[] resultData
= cipher
.doFinal(enc
);
43 System
.out
.println(new String(resultData
));