our team encrypting data based on javascript code snippet follow page http://www.movable-type.co.uk/scripts/aes.html , here http://anh.cs.luc.edu/331/code/aes.py
now have decrypt data using java on mobile devices.
the page say:
the key in script obtained applying cipher routine encrypt first 16/24/32 characters of password (for 128-/192-/256-bit keys) make key. convenient way obtain secure key within entirely self-contained script (in production environment, opposed tutorial code, key might generated hash, e.g. key = sha256(password)). in more detail, supplied password converted to utf-8 (to byte-safe), first 16/24/32 characters converted bytes. resulting pwbytes used seed aes.keyexpansion() used key encrypt pwbytes aes.cipher(). examples of keys generated in way (unrealistically) simple `
so think got stuck @ generating key using password
here test case:
- the original text:
today
- the password:
aa2145f9e2a5daaa9c6a8ddc5f5c1a39
- the actuall result
j��
they don't use key twice, use random initialisation vector (iv) every time, hence results different.
java code:
private static string decrypt(secretkey aeskey, string encodedciphertext) { try { // that's no base 64, that's base 64 on utf-8 encoding of code points byte[] ciphertext = jsbase64decode(encodedciphertext); cipher aesctr = cipher.getinstance("aes/ctr/nopadding"); int n = aesctr.getblocksize(); byte[] counter = new byte[n]; int noncesize = n / 2; system.arraycopy(ciphertext, 0, counter, 0, noncesize); ivparameterspec iv = new ivparameterspec(counter); aesctr.init(cipher.decrypt_mode, aeskey, iv); byte[] plaintext = aesctr.dofinal(ciphertext, noncesize, ciphertext.length - noncesize); return new string(plaintext, "utf-8"); // that's no base 64, that's base 64 on utf-8 encoding of code points } catch (exception e) { e.printstacktrace(); } return ""; } private static byte[] jsbase64decode(string encodedciphertext) { byte[] ciphertext = null; try { byte[] utf8ct = base64.decode(encodedciphertext); string cts = new string(utf8ct, "utf-8"); ciphertext = new byte[cts.length()]; (int = 0; < cts.length(); i++) { ciphertext[i] = (byte) (cts.charat(i) & 0xff); } }catch (exception e) { e.printstacktrace(); } //arrays.copyofrange(new byte[100], 0, 99); return ciphertext; } // should not singleton lazybones, may contain state private static secretkey derivekey(string password, int nbits) throws charactercodingexception { try { charset charset = charset.forname("utf-8"); charsetencoder encoder = charset.newencoder(); bytebuffer buf = encoder.encode(charbuffer.wrap(password)); //byte[] buf1 = password.getbytes(); int nbytes = nbits / byte.size; // bits / byte.size; cipher aesecb = cipher.getinstance("aes/ecb/nopadding"); int n = aesecb.getblocksize(); byte[] pwbytes = new byte[nbytes]; // use characters fit in nbytes! oops! buf.get(pwbytes, 0, buf.remaining()); //pwbytes = password.getbytes("utf-8"); secretkey derivationkey = new secretkeyspec(pwbytes, "aes"); aesecb.init(cipher.encrypt_mode, derivationkey); // , although derivationkey nbytes in size, encrypt 16 (the block size) byte[] partialkey = aesecb.dofinal(pwbytes, 0, n); byte[] key = new byte[nbytes]; system.arraycopy(partialkey, 0, key, 0, n); // have few *copy* key bytes // increased number of rounds configured using nbits system.arraycopy(partialkey, 0, key, n, nbytes - n); secretkey derivatedkey = new secretkeyspec(key, "aes"); return derivatedkey; } catch (exception e) { throw new illegalstateexception("key derivation should finish", e); } } public static string main(){ secretkey key = null; try { key = derivekey("aa2145f9e2a5daaa9c6a8ddc5f5c1a39", 256); } catch (exception e) { e.printstacktrace(); } // ciphertext may vary in length depending on utf-8 encoding string pt = decrypt(key, "eqdh+srpqlbh7ml42g=="); return pt; }
i have ported myself. tks anyway.
post here want me
/** * created luu on 1/28/2016. */ public class aes { // sbox pre-computed multiplicative inverse in gf(2^8) used in subbytes , keyexpansion [§5.1.1] private static final int[] sbox = new int[]{ 0x63,0x7c,0x77,0x7b,0xf2,0x6b,0x6f,0xc5,0x30,0x01,0x67,0x2b,0xfe,0xd7,0xab,0x76, 0xca,0x82,0xc9,0x7d,0xfa,0x59,0x47,0xf0,0xad,0xd4,0xa2,0xaf,0x9c,0xa4,0x72,0xc0, 0xb7,0xfd,0x93,0x26,0x36,0x3f,0xf7,0xcc,0x34,0xa5,0xe5,0xf1,0x71,0xd8,0x31,0x15, 0x04,0xc7,0x23,0xc3,0x18,0x96,0x05,0x9a,0x07,0x12,0x80,0xe2,0xeb,0x27,0xb2,0x75, 0x09,0x83,0x2c,0x1a,0x1b,0x6e,0x5a,0xa0,0x52,0x3b,0xd6,0xb3,0x29,0xe3,0x2f,0x84, 0x53,0xd1,0x00,0xed,0x20,0xfc,0xb1,0x5b,0x6a,0xcb,0xbe,0x39,0x4a,0x4c,0x58,0xcf, 0xd0,0xef,0xaa,0xfb,0x43,0x4d,0x33,0x85,0x45,0xf9,0x02,0x7f,0x50,0x3c,0x9f,0xa8, 0x51,0xa3,0x40,0x8f,0x92,0x9d,0x38,0xf5,0xbc,0xb6,0xda,0x21,0x10,0xff,0xf3,0xd2, 0xcd,0x0c,0x13,0xec,0x5f,0x97,0x44,0x17,0xc4,0xa7,0x7e,0x3d,0x64,0x5d,0x19,0x73, 0x60,0x81,0x4f,0xdc,0x22,0x2a,0x90,0x88,0x46,0xee,0xb8,0x14,0xde,0x5e,0x0b,0xdb, 0xe0,0x32,0x3a,0x0a,0x49,0x06,0x24,0x5c,0xc2,0xd3,0xac,0x62,0x91,0x95,0xe4,0x79, 0xe7,0xc8,0x37,0x6d,0x8d,0xd5,0x4e,0xa9,0x6c,0x56,0xf4,0xea,0x65,0x7a,0xae,0x08, 0xba,0x78,0x25,0x2e,0x1c,0xa6,0xb4,0xc6,0xe8,0xdd,0x74,0x1f,0x4b,0xbd,0x8b,0x8a, 0x70,0x3e,0xb5,0x66,0x48,0x03,0xf6,0x0e,0x61,0x35,0x57,0xb9,0x86,0xc1,0x1d,0x9e, 0xe1,0xf8,0x98,0x11,0x69,0xd9,0x8e,0x94,0x9b,0x1e,0x87,0xe9,0xce,0x55,0x28,0xdf, 0x8c,0xa1,0x89,0x0d,0xbf,0xe6,0x42,0x68,0x41,0x99,0x2d,0x0f,0xb0,0x54,0xbb,0x16 }; // rcon round constant used key expansion [1st col 2^(r-1) in gf(2^8)] [§5.2] private static final int[][] rcon = new int[][]{ new int[]{0x00, 0x00, 0x00, 0x00},//0 new int[]{0x01, 0x00, 0x00, 0x00},//1 new int[]{0x02, 0x00, 0x00, 0x00},//2 new int[]{0x04, 0x00, 0x00, 0x00},//3 new int[]{0x08, 0x00, 0x00, 0x00},//4 new int[]{0x10, 0x00, 0x00, 0x00},//5 new int[]{0x20, 0x00, 0x00, 0x00},//6 new int[]{0x40, 0x00, 0x00, 0x00},//7 new int[]{0x80, 0x00, 0x00, 0x00},//8 new int[]{0x1b, 0x00, 0x00, 0x00},//9 new int[]{0x36, 0x00, 0x00, 0x00}//10 }; /** * aes cipher function: encrypt 'input' state rijndael algorithm [§5.1]; * applies nr rounds (10/12/14) using key schedule w 'add round key' stage. * * @param {number[]} input - 16-byte (128-bit) input state array. * @param {number[][]} w - key schedule 2d byte-array (nr+1 x nb bytes). * @returns {number[]} encrypted output state array. */ private static int[] cipher (int[] input, int[][] w) { int nb = 4; // block size (in words): no of columns in state (fixed @ 4 aes) int nr = w.length / nb - 1; // no of rounds: 10/12/14 128/192/256-bit keys int[][] state = new int[4][];// [[],[],[],[]]; // initialise 4xnb byte-array 'state' input [§3.4] (int = 0; < 4 * nb; i++) { if (state[i % 4] == null) state[i % 4] = new int[4]; state[i % 4][(int)math.floor((double) / 4)] = input[i]; } state = addroundkey(state, w, 0, nb); (int round = 1; round < nr; round++) { state = subbytes(state, nb); state = shiftrows(state, nb); state = mixcolumns(state, nb); state = addroundkey(state, w, round, nb); } state = subbytes(state, nb); state = shiftrows(state, nb); state = addroundkey(state, w, nr, nb); int[] output = new int[4 * nb]; // convert state 1-d array before returning [§3.4] (int = 0; < 4 * nb; i++) output[i] = state[i % 4][(int)math.floor((double) / 4)]; return output; }; /** * perform key expansion generate key schedule cipher key [§5.2]. * * @param {number[]} key - cipher key 16/24/32-byte array. * @returns {number[][]} expanded key schedule 2d byte-array (nr+1 x nb bytes). */ private static int[][] keyexpansion (int[] key) { int nb = 4; // block size (in words): no of columns in state (fixed @ 4 aes) int nk = key.length / 4; // key length (in words): 4/6/8 128/192/256-bit keys int nr = nk + 6; // no of rounds: 10/12/14 128/192/256-bit keys int[][] w = new int[nb * (nr + 1)][nb]; int[] temp = new int[4]; // initialise first nk words of expanded key cipher key (int = 0; < nk; i++) { int[] r = new int[] { key[4 * i], key[4 * + 1], key[4 * + 2], key[4 * + 3] }; w[i] = r; } // expand key remainder of schedule (int = nk; < (nb * (nr + 1)); i++) { w[i] = new int[4]; (int t = 0; t < 4; t++) temp[t] = w[i - 1][t]; // each nk'th word has transformation if (i % nk == 0) { temp = subword(rotword(temp)); (int t = 0; t < 4; t++) temp[t] ^= rcon[i / nk][t]; } // 256-bit key has subword applied every 4th word else if (nk > 6 && % nk == 4) { temp = subword(temp); } // xor w[i] w[i-1] , w[i-nk] (int t = 0; t < 4; t++) w[i][t] = (w[i - nk][t] ^ temp[t]); } return w; }; /** * apply sbox state s [§5.1.1] * @private */ private static int[][] subbytes (int[][] s, int nb) { (int r = 0; r < 4; r++) { (int c = 0; c < nb; c++) s[r][c] = sbox[s[r][c]]; } return s; } /** * shift row r of state s left r bytes [§5.1.2] * @private */ private static int[][] shiftrows (int[][] s, int nb) { int[] t = new int[4]; (int r = 1; r < 4; r++) { (int c = 0; c < 4; c++) t[c] = s[r][(c + r) % nb]; // shift temp copy (int c = 0; c < 4; c++) s[r][c] = t[c]; // , copy } // note work nb=4,5,6, not 7,8 (always 4 aes): return s; // see asmaes.sourceforge.net/rijndael/rijndaelimplementation.pdf } /** * combine bytes of each col of state s [§5.1.3] * @private */ private static int[][] mixcolumns (int[][] s, int nb) { (int c = 0; c < 4; c++) { int[] = new int[4]; // 'a' copy of current column 's' int[] b = new int[4]; // 'b' a•{02} in gf(2^8) (int = 0; < 4; i++) { a[i] = s[i][c]; b[i] = (s[i][c] & 0x80) > 0 ? (s[i][c] << 1 ^ 0x011b) : (s[i][c] << 1); } // a[n] ^ b[n] a•{03} in gf(2^8) s[0][c] = (b[0] ^ a[1] ^ b[1] ^ a[2] ^ a[3]); // {02}•a0 + {03}•a1 + a2 + a3 s[1][c] = (a[0] ^ b[1] ^ a[2] ^ b[2] ^ a[3]); // a0 • {02}•a1 + {03}•a2 + a3 s[2][c] = (a[0] ^ a[1] ^ b[2] ^ a[3] ^ b[3]); // a0 + a1 + {02}•a2 + {03}•a3 s[3][c] = (a[0] ^ b[0] ^ a[1] ^ a[2] ^ b[3]); // {03}•a0 + a1 + a2 + {02}•a3 } return s; } /** * xor round key state s [§5.1.4] * @private */ private static int[][] addroundkey (int[][] state, int[][] w, int rnd, int nb) { (int r = 0; r < 4; r++) { (int c = 0; c < nb; c++) state[r][c] ^= w[rnd * 4 + c][r]; } return state; } /** * apply sbox 4-byte word w * @private */ private static int[] subword (int[] w) { (int = 0; < 4; i++) w[i] = sbox[w[i]]; return w; } /** * rotate 4-byte word w left 1 byte * @private */ private static int[] rotword (int[] w) { int tmp = w[0]; (int = 0; < 3; i++) w[i] = w[i + 1]; w[3] = tmp; return w; } /** * decrypt text encrypted aes in counter mode of operation * * @param {string} ciphertext - source text encrypted. * @param {string} password - password use generate key. * @param {number} nbits - number of bits used in key; 128 / 192 / 256. * @returns {string} decrypted text * * @example * var decr = aes.ctr.encrypt('lwgl66vvwvobkir6of8hvqjr', 'pāşšŵōřđ', 256); // decr: 'big secret' */ public static string decrypt(string ciphertext, string password, int nbits) throws exception{ string plaintext = ""; //try { int blocksize = 16; // block size fixed @ 16 bytes / 128 bits (nb=4) aes if (!(nbits == 128 || nbits == 192 || nbits == 256)) return ""; // standard allows 128/192/256 bit keys ciphertext = base64decoder(ciphertext); password = utf8encode(password); // use aes encrypt password (mirroring encrypt routine) int nbytes = nbits / 8; // no bytes in key int[] pwbytes = new int[nbytes]; (int = 0; < nbytes; i++) { pwbytes[i] = float.isnan(password.charat(i)) ? 0 : password.charat(i); } int[] key = cipher(pwbytes, keyexpansion(pwbytes)); // expand key 16/24/32 bytes long int bytesexpand = nbytes - 16; if(bytesexpand > 0){ int keyoriginallength = key.length; int[] expandkey = new int[bytesexpand]; int[] endkey = new int[keyoriginallength + bytesexpand]; system.arraycopy(key, 0, expandkey, 0, bytesexpand);// initial expandkey system.arraycopy(key, 0, endkey, 0, key.length);// copy key endkey system.arraycopy(expandkey, 0, endkey, key.length, expandkey.length); key = endkey; } // recover nonce 1st 8 bytes of ciphertext int[] counterblock = new int[16]; string ctrtxt = ciphertext.substring(0, 8); (int = 0; < 8; i++) counterblock[i] = ctrtxt.charat(i); // generate key schedule int[][] keyschedule = keyexpansion(key); // separate ciphertext blocks (skipping past initial 8 bytes) int nblocks = (int) math.ceil((ciphertext.length() - 8) / (float)blocksize); string[] cipherarr = new string[nblocks]; (int b = 0; b < nblocks; b++) { int start = 8 + b * blocksize; int end = 8 + b * blocksize + blocksize; if (end >= ciphertext.length()) cipherarr[b] = utf8encode(ciphertext.substring(start)); else cipherarr[b] = utf8encode(ciphertext.substring(start, end)); } // ciphertext array of block-length strings, ³f.àiþ±wãì¿,ß°d // plaintext generated block-by-block "³f.àiþ±wãì¿,ß°" array of block-length strings string[] plaintxt = new string[cipherarr.length]; // expand counterblock (int b = 0; b < nblocks; b++) { // set counter (block #) in last 8 bytes of counter block (leaving nonce in 1st 8 bytes) (int c = 0; c < 4; c++) counterblock[15 - c] = (b >> c * 8) & 0xff; (int c = 0; c < 4; c++) // counterblock[15 - c - 4] = (b / 0x100000000 >>> c * 8); counterblock[15 - c - 4] = 0; int[] ciphercntr = cipher(counterblock, keyschedule); // encrypt counter block char[] plaintxtbyte = new char[cipherarr[b].length()]; (int = 0; < cipherarr[b].length(); i++) { plaintxtbyte[i] = (char) (ciphercntr[i] ^ cipherarr[b].charat(i)); } plaintxt[b] = string.copyvalueof(plaintxtbyte); } // join array of blocks single plaintext string plaintext = joinarray(plaintxt);// plaintxt.join(''); // join array of blocks single plaintext string plaintext = utf8decode(plaintext);// decode utf8 unicode multi-byte chars return plaintext; }; private static string joinarray(object[] source){ string dest = ""; for(int = 0; i< source.length; i++){ dest += (string)source[i]; } return dest; } private static string utf8decode(string s) throws exception { byte[] utf8bytes = new byte[s.length()]; (int = 0; < s.length(); ++i) { //debug.assert( 0 <= utf8string[i] && utf8string[i] <= 255, "the char must in byte's range"); utf8bytes[i] = (byte)s.charat(i); } return new string(utf8bytes, utf8); } public static string base64decoder(string data) throws exception { byte[] b = base64.decode(data.getbytes("iso-8859-1"), base64.no_wrap); return new string(b, "iso-8859-1"); } private static final string utf8 = "utf-8"; private static string utf8encode(string s) throws unsupportedencodingexception { return new string(s.getbytes(utf8), utf8); } }
Comments
Post a Comment