| 1 | """ |
|---|
| 2 | Encryption module that uses the Java Cryptography Extensions (JCE). |
|---|
| 3 | |
|---|
| 4 | Note that in default installations of the Java Runtime Environment, the |
|---|
| 5 | maximum key length is limited to 128 bits due to US export |
|---|
| 6 | restrictions. This makes the generated keys incompatible with the ones |
|---|
| 7 | generated by pycryptopp, which has no such restrictions. To fix this, |
|---|
| 8 | download the "Unlimited Strength Jurisdiction Policy Files" from Sun, |
|---|
| 9 | which will allow encryption using 256 bit AES keys. |
|---|
| 10 | """ |
|---|
| 11 | from javax.crypto import Cipher |
|---|
| 12 | from javax.crypto.spec import SecretKeySpec, IvParameterSpec |
|---|
| 13 | |
|---|
| 14 | import jarray |
|---|
| 15 | |
|---|
| 16 | # Initialization vector filled with zeros |
|---|
| 17 | _iv = IvParameterSpec(jarray.zeros(16, 'b')) |
|---|
| 18 | |
|---|
| 19 | def aesEncrypt(data, key): |
|---|
| 20 | cipher = Cipher.getInstance('AES/CTR/NoPadding') |
|---|
| 21 | skeySpec = SecretKeySpec(key, 'AES') |
|---|
| 22 | cipher.init(Cipher.ENCRYPT_MODE, skeySpec, _iv) |
|---|
| 23 | return cipher.doFinal(data).tostring() |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | def getKeyLength(): |
|---|
| 27 | maxlen = Cipher.getMaxAllowedKeyLength('AES/CTR/NoPadding') |
|---|
| 28 | return min(maxlen, 256) / 8 |
|---|