Rebuild Angular Encryption on Android












0















I want to rebuild a symmetric encryption I am using from an angular library in my android app to decrypt messages.



Those are the functions of the angular library (https://www.npmjs.com/package/angular-encryption-service):



EncryptionService.prototype.generateKey = function (passphrase) {
return this.config.getSalt().then(function (salt) {
var /** @type {?} */ passphraseBytes = new textEncodingUtf8.TextEncoder().encode(passphrase);
var /** @type {?} */ importedKeyPromise = window.crypto.subtle.importKey('raw', passphraseBytes, { 'name': 'PBKDF2' }, false, ['deriveKey']);
var /** @type {?} */ derivedKeyPromise = importedKeyPromise.then(function (key) {
return window.crypto.subtle.deriveKey({
'name': 'PBKDF2',
'salt': typescriptBase64Arraybuffer.decode(salt),
// This should take around 0.5s to 1s
'iterations': 1000000,
'hash': 'SHA-256'
}, key, { 'name': 'AES-CBC', 'length': 256 }, false, ['encrypt', 'decrypt']);
});
return derivedKeyPromise;
});
};
/**
* @param {?} text
* @param {?} key
* @return {?}
*/
EncryptionService.prototype.encrypt = function (text, key) {
var /** @type {?} */ iv = new Uint8Array(16);
window.crypto.getRandomValues(iv);
return (window.crypto.subtle.encrypt({ 'name': 'AES-CBC', 'iv': iv }, key, new textEncodingUtf8.TextEncoder().encode(text)).then(function (cipherBuffer) {
// Need to implement base64 stuff here:
return typescriptBase64Arraybuffer.encode(iv.buffer) + ':' + typescriptBase64Arraybuffer.encode(cipherBuffer);
}));
};
/**
* @param {?} cipherText
* @param {?} key
* @return {?}
*/
EncryptionService.prototype.decrypt = function (cipherText, key) {
try {
var /** @type {?} */ pieces = cipherText.split(':');
if (pieces.length !== 2) {
return Promise.reject(new Error('encrypted text not formatted properly; missing ":"'));
}
var /** @type {?} */ iv = typescriptBase64Arraybuffer.decode(pieces[0]);
var /** @type {?} */ buffer = typescriptBase64Arraybuffer.decode(pieces[1]);
return (window.crypto.subtle.decrypt({ 'name': 'AES-CBC', 'iv': iv }, key, buffer).then(function (decryptedBuffer) {
return new textEncodingUtf8.TextDecoder().decode(new Uint8Array(decryptedBuffer));
}));
}
catch (e) {
return Promise.reject(e);
}
};


I stumbled across this answer:
PBKDF2 with SHA256 on android



However the 1000000 iterations are not viable to perform on my phone (app dies after like a minute).



So I have two choices - either switch the angular library or find a way to do this in android. Which would be the best way to do this?










share|improve this question























  • The number of iterations is for key stretching, you can reduce this number.

    – kelalaka
    Dec 28 '18 at 19:33
















0















I want to rebuild a symmetric encryption I am using from an angular library in my android app to decrypt messages.



Those are the functions of the angular library (https://www.npmjs.com/package/angular-encryption-service):



EncryptionService.prototype.generateKey = function (passphrase) {
return this.config.getSalt().then(function (salt) {
var /** @type {?} */ passphraseBytes = new textEncodingUtf8.TextEncoder().encode(passphrase);
var /** @type {?} */ importedKeyPromise = window.crypto.subtle.importKey('raw', passphraseBytes, { 'name': 'PBKDF2' }, false, ['deriveKey']);
var /** @type {?} */ derivedKeyPromise = importedKeyPromise.then(function (key) {
return window.crypto.subtle.deriveKey({
'name': 'PBKDF2',
'salt': typescriptBase64Arraybuffer.decode(salt),
// This should take around 0.5s to 1s
'iterations': 1000000,
'hash': 'SHA-256'
}, key, { 'name': 'AES-CBC', 'length': 256 }, false, ['encrypt', 'decrypt']);
});
return derivedKeyPromise;
});
};
/**
* @param {?} text
* @param {?} key
* @return {?}
*/
EncryptionService.prototype.encrypt = function (text, key) {
var /** @type {?} */ iv = new Uint8Array(16);
window.crypto.getRandomValues(iv);
return (window.crypto.subtle.encrypt({ 'name': 'AES-CBC', 'iv': iv }, key, new textEncodingUtf8.TextEncoder().encode(text)).then(function (cipherBuffer) {
// Need to implement base64 stuff here:
return typescriptBase64Arraybuffer.encode(iv.buffer) + ':' + typescriptBase64Arraybuffer.encode(cipherBuffer);
}));
};
/**
* @param {?} cipherText
* @param {?} key
* @return {?}
*/
EncryptionService.prototype.decrypt = function (cipherText, key) {
try {
var /** @type {?} */ pieces = cipherText.split(':');
if (pieces.length !== 2) {
return Promise.reject(new Error('encrypted text not formatted properly; missing ":"'));
}
var /** @type {?} */ iv = typescriptBase64Arraybuffer.decode(pieces[0]);
var /** @type {?} */ buffer = typescriptBase64Arraybuffer.decode(pieces[1]);
return (window.crypto.subtle.decrypt({ 'name': 'AES-CBC', 'iv': iv }, key, buffer).then(function (decryptedBuffer) {
return new textEncodingUtf8.TextDecoder().decode(new Uint8Array(decryptedBuffer));
}));
}
catch (e) {
return Promise.reject(e);
}
};


I stumbled across this answer:
PBKDF2 with SHA256 on android



However the 1000000 iterations are not viable to perform on my phone (app dies after like a minute).



So I have two choices - either switch the angular library or find a way to do this in android. Which would be the best way to do this?










share|improve this question























  • The number of iterations is for key stretching, you can reduce this number.

    – kelalaka
    Dec 28 '18 at 19:33














0












0








0








I want to rebuild a symmetric encryption I am using from an angular library in my android app to decrypt messages.



Those are the functions of the angular library (https://www.npmjs.com/package/angular-encryption-service):



EncryptionService.prototype.generateKey = function (passphrase) {
return this.config.getSalt().then(function (salt) {
var /** @type {?} */ passphraseBytes = new textEncodingUtf8.TextEncoder().encode(passphrase);
var /** @type {?} */ importedKeyPromise = window.crypto.subtle.importKey('raw', passphraseBytes, { 'name': 'PBKDF2' }, false, ['deriveKey']);
var /** @type {?} */ derivedKeyPromise = importedKeyPromise.then(function (key) {
return window.crypto.subtle.deriveKey({
'name': 'PBKDF2',
'salt': typescriptBase64Arraybuffer.decode(salt),
// This should take around 0.5s to 1s
'iterations': 1000000,
'hash': 'SHA-256'
}, key, { 'name': 'AES-CBC', 'length': 256 }, false, ['encrypt', 'decrypt']);
});
return derivedKeyPromise;
});
};
/**
* @param {?} text
* @param {?} key
* @return {?}
*/
EncryptionService.prototype.encrypt = function (text, key) {
var /** @type {?} */ iv = new Uint8Array(16);
window.crypto.getRandomValues(iv);
return (window.crypto.subtle.encrypt({ 'name': 'AES-CBC', 'iv': iv }, key, new textEncodingUtf8.TextEncoder().encode(text)).then(function (cipherBuffer) {
// Need to implement base64 stuff here:
return typescriptBase64Arraybuffer.encode(iv.buffer) + ':' + typescriptBase64Arraybuffer.encode(cipherBuffer);
}));
};
/**
* @param {?} cipherText
* @param {?} key
* @return {?}
*/
EncryptionService.prototype.decrypt = function (cipherText, key) {
try {
var /** @type {?} */ pieces = cipherText.split(':');
if (pieces.length !== 2) {
return Promise.reject(new Error('encrypted text not formatted properly; missing ":"'));
}
var /** @type {?} */ iv = typescriptBase64Arraybuffer.decode(pieces[0]);
var /** @type {?} */ buffer = typescriptBase64Arraybuffer.decode(pieces[1]);
return (window.crypto.subtle.decrypt({ 'name': 'AES-CBC', 'iv': iv }, key, buffer).then(function (decryptedBuffer) {
return new textEncodingUtf8.TextDecoder().decode(new Uint8Array(decryptedBuffer));
}));
}
catch (e) {
return Promise.reject(e);
}
};


I stumbled across this answer:
PBKDF2 with SHA256 on android



However the 1000000 iterations are not viable to perform on my phone (app dies after like a minute).



So I have two choices - either switch the angular library or find a way to do this in android. Which would be the best way to do this?










share|improve this question














I want to rebuild a symmetric encryption I am using from an angular library in my android app to decrypt messages.



Those are the functions of the angular library (https://www.npmjs.com/package/angular-encryption-service):



EncryptionService.prototype.generateKey = function (passphrase) {
return this.config.getSalt().then(function (salt) {
var /** @type {?} */ passphraseBytes = new textEncodingUtf8.TextEncoder().encode(passphrase);
var /** @type {?} */ importedKeyPromise = window.crypto.subtle.importKey('raw', passphraseBytes, { 'name': 'PBKDF2' }, false, ['deriveKey']);
var /** @type {?} */ derivedKeyPromise = importedKeyPromise.then(function (key) {
return window.crypto.subtle.deriveKey({
'name': 'PBKDF2',
'salt': typescriptBase64Arraybuffer.decode(salt),
// This should take around 0.5s to 1s
'iterations': 1000000,
'hash': 'SHA-256'
}, key, { 'name': 'AES-CBC', 'length': 256 }, false, ['encrypt', 'decrypt']);
});
return derivedKeyPromise;
});
};
/**
* @param {?} text
* @param {?} key
* @return {?}
*/
EncryptionService.prototype.encrypt = function (text, key) {
var /** @type {?} */ iv = new Uint8Array(16);
window.crypto.getRandomValues(iv);
return (window.crypto.subtle.encrypt({ 'name': 'AES-CBC', 'iv': iv }, key, new textEncodingUtf8.TextEncoder().encode(text)).then(function (cipherBuffer) {
// Need to implement base64 stuff here:
return typescriptBase64Arraybuffer.encode(iv.buffer) + ':' + typescriptBase64Arraybuffer.encode(cipherBuffer);
}));
};
/**
* @param {?} cipherText
* @param {?} key
* @return {?}
*/
EncryptionService.prototype.decrypt = function (cipherText, key) {
try {
var /** @type {?} */ pieces = cipherText.split(':');
if (pieces.length !== 2) {
return Promise.reject(new Error('encrypted text not formatted properly; missing ":"'));
}
var /** @type {?} */ iv = typescriptBase64Arraybuffer.decode(pieces[0]);
var /** @type {?} */ buffer = typescriptBase64Arraybuffer.decode(pieces[1]);
return (window.crypto.subtle.decrypt({ 'name': 'AES-CBC', 'iv': iv }, key, buffer).then(function (decryptedBuffer) {
return new textEncodingUtf8.TextDecoder().decode(new Uint8Array(decryptedBuffer));
}));
}
catch (e) {
return Promise.reject(e);
}
};


I stumbled across this answer:
PBKDF2 with SHA256 on android



However the 1000000 iterations are not viable to perform on my phone (app dies after like a minute).



So I have two choices - either switch the angular library or find a way to do this in android. Which would be the best way to do this?







java android angular encryption






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 28 '18 at 19:27









CurunirCurunir

116114




116114













  • The number of iterations is for key stretching, you can reduce this number.

    – kelalaka
    Dec 28 '18 at 19:33



















  • The number of iterations is for key stretching, you can reduce this number.

    – kelalaka
    Dec 28 '18 at 19:33

















The number of iterations is for key stretching, you can reduce this number.

– kelalaka
Dec 28 '18 at 19:33





The number of iterations is for key stretching, you can reduce this number.

– kelalaka
Dec 28 '18 at 19:33












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53963417%2frebuild-angular-encryption-on-android%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53963417%2frebuild-angular-encryption-on-android%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Mossoró

Error while reading .h5 file using the rhdf5 package in R

Pushsharp Apns notification error: 'InvalidToken'