|
|
@ -112,10 +112,12 @@ function bytesXor (bytes1, bytes2) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function bytesToWords (bytes) { |
|
|
|
function bytesToWords (bytes) { |
|
|
|
var len = bytes.length, |
|
|
|
var len = bytes.byteLength || bytes.length, |
|
|
|
words = []; |
|
|
|
words = [], i; |
|
|
|
|
|
|
|
if (bytes instanceof ArrayBuffer) { |
|
|
|
for (var i = 0; i < len; i++) { |
|
|
|
bytes = new Uint8Array(bytes); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
for (i = 0; i < len; i++) { |
|
|
|
words[i >>> 2] |= bytes[i] << (24 - (i % 4) * 8); |
|
|
|
words[i >>> 2] |= bytes[i] << (24 - (i % 4) * 8); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -154,6 +156,16 @@ function bytesToArrayBuffer (b) { |
|
|
|
return (new Uint8Array(b)).buffer; |
|
|
|
return (new Uint8Array(b)).buffer; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function bufferConcat(buffer1, buffer2) { |
|
|
|
|
|
|
|
var l1 = buffer1.byteLength || buffer1.length, |
|
|
|
|
|
|
|
l2 = buffer2.byteLength || buffer2.length; |
|
|
|
|
|
|
|
var tmp = new Uint8Array(l1 + l2); |
|
|
|
|
|
|
|
tmp.set(buffer1 instanceof ArrayBuffer ? new Uint8Array(buffer1) : buffer1, 0); |
|
|
|
|
|
|
|
tmp.set(buffer2 instanceof ArrayBuffer ? new Uint8Array(buffer2) : buffer2, l1); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return tmp.buffer; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function bytesFromArrayBuffer (buffer) { |
|
|
|
function bytesFromArrayBuffer (buffer) { |
|
|
|
var len = buffer.byteLength, |
|
|
|
var len = buffer.byteLength, |
|
|
|
byteView = new Uint8Array(buffer), |
|
|
|
byteView = new Uint8Array(buffer), |
|
|
@ -228,15 +240,20 @@ function rsaEncrypt (publicKey, bytes) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function aesEncrypt (bytes, keyBytes, ivBytes) { |
|
|
|
function aesEncrypt (bytes, keyBytes, ivBytes) { |
|
|
|
// console.log('AES encrypt start', bytes.length/*, bytesToHex(keyBytes), bytesToHex(ivBytes)*/);
|
|
|
|
var len = bytes.byteLength || bytes.length; |
|
|
|
|
|
|
|
console.log(dT(), 'AES encrypt start', len/*, bytesToHex(keyBytes), bytesToHex(ivBytes)*/); |
|
|
|
|
|
|
|
|
|
|
|
var needPadding = 16 - (bytes.length % 16); |
|
|
|
var needPadding = 16 - (len % 16); |
|
|
|
if (needPadding > 0 && needPadding < 16) { |
|
|
|
if (needPadding > 0 && needPadding < 16) { |
|
|
|
var padding = new Array(needPadding); |
|
|
|
var padding = new Array(needPadding); |
|
|
|
(new SecureRandom()).nextBytes(padding); |
|
|
|
(new SecureRandom()).nextBytes(padding); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (bytes instanceof ArrayBuffer) { |
|
|
|
|
|
|
|
bytes = bufferConcat(bytes, padding); |
|
|
|
|
|
|
|
} else { |
|
|
|
bytes = bytes.concat(padding); |
|
|
|
bytes = bytes.concat(padding); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var encryptedWords = CryptoJS.AES.encrypt(bytesToWords(bytes), bytesToWords(keyBytes), { |
|
|
|
var encryptedWords = CryptoJS.AES.encrypt(bytesToWords(bytes), bytesToWords(keyBytes), { |
|
|
|
iv: bytesToWords(ivBytes), |
|
|
|
iv: bytesToWords(ivBytes), |
|
|
@ -246,7 +263,7 @@ function aesEncrypt (bytes, keyBytes, ivBytes) { |
|
|
|
|
|
|
|
|
|
|
|
var encryptedBytes = bytesFromWords(encryptedWords); |
|
|
|
var encryptedBytes = bytesFromWords(encryptedWords); |
|
|
|
|
|
|
|
|
|
|
|
// console.log('AES encrypt finish');
|
|
|
|
console.log(dT(), 'AES encrypt finish'); |
|
|
|
|
|
|
|
|
|
|
|
return encryptedBytes; |
|
|
|
return encryptedBytes; |
|
|
|
} |
|
|
|
} |
|
|
|