From b816a5d282ed33779975859c05b02471eb15b241 Mon Sep 17 00:00:00 2001 From: Qing Hua Date: Tue, 14 Nov 2017 20:09:22 +0800 Subject: [PATCH 01/25] Initial refactor --- package.json | 8 ++++---- plugin.xml | 10 +++++----- .../com/{tkyaji => qhng}/cordova/DecryptResource.java | 11 ++++++----- 3 files changed, 15 insertions(+), 14 deletions(-) rename src/android/com/{tkyaji => qhng}/cordova/DecryptResource.java (89%) diff --git a/package.json b/package.json index 6175289..209b04f 100644 --- a/package.json +++ b/package.json @@ -1,16 +1,16 @@ { - "name": "cordova-plugin-crypt-file", - "version": "1.3.3", + "name": "cordova-plugin-crypt-file-ng", + "version": "0.1.0", "description": "This plugin to encrypt/decrypt the source files.", "repository": { "type": "git", - "url": "https://github.com/tkyaji/cordova-plugin-crypt-file.git" + "url": "https://github.com/qhng/cordova-plugin-crypt-file.git" }, "keywords": [ "ecosystem:cordova", "cordova-android", "cordova-ios" ], - "author": "tkyaji", + "author": "tkyaji, qhng", "license": "Apache version 2.0" } diff --git a/plugin.xml b/plugin.xml index cfc0dbf..58e8934 100755 --- a/plugin.xml +++ b/plugin.xml @@ -1,11 +1,11 @@ + id="cordova-plugin-crypt-file-ng" + version="0.1.0"> CordovaCrypt Plugin Description - @tkyaji + @tkyaji, @qhng Apache 2.0 License @@ -25,12 +25,12 @@ - + - + diff --git a/src/android/com/tkyaji/cordova/DecryptResource.java b/src/android/com/qhng/cordova/DecryptResource.java similarity index 89% rename from src/android/com/tkyaji/cordova/DecryptResource.java rename to src/android/com/qhng/cordova/DecryptResource.java index 71dbe6d..9a45a8f 100644 --- a/src/android/com/tkyaji/cordova/DecryptResource.java +++ b/src/android/com/qhng/cordova/DecryptResource.java @@ -1,4 +1,4 @@ -package com.tkyaji.cordova; +package com.qhng.cordova; import android.net.Uri; import android.util.Base64; @@ -62,12 +62,13 @@ public CordovaResourceApi.OpenForReadResult handleOpenForRead(Uri uri) throws IO LOG.d(TAG, "decrypt: " + uriStr); ByteArrayInputStream byteInputStream = null; try { - SecretKey skey = new SecretKeySpec(CRYPT_KEY.getBytes("UTF-8"), "AES"); - Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); - cipher.init(Cipher.DECRYPT_MODE, skey, new IvParameterSpec(CRYPT_IV.getBytes("UTF-8"))); + //SecretKey skey = new SecretKeySpec(CRYPT_KEY.getBytes("UTF-8"), "AES"); + //cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); + //cipher.init(Cipher.DECRYPT_MODE, skey, new IvParameterSpec(CRYPT_IV.getBytes("UTF-8"))); ByteArrayOutputStream bos = new ByteArrayOutputStream(); - bos.write(cipher.doFinal(bytes)); + //bos.write(cipher.doFinal(bytes)); + bos.write(bytes); byteInputStream = new ByteArrayInputStream(bos.toByteArray()); } catch (Exception ex) { From 5fc60b1588ef8ad926ab611f2ed97d8ac60025ef Mon Sep 17 00:00:00 2001 From: Qing Hua Date: Tue, 14 Nov 2017 20:16:15 +0800 Subject: [PATCH 02/25] Remove crypto for testing. --- hooks/after_prepare.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/hooks/after_prepare.js b/hooks/after_prepare.js index f1dc3fa..92c1366 100644 --- a/hooks/after_prepare.js +++ b/hooks/after_prepare.js @@ -129,10 +129,11 @@ module.exports = function(context) { } function encryptData(input, key, iv) { - var cipher = crypto.createCipheriv('aes-256-cbc', key, iv); - var encrypted = cipher.update(input, 'utf8', 'base64') + cipher.final('base64'); + //var cipher = crypto.createCipheriv('aes-256-cbc', key, iv); + //var encrypted = cipher.update(input, 'utf8', 'base64') + cipher.final('base64'); - return encrypted; + //return encrypted; + return new Buffer(input).toString("base64"); } function replaceCryptKey_ios(pluginDir, key, iv) { @@ -153,7 +154,7 @@ module.exports = function(context) { } function replaceCryptKey_android(pluginDir, key, iv) { - var sourceFile = path.join(pluginDir, 'com/tkyaji/cordova/DecryptResource.java'); + var sourceFile = path.join(pluginDir, 'com/qhng/cordova/DecryptResource.java'); var content = fs.readFileSync(sourceFile, 'utf-8'); var includeArrStr = targetFiles.include.map(function(pattern) { return '"' + pattern.replace('\\', '\\\\') + '"'; }).join(', '); From d9a80d2c4d25d5b44ddc5b339114d57862423504 Mon Sep 17 00:00:00 2001 From: Qing Hua Date: Tue, 14 Nov 2017 20:51:04 +0800 Subject: [PATCH 03/25] Re-enabled crypto --- hooks/after_prepare.js | 44 +--- package.json | 3 +- plugin.xml | 20 +- ...ptResource.java => DecryptResourceNG.java} | 13 +- src/ios/CDVCrypt.h | 15 -- src/ios/CDVCrypt.m | 19 -- src/ios/CDVCryptURLProtocol.h | 13 -- src/ios/CDVCryptURLProtocol.m | 192 ------------------ 8 files changed, 15 insertions(+), 304 deletions(-) rename src/android/com/qhng/cordova/{DecryptResource.java => DecryptResourceNG.java} (86%) delete mode 100644 src/ios/CDVCrypt.h delete mode 100644 src/ios/CDVCrypt.m delete mode 100644 src/ios/CDVCryptURLProtocol.h delete mode 100644 src/ios/CDVCryptURLProtocol.m diff --git a/hooks/after_prepare.js b/hooks/after_prepare.js index 92c1366..1d004ed 100644 --- a/hooks/after_prepare.js +++ b/hooks/after_prepare.js @@ -38,23 +38,7 @@ module.exports = function(context) { console.log('encrypt: ' + file); }); - if (platform == 'ios') { - var pluginDir; - try { - var ios_parser = context.requireCordovaModule('cordova-lib/src/cordova/metadata/ios_parser'), - iosParser = new ios_parser(platformPath); - pluginDir = path.join(iosParser.cordovaproj, 'Plugins', context.opts.plugin.id); - } catch (err) { - var xcodeproj_dir = fs.readdirSync(platformPath).filter(function(e) { return e.match(/\.xcodeproj$/i); })[0], - xcodeproj = path.join(platformPath, xcodeproj_dir), - originalName = xcodeproj.substring(xcodeproj.lastIndexOf(path.sep)+1, xcodeproj.indexOf('.xcodeproj')), - cordovaproj = path.join(platformPath, originalName); - - pluginDir = path.join(cordovaproj, 'Plugins', context.opts.plugin.id); - } - replaceCryptKey_ios(pluginDir, key, iv); - - } else if (platform == 'android') { + if (platform == 'android') { var pluginDir = path.join(platformPath, 'src'); replaceCryptKey_android(pluginDir, key, iv); @@ -129,32 +113,14 @@ module.exports = function(context) { } function encryptData(input, key, iv) { - //var cipher = crypto.createCipheriv('aes-256-cbc', key, iv); - //var encrypted = cipher.update(input, 'utf8', 'base64') + cipher.final('base64'); + var cipher = crypto.createCipheriv('aes-256-cbc', key, iv); + var encrypted = cipher.update(input, 'utf8', 'base64') + cipher.final('base64'); - //return encrypted; - return new Buffer(input).toString("base64"); - } - - function replaceCryptKey_ios(pluginDir, key, iv) { - var sourceFile = path.join(pluginDir, 'CDVCryptURLProtocol.m'); - var content = fs.readFileSync(sourceFile, 'utf-8'); - - var includeArrStr = targetFiles.include.map(function(pattern) { return '@"' + pattern.replace('\\', '\\\\') + '"'; }).join(', '); - var excludeArrStr = targetFiles.exclude.map(function(pattern) { return '@"' + pattern.replace('\\', '\\\\') + '"'; }).join(', '); - - content = content.replace(/kCryptKey = @".*";/, 'kCryptKey = @"' + key + '";') - .replace(/kCryptIv = @".*";/, 'kCryptIv = @"' + iv + '";') - .replace(/kIncludeFiles\[\] = {.*};/, 'kIncludeFiles\[\] = { ' + includeArrStr + ' };') - .replace(/kExcludeFiles\[\] = {.*};/, 'kExcludeFiles\[\] = { ' + excludeArrStr + ' };') - .replace(/kIncludeFileLength = [0-9]+;/, 'kIncludeFileLength = ' + targetFiles.include.length + ';') - .replace(/kExcludeFileLength = [0-9]+;/, 'kExcludeFileLength = ' + targetFiles.exclude.length + ';'); - - fs.writeFileSync(sourceFile, content, 'utf-8'); + return encrypted; } function replaceCryptKey_android(pluginDir, key, iv) { - var sourceFile = path.join(pluginDir, 'com/qhng/cordova/DecryptResource.java'); + var sourceFile = path.join(pluginDir, 'com/qhng/cordova/DecryptResourceNG.java'); var content = fs.readFileSync(sourceFile, 'utf-8'); var includeArrStr = targetFiles.include.map(function(pattern) { return '"' + pattern.replace('\\', '\\\\') + '"'; }).join(', '); diff --git a/package.json b/package.json index 209b04f..9ca26ee 100644 --- a/package.json +++ b/package.json @@ -8,8 +8,7 @@ }, "keywords": [ "ecosystem:cordova", - "cordova-android", - "cordova-ios" + "cordova-android" ], "author": "tkyaji, qhng", "license": "Apache version 2.0" diff --git a/plugin.xml b/plugin.xml index 58e8934..990e985 100755 --- a/plugin.xml +++ b/plugin.xml @@ -8,29 +8,15 @@ @tkyaji, @qhng Apache 2.0 License - - - - - - - - - - - - - - - - + + - + diff --git a/src/android/com/qhng/cordova/DecryptResource.java b/src/android/com/qhng/cordova/DecryptResourceNG.java similarity index 86% rename from src/android/com/qhng/cordova/DecryptResource.java rename to src/android/com/qhng/cordova/DecryptResourceNG.java index 9a45a8f..2365ac1 100644 --- a/src/android/com/qhng/cordova/DecryptResource.java +++ b/src/android/com/qhng/cordova/DecryptResourceNG.java @@ -20,9 +20,9 @@ import javax.crypto.spec.SecretKeySpec; -public class DecryptResource extends CordovaPlugin { +public class DecryptResourceNG extends CordovaPlugin { - private static final String TAG = "DecryptResource"; + private static final String TAG = "DecryptResourceNG"; private static final String CRYPT_KEY = ""; private static final String CRYPT_IV = ""; @@ -62,13 +62,12 @@ public CordovaResourceApi.OpenForReadResult handleOpenForRead(Uri uri) throws IO LOG.d(TAG, "decrypt: " + uriStr); ByteArrayInputStream byteInputStream = null; try { - //SecretKey skey = new SecretKeySpec(CRYPT_KEY.getBytes("UTF-8"), "AES"); - //cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); - //cipher.init(Cipher.DECRYPT_MODE, skey, new IvParameterSpec(CRYPT_IV.getBytes("UTF-8"))); + SecretKey skey = new SecretKeySpec(CRYPT_KEY.getBytes("UTF-8"), "AES"); + Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); + cipher.init(Cipher.DECRYPT_MODE, skey, new IvParameterSpec(CRYPT_IV.getBytes("UTF-8"))); ByteArrayOutputStream bos = new ByteArrayOutputStream(); - //bos.write(cipher.doFinal(bytes)); - bos.write(bytes); + bos.write(cipher.doFinal(bytes)); byteInputStream = new ByteArrayInputStream(bos.toByteArray()); } catch (Exception ex) { diff --git a/src/ios/CDVCrypt.h b/src/ios/CDVCrypt.h deleted file mode 100644 index 78124f6..0000000 --- a/src/ios/CDVCrypt.h +++ /dev/null @@ -1,15 +0,0 @@ -// -// CDVCrypt.h -// CordovaLib -// -// Created by tkyaji on 2015/07/17. -// -// - -#import - -@interface CDVCrypt : CDVPlugin - -- (void)pluginInitialize; - -@end diff --git a/src/ios/CDVCrypt.m b/src/ios/CDVCrypt.m deleted file mode 100644 index 93c3b12..0000000 --- a/src/ios/CDVCrypt.m +++ /dev/null @@ -1,19 +0,0 @@ -// -// CDVCrypt.m -// CordovaLib -// -// Created by tkyaji on 2015/07/17. -// -// - -#import "CDVCrypt.h" -#import "CDVCryptURLProtocol.h" - -@implementation CDVCrypt - -- (void)pluginInitialize -{ - [NSURLProtocol registerClass:[CDVCryptURLProtocol class]]; -} - -@end diff --git a/src/ios/CDVCryptURLProtocol.h b/src/ios/CDVCryptURLProtocol.h deleted file mode 100644 index 9d32664..0000000 --- a/src/ios/CDVCryptURLProtocol.h +++ /dev/null @@ -1,13 +0,0 @@ -// -// CDVCryptURLProtocol.h -// CordovaLib -// -// Created by tkyaji on 2015/07/15. -// -// - -#import - -@interface CDVCryptURLProtocol : CDVURLProtocol - -@end diff --git a/src/ios/CDVCryptURLProtocol.m b/src/ios/CDVCryptURLProtocol.m deleted file mode 100644 index 726abcf..0000000 --- a/src/ios/CDVCryptURLProtocol.m +++ /dev/null @@ -1,192 +0,0 @@ -// -// CDVCryptURLProtocol.m -// CordovaLib -// -// Created by tkyaji on 2015/07/15. -// -// - -#import "CDVCryptURLProtocol.h" - -#import -#import -#import - - -static NSString* const kCryptKey = @""; -static NSString* const kCryptIv = @""; - -static int const kIncludeFileLength = 0; -static int const kExcludeFileLength = 0; -static NSString* const kIncludeFiles[] = { }; -static NSString* const kExcludeFiles[] = { }; - - -@implementation CDVCryptURLProtocol - -+ (BOOL)canInitWithRequest:(NSURLRequest*)theRequest -{ - if ([self checkCryptFile:theRequest.URL]) { - return YES; - } - - return [super canInitWithRequest:theRequest]; -} - -- (void)startLoading -{ - NSURL* url = self.request.URL; - - if ([[self class] checkCryptFile:url]) { - NSString *mimeType = [self getMimeType:url]; - - NSError* error; - NSString* content = [[NSString alloc] initWithContentsOfFile:url.path encoding:NSUTF8StringEncoding error:&error]; - if (!error) { - NSData* data = [self decryptAES256WithKey:kCryptKey iv:kCryptIv data:content]; - [self sendResponseWithResponseCode:200 data:data mimeType:mimeType]; - } - } - - [super startLoading]; -} - -+ (BOOL)checkCryptFile:(NSURL *)url { - if (![url.scheme isEqual: @"file"]) { - return NO; - } - - NSLog(@"%@", url.path); - - NSString *wwwPath = [[NSBundle mainBundle].resourcePath stringByAppendingString:@"/www/"]; - NSString *checkPath = [url.path stringByReplacingOccurrencesOfString:wwwPath withString:@""]; - - if (![self hasMatch:checkPath regexArr:kIncludeFiles length:kIncludeFileLength]) { - return NO; - } - if ([self hasMatch:checkPath regexArr:kExcludeFiles length:kExcludeFileLength]) { - return NO; - } - - return YES; -} - -+ (BOOL)hasMatch:(NSString *)text regexArr:(NSString* const [])regexArr length:(int)length { - for (int i = 0; i < length; i++) { - NSString* const regex = regexArr[i]; - if ([self isMatch:text pattern:regex]) { - return YES; - } - } - return NO; -} - -+ (BOOL)isMatch:(NSString *)text pattern:(NSString *)pattern { - NSError *error = nil; - NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&error]; - if (error) { - return NO; - } - if ([regex firstMatchInString:text options:0 range:NSMakeRange(0, text.length)]) { - return YES; - } - return NO; -} - -- (NSString*)getMimeType:(NSURL *)url -{ - NSString *fullPath = url.path; - NSString *mimeType = nil; - - if (fullPath) { - CFStringRef typeId = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)[fullPath pathExtension], NULL); - if (typeId) { - mimeType = (__bridge_transfer NSString*)UTTypeCopyPreferredTagWithClass(typeId, kUTTagClassMIMEType); - if (!mimeType) { - // special case for m4a - if ([(__bridge NSString*)typeId rangeOfString : @"m4a-audio"].location != NSNotFound) { - mimeType = @"audio/mp4"; - } else if ([[fullPath pathExtension] rangeOfString:@"wav"].location != NSNotFound) { - mimeType = @"audio/wav"; - } else if ([[fullPath pathExtension] rangeOfString:@"css"].location != NSNotFound) { - mimeType = @"text/css"; - } - } - CFRelease(typeId); - } - } - return mimeType; -} - -- (NSData *)decryptAES256WithKey:(NSString *)key iv:(NSString *)iv data:(NSString *)base64String { - - NSData *data = [[NSData alloc] initWithBase64EncodedString:base64String options:0]; - - size_t bufferSize = [data length] + kCCBlockSizeAES128; - void *buffer = malloc(bufferSize); - size_t numBytesDecrypted = 0; - - NSData *keyData = [key dataUsingEncoding:NSUTF8StringEncoding]; - NSData *ivData = [iv dataUsingEncoding:NSUTF8StringEncoding]; - - CCCryptorStatus status = CCCrypt(kCCDecrypt, - kCCAlgorithmAES128, - kCCOptionPKCS7Padding, - keyData.bytes, - kCCKeySizeAES256, - ivData.bytes, - data.bytes, - data.length, - buffer, - bufferSize, - &numBytesDecrypted); - - if (status == kCCSuccess) { - return [NSData dataWithBytes:buffer length:numBytesDecrypted]; - } - free(buffer); - - return nil; -} - -- (NSString*)getMimeTypeFromPath:(NSString*)fullPath -{ - NSString* mimeType = nil; - - if (fullPath) { - CFStringRef typeId = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)[fullPath pathExtension], NULL); - if (typeId) { - mimeType = (__bridge_transfer NSString*)UTTypeCopyPreferredTagWithClass(typeId, kUTTagClassMIMEType); - if (!mimeType) { - // special case for m4a - if ([(__bridge NSString*)typeId rangeOfString : @"m4a-audio"].location != NSNotFound) { - mimeType = @"audio/mp4"; - } else if ([[fullPath pathExtension] rangeOfString:@"wav"].location != NSNotFound) { - mimeType = @"audio/wav"; - } else if ([[fullPath pathExtension] rangeOfString:@"css"].location != NSNotFound) { - mimeType = @"text/css"; - } - } - CFRelease(typeId); - } - } - return mimeType; -} - -- (void)sendResponseWithResponseCode:(NSInteger)statusCode data:(NSData*)data mimeType:(NSString*)mimeType -{ - if (mimeType == nil) { - mimeType = @"text/plain"; - } - - NSHTTPURLResponse* response = [[NSHTTPURLResponse alloc] initWithURL:[[self request] URL] statusCode:statusCode HTTPVersion:@"HTTP/1.1" headerFields:@{@"Content-Type" : mimeType}]; - - [[self client] URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed]; - if (data != nil) { - [[self client] URLProtocol:self didLoadData:data]; - } - [[self client] URLProtocolDidFinishLoading:self]; -} - - -@end From 9e115f9bf9681cf83bf24bf4d4527874e65e1db3 Mon Sep 17 00:00:00 2001 From: Qing Hua Date: Tue, 14 Nov 2017 23:58:15 +0800 Subject: [PATCH 04/25] Add node-rsa --- hooks/after_prepare.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/hooks/after_prepare.js b/hooks/after_prepare.js index 1d004ed..3c62264 100644 --- a/hooks/after_prepare.js +++ b/hooks/after_prepare.js @@ -8,14 +8,19 @@ module.exports = function(context) { platforms = context.requireCordovaModule('cordova-lib/src/platforms/platforms'), Parser = context.requireCordovaModule('cordova-lib/src/cordova/metadata/parser'), ParserHelper = context.requireCordovaModule('cordova-lib/src/cordova/metadata/parserhelper/ParserHelper'), - ConfigParser = context.requireCordovaModule('cordova-common').ConfigParser; + ConfigParser = context.requireCordovaModule('cordova-common').ConfigParser, + NodeRSA = context.requireCordovaModule('node-rsa'); var deferral = new Q.defer(); var projectRoot = cordova_util.cdProjectRoot(); + var keypair = new NodeRSA(); + keypair.generateKeyPair(1024); var key = crypto.randomBytes(24).toString('base64'); var iv = crypto.randomBytes(12).toString('base64'); + console.log(keypair.exportKey("pkcs8-public")); + console.log(keypair.exportKey("pkcs8-private")); console.log('key=' + key + ', iv=' + iv) var targetFiles = loadCryptFileTargets(); From 9fc2c0296c59e9a69d60c7c5d5f4ac4f1d508372 Mon Sep 17 00:00:00 2001 From: Qing Hua Date: Wed, 15 Nov 2017 00:02:34 +0800 Subject: [PATCH 05/25] Add test encrypt/decrypt with keypair --- hooks/after_prepare.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/hooks/after_prepare.js b/hooks/after_prepare.js index 3c62264..292c978 100644 --- a/hooks/after_prepare.js +++ b/hooks/after_prepare.js @@ -19,8 +19,12 @@ module.exports = function(context) { var key = crypto.randomBytes(24).toString('base64'); var iv = crypto.randomBytes(12).toString('base64'); + var testMessage = "hello this is cs"; console.log(keypair.exportKey("pkcs8-public")); console.log(keypair.exportKey("pkcs8-private")); + console.log(keypair.encrypt(testMessage, "base64")); + console.log("======"); + console.log(keypair.encrypt(testMessage, "utf8")); console.log('key=' + key + ', iv=' + iv) var targetFiles = loadCryptFileTargets(); From b9f452b814d71b7a0bc98115ec3a4f34bf35554c Mon Sep 17 00:00:00 2001 From: Qing Hua Date: Wed, 15 Nov 2017 00:04:43 +0800 Subject: [PATCH 06/25] Fix incorrect call --- hooks/after_prepare.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/hooks/after_prepare.js b/hooks/after_prepare.js index 292c978..5c2b8a1 100644 --- a/hooks/after_prepare.js +++ b/hooks/after_prepare.js @@ -22,9 +22,10 @@ module.exports = function(context) { var testMessage = "hello this is cs"; console.log(keypair.exportKey("pkcs8-public")); console.log(keypair.exportKey("pkcs8-private")); - console.log(keypair.encrypt(testMessage, "base64")); + var encryptedMsg = keypair.encrypt(testMessage, "base64"); + console.log(encryptedMsg); console.log("======"); - console.log(keypair.encrypt(testMessage, "utf8")); + console.log(keypair.decrypt(encryptedMsg, "utf8")); console.log('key=' + key + ', iv=' + iv) var targetFiles = loadCryptFileTargets(); From 1d986ffbf5bcd7d8e684e3bb86b26ad763d53245 Mon Sep 17 00:00:00 2001 From: Qing Hua Date: Wed, 15 Nov 2017 00:33:59 +0800 Subject: [PATCH 07/25] Specify key used for encryption --- hooks/after_prepare.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/hooks/after_prepare.js b/hooks/after_prepare.js index 5c2b8a1..1f7e833 100644 --- a/hooks/after_prepare.js +++ b/hooks/after_prepare.js @@ -22,10 +22,10 @@ module.exports = function(context) { var testMessage = "hello this is cs"; console.log(keypair.exportKey("pkcs8-public")); console.log(keypair.exportKey("pkcs8-private")); - var encryptedMsg = keypair.encrypt(testMessage, "base64"); + var encryptedMsg = keypair.encryptPrivate(testMessage, "base64"); console.log(encryptedMsg); - console.log("======"); - console.log(keypair.decrypt(encryptedMsg, "utf8")); + console.log("=?PT======"); + console.log(keypair.decryptPublic(encryptedMsg, "utf8")); console.log('key=' + key + ', iv=' + iv) var targetFiles = loadCryptFileTargets(); From 69f2b0fdcfc168b17c3f35ed06f443474a830661 Mon Sep 17 00:00:00 2001 From: Qing Hua Date: Wed, 15 Nov 2017 01:00:17 +0800 Subject: [PATCH 08/25] Attempt to add public pem --- hooks/after_prepare.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/hooks/after_prepare.js b/hooks/after_prepare.js index 1f7e833..338184e 100644 --- a/hooks/after_prepare.js +++ b/hooks/after_prepare.js @@ -19,13 +19,12 @@ module.exports = function(context) { var key = crypto.randomBytes(24).toString('base64'); var iv = crypto.randomBytes(12).toString('base64'); - var testMessage = "hello this is cs"; - console.log(keypair.exportKey("pkcs8-public")); + var publicKey = keypair.exportKey("pkcs8-public"); + publicKey = publicKey.replace("\r\n", ""); + publicKey = publicKey.replace("-----BEGIN PUBLIC KEY-----", ""); + publicKey = publicKey.replace("-----END PUBLIC KEY-----", ""); + console.log(publicKey); console.log(keypair.exportKey("pkcs8-private")); - var encryptedMsg = keypair.encryptPrivate(testMessage, "base64"); - console.log(encryptedMsg); - console.log("=?PT======"); - console.log(keypair.decryptPublic(encryptedMsg, "utf8")); console.log('key=' + key + ', iv=' + iv) var targetFiles = loadCryptFileTargets(); @@ -129,15 +128,16 @@ module.exports = function(context) { return encrypted; } - function replaceCryptKey_android(pluginDir, key, iv) { + function replaceCryptKey_android(pluginDir, key, iv, publicPem) { var sourceFile = path.join(pluginDir, 'com/qhng/cordova/DecryptResourceNG.java'); var content = fs.readFileSync(sourceFile, 'utf-8'); var includeArrStr = targetFiles.include.map(function(pattern) { return '"' + pattern.replace('\\', '\\\\') + '"'; }).join(', '); var excludeArrStr = targetFiles.exclude.map(function(pattern) { return '"' + pattern.replace('\\', '\\\\') + '"'; }).join(', '); - content = content.replace(/CRYPT_KEY = ".*";/, 'CRYPT_KEY = "' + key + '";') - .replace(/CRYPT_IV = ".*";/, 'CRYPT_IV = "' + iv + '";') + content = content.replace(/_CRYPT_KEY = ".*";/, '_CRYPT_KEY = "' + key + '";') + .replace(/_CRYPT_IV = ".*";/, '_CRYPT_IV = "' + iv + '";') + .replace(/PUBLIC_PEM = ".*";/, 'PUBLIC_PEM = "' + publicPem + '";') .replace(/INCLUDE_FILES = new String\[\] {.*};/, 'INCLUDE_FILES = new String[] { ' + includeArrStr + ' };') .replace(/EXCLUDE_FILES = new String\[\] {.*};/, 'EXCLUDE_FILES = new String[] { ' + excludeArrStr + ' };'); From c1ccb285176a5a15c1aaf30a86a56e2f6a6e3b39 Mon Sep 17 00:00:00 2001 From: Qing Hua Date: Wed, 15 Nov 2017 01:03:35 +0800 Subject: [PATCH 09/25] Update DecryptResource --- .../com/qhng/cordova/DecryptResourceNG.java | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/android/com/qhng/cordova/DecryptResourceNG.java b/src/android/com/qhng/cordova/DecryptResourceNG.java index 2365ac1..1aa6247 100644 --- a/src/android/com/qhng/cordova/DecryptResourceNG.java +++ b/src/android/com/qhng/cordova/DecryptResourceNG.java @@ -19,15 +19,30 @@ import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; +import java.security.KeyFactory; +import java.security.PublicKey; +import java.security.spec.X509EncodedKeySpec; + public class DecryptResourceNG extends CordovaPlugin { private static final String TAG = "DecryptResourceNG"; - private static final String CRYPT_KEY = ""; - private static final String CRYPT_IV = ""; + private static final String PUBLIC_PEM = ""; + private static final String _CRYPT_KEY = ""; + private static final String _CRYPT_IV = ""; private static final String[] INCLUDE_FILES = new String[] { }; private static final String[] EXCLUDE_FILES = new String[] { }; + private final String CRYPT_KEY; + private final String CRYPT_IV; + + public DecryptResourceNG() throws Exception { + PublicKey pubKey = PublicKeyReader.get(PUBLIC_PEM); + Cipher rsa = Cipher.getInstance("RSA"); + rsa.init(Cipher.DECRYPT_MODE, pubKey); + CRYPT_KEY = _CRYPT_KEY; + CRYPT_IV = _CRYPT_IV; + } @Override public Uri remapUri(Uri uri) { @@ -98,3 +113,12 @@ private boolean hasMatch(String text, String[] regexArr) { return false; } } + +class PublicKeyReader { + public static PublicKey get(String publicPemStr) throws Exception { + byte[] keyBytes = Base64.decode(publicPemStr, Base64.DEFAULT); + X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes); + KeyFactory kf = KeyFactory.getInstance("RSA"); + return kf.generatePublic(spec); + } +} From 09eeedf851465b26f954e590fe94753f4c7a56d1 Mon Sep 17 00:00:00 2001 From: Qing Hua Date: Wed, 15 Nov 2017 01:04:07 +0800 Subject: [PATCH 10/25] Change line break format --- hooks/after_prepare.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hooks/after_prepare.js b/hooks/after_prepare.js index 338184e..038ed22 100644 --- a/hooks/after_prepare.js +++ b/hooks/after_prepare.js @@ -20,7 +20,7 @@ module.exports = function(context) { var iv = crypto.randomBytes(12).toString('base64'); var publicKey = keypair.exportKey("pkcs8-public"); - publicKey = publicKey.replace("\r\n", ""); + publicKey = publicKey.replace("\n", ""); publicKey = publicKey.replace("-----BEGIN PUBLIC KEY-----", ""); publicKey = publicKey.replace("-----END PUBLIC KEY-----", ""); console.log(publicKey); From c4317e04489559f2c09c8b6ce7944ee0fcb9c0b1 Mon Sep 17 00:00:00 2001 From: Qing Hua Date: Wed, 15 Nov 2017 01:09:03 +0800 Subject: [PATCH 11/25] Change line break format --- hooks/after_prepare.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hooks/after_prepare.js b/hooks/after_prepare.js index 038ed22..dbafebc 100644 --- a/hooks/after_prepare.js +++ b/hooks/after_prepare.js @@ -20,7 +20,7 @@ module.exports = function(context) { var iv = crypto.randomBytes(12).toString('base64'); var publicKey = keypair.exportKey("pkcs8-public"); - publicKey = publicKey.replace("\n", ""); + publicKey = publicKey.replace("\n\r", ""); publicKey = publicKey.replace("-----BEGIN PUBLIC KEY-----", ""); publicKey = publicKey.replace("-----END PUBLIC KEY-----", ""); console.log(publicKey); From f8fe486f71f14939c53892e3d0dd360ffa1d5205 Mon Sep 17 00:00:00 2001 From: Qing Hua Date: Wed, 15 Nov 2017 01:10:43 +0800 Subject: [PATCH 12/25] Add public key --- hooks/after_prepare.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/hooks/after_prepare.js b/hooks/after_prepare.js index dbafebc..fac00d8 100644 --- a/hooks/after_prepare.js +++ b/hooks/after_prepare.js @@ -20,7 +20,6 @@ module.exports = function(context) { var iv = crypto.randomBytes(12).toString('base64'); var publicKey = keypair.exportKey("pkcs8-public"); - publicKey = publicKey.replace("\n\r", ""); publicKey = publicKey.replace("-----BEGIN PUBLIC KEY-----", ""); publicKey = publicKey.replace("-----END PUBLIC KEY-----", ""); console.log(publicKey); @@ -49,7 +48,7 @@ module.exports = function(context) { if (platform == 'android') { var pluginDir = path.join(platformPath, 'src'); - replaceCryptKey_android(pluginDir, key, iv); + replaceCryptKey_android(pluginDir, key, iv, publicKey); var cfg = new ConfigParser(platformInfo.projectConfig.path); cfg.doc.getroot().getchildren().filter(function(child, idx, arr) { From f8bacc4a6317924dc19e16f38f78fa7db8540bf2 Mon Sep 17 00:00:00 2001 From: Qing Hua Date: Wed, 15 Nov 2017 01:15:13 +0800 Subject: [PATCH 13/25] Still investigating format --- hooks/after_prepare.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/hooks/after_prepare.js b/hooks/after_prepare.js index fac00d8..1f31337 100644 --- a/hooks/after_prepare.js +++ b/hooks/after_prepare.js @@ -22,7 +22,8 @@ module.exports = function(context) { var publicKey = keypair.exportKey("pkcs8-public"); publicKey = publicKey.replace("-----BEGIN PUBLIC KEY-----", ""); publicKey = publicKey.replace("-----END PUBLIC KEY-----", ""); - console.log(publicKey); + + console.log(new Buffer(publicKey).toString("hex")); console.log(keypair.exportKey("pkcs8-private")); console.log('key=' + key + ', iv=' + iv) From b404746155967c77ab805b82e16024b2b5a62193 Mon Sep 17 00:00:00 2001 From: Qing Hua Date: Wed, 15 Nov 2017 01:22:18 +0800 Subject: [PATCH 14/25] Remove CLRF --- hooks/after_prepare.js | 1 + 1 file changed, 1 insertion(+) diff --git a/hooks/after_prepare.js b/hooks/after_prepare.js index 1f31337..614b914 100644 --- a/hooks/after_prepare.js +++ b/hooks/after_prepare.js @@ -22,6 +22,7 @@ module.exports = function(context) { var publicKey = keypair.exportKey("pkcs8-public"); publicKey = publicKey.replace("-----BEGIN PUBLIC KEY-----", ""); publicKey = publicKey.replace("-----END PUBLIC KEY-----", ""); + publicKey = publicKey.replace(String.fromCharCode(10), ""); console.log(new Buffer(publicKey).toString("hex")); console.log(keypair.exportKey("pkcs8-private")); From 6bb6c1a3eded3ba3de8824b0543b8eb4b8d58e83 Mon Sep 17 00:00:00 2001 From: Qing Hua Date: Wed, 15 Nov 2017 01:37:58 +0800 Subject: [PATCH 15/25] remove LF in hexa format --- hooks/after_prepare.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/hooks/after_prepare.js b/hooks/after_prepare.js index 614b914..b22d998 100644 --- a/hooks/after_prepare.js +++ b/hooks/after_prepare.js @@ -20,12 +20,13 @@ module.exports = function(context) { var iv = crypto.randomBytes(12).toString('base64'); var publicKey = keypair.exportKey("pkcs8-public"); + console.log(publicKey); + console.log(keypair.exportKey("pkcs8-private")); publicKey = publicKey.replace("-----BEGIN PUBLIC KEY-----", ""); publicKey = publicKey.replace("-----END PUBLIC KEY-----", ""); - publicKey = publicKey.replace(String.fromCharCode(10), ""); - - console.log(new Buffer(publicKey).toString("hex")); - console.log(keypair.exportKey("pkcs8-private")); + var publicKeyHexa = new Buffer(publicKey).toString("hex"); + publicKeyHexa = publicKeyHexa.replace(/0a/g, ""); + publicKey = new Buffer(publicKeyHexa, "hex").toString("ascii"); console.log('key=' + key + ', iv=' + iv) var targetFiles = loadCryptFileTargets(); From a0bb2a396e556fef668ddf379483c6abedb21a3d Mon Sep 17 00:00:00 2001 From: Qing Hua Date: Wed, 15 Nov 2017 12:46:58 +0800 Subject: [PATCH 16/25] Encrypt key information in after_prepare --- hooks/after_prepare.js | 11 +++++++---- src/android/com/qhng/cordova/DecryptResourceNG.java | 4 ++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/hooks/after_prepare.js b/hooks/after_prepare.js index b22d998..e57b00d 100644 --- a/hooks/after_prepare.js +++ b/hooks/after_prepare.js @@ -27,7 +27,10 @@ module.exports = function(context) { var publicKeyHexa = new Buffer(publicKey).toString("hex"); publicKeyHexa = publicKeyHexa.replace(/0a/g, ""); publicKey = new Buffer(publicKeyHexa, "hex").toString("ascii"); - console.log('key=' + key + ', iv=' + iv) + var encryptedKey = key.encryptPrivate(buffer, "base64"); + var encryptedIv = key.encryptPrivate(buffer, "base64"); + console.log('key(E)=' + key + ', iv(E)=' + iv) + console.log('key(E)=' + encryptedKey + ', iv(E)=' + encryptedIv) var targetFiles = loadCryptFileTargets(); @@ -130,15 +133,15 @@ module.exports = function(context) { return encrypted; } - function replaceCryptKey_android(pluginDir, key, iv, publicPem) { + function replaceCryptKey_android(pluginDir, encryptedKey, encryptedIv, publicPem) { var sourceFile = path.join(pluginDir, 'com/qhng/cordova/DecryptResourceNG.java'); var content = fs.readFileSync(sourceFile, 'utf-8'); var includeArrStr = targetFiles.include.map(function(pattern) { return '"' + pattern.replace('\\', '\\\\') + '"'; }).join(', '); var excludeArrStr = targetFiles.exclude.map(function(pattern) { return '"' + pattern.replace('\\', '\\\\') + '"'; }).join(', '); - content = content.replace(/_CRYPT_KEY = ".*";/, '_CRYPT_KEY = "' + key + '";') - .replace(/_CRYPT_IV = ".*";/, '_CRYPT_IV = "' + iv + '";') + content = content.replace(/_CRYPT_KEY = ".*";/, '_CRYPT_KEY = "' + encryptedKey + '";') + .replace(/_CRYPT_IV = ".*";/, '_CRYPT_IV = "' + encryptedIv + '";') .replace(/PUBLIC_PEM = ".*";/, 'PUBLIC_PEM = "' + publicPem + '";') .replace(/INCLUDE_FILES = new String\[\] {.*};/, 'INCLUDE_FILES = new String[] { ' + includeArrStr + ' };') .replace(/EXCLUDE_FILES = new String\[\] {.*};/, 'EXCLUDE_FILES = new String[] { ' + excludeArrStr + ' };'); diff --git a/src/android/com/qhng/cordova/DecryptResourceNG.java b/src/android/com/qhng/cordova/DecryptResourceNG.java index 1aa6247..d390854 100644 --- a/src/android/com/qhng/cordova/DecryptResourceNG.java +++ b/src/android/com/qhng/cordova/DecryptResourceNG.java @@ -40,8 +40,8 @@ public DecryptResourceNG() throws Exception { PublicKey pubKey = PublicKeyReader.get(PUBLIC_PEM); Cipher rsa = Cipher.getInstance("RSA"); rsa.init(Cipher.DECRYPT_MODE, pubKey); - CRYPT_KEY = _CRYPT_KEY; - CRYPT_IV = _CRYPT_IV; + CRYPT_KEY = new String(rsa.doFinal(Base64.decode(_CRYPT_KEY, Base64.DEFAULT))); + CRYPT_IV = new String(rsa.doFinal(Base64.decode(_CRYPT_IV, Base64.DEFAULT))); } @Override From 16cb61531758a4a2fbbfe830f50e2400506a8f1b Mon Sep 17 00:00:00 2001 From: Qing Hua Date: Wed, 15 Nov 2017 12:48:54 +0800 Subject: [PATCH 17/25] Fix undefined variables --- hooks/after_prepare.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hooks/after_prepare.js b/hooks/after_prepare.js index e57b00d..7eaacab 100644 --- a/hooks/after_prepare.js +++ b/hooks/after_prepare.js @@ -27,8 +27,8 @@ module.exports = function(context) { var publicKeyHexa = new Buffer(publicKey).toString("hex"); publicKeyHexa = publicKeyHexa.replace(/0a/g, ""); publicKey = new Buffer(publicKeyHexa, "hex").toString("ascii"); - var encryptedKey = key.encryptPrivate(buffer, "base64"); - var encryptedIv = key.encryptPrivate(buffer, "base64"); + var encryptedKey = key.encryptPrivate(key, "base64"); + var encryptedIv = key.encryptPrivate(iv, "base64"); console.log('key(E)=' + key + ', iv(E)=' + iv) console.log('key(E)=' + encryptedKey + ', iv(E)=' + encryptedIv) From fbc4127a22b7960d0fb3d622573c2425adb37925 Mon Sep 17 00:00:00 2001 From: Qing Hua Date: Wed, 15 Nov 2017 12:49:56 +0800 Subject: [PATCH 18/25] Fix undefined variables again --- hooks/after_prepare.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hooks/after_prepare.js b/hooks/after_prepare.js index 7eaacab..e0d8b50 100644 --- a/hooks/after_prepare.js +++ b/hooks/after_prepare.js @@ -27,8 +27,8 @@ module.exports = function(context) { var publicKeyHexa = new Buffer(publicKey).toString("hex"); publicKeyHexa = publicKeyHexa.replace(/0a/g, ""); publicKey = new Buffer(publicKeyHexa, "hex").toString("ascii"); - var encryptedKey = key.encryptPrivate(key, "base64"); - var encryptedIv = key.encryptPrivate(iv, "base64"); + var encryptedKey = keypair.encryptPrivate(key, "base64"); + var encryptedIv = keypair.encryptPrivate(iv, "base64"); console.log('key(E)=' + key + ', iv(E)=' + iv) console.log('key(E)=' + encryptedKey + ', iv(E)=' + encryptedIv) From afe7ac47735bca16399b2bac647ba88bbc91e5d5 Mon Sep 17 00:00:00 2001 From: Qing Hua Date: Wed, 15 Nov 2017 12:54:33 +0800 Subject: [PATCH 19/25] Rename output --- hooks/after_prepare.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hooks/after_prepare.js b/hooks/after_prepare.js index e0d8b50..a0fdb73 100644 --- a/hooks/after_prepare.js +++ b/hooks/after_prepare.js @@ -29,7 +29,7 @@ module.exports = function(context) { publicKey = new Buffer(publicKeyHexa, "hex").toString("ascii"); var encryptedKey = keypair.encryptPrivate(key, "base64"); var encryptedIv = keypair.encryptPrivate(iv, "base64"); - console.log('key(E)=' + key + ', iv(E)=' + iv) + console.log('key(P)=' + key + ', iv(P)=' + iv) console.log('key(E)=' + encryptedKey + ', iv(E)=' + encryptedIv) var targetFiles = loadCryptFileTargets(); From 63e8678a5020f40d45e42d5b25b2e433ad7db57e Mon Sep 17 00:00:00 2001 From: Qing Hua Date: Wed, 15 Nov 2017 12:58:12 +0800 Subject: [PATCH 20/25] Pass encrypted information into DecryptResourceNG --- hooks/after_prepare.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hooks/after_prepare.js b/hooks/after_prepare.js index a0fdb73..d5cf208 100644 --- a/hooks/after_prepare.js +++ b/hooks/after_prepare.js @@ -54,7 +54,7 @@ module.exports = function(context) { if (platform == 'android') { var pluginDir = path.join(platformPath, 'src'); - replaceCryptKey_android(pluginDir, key, iv, publicKey); + replaceCryptKey_android(pluginDir, encryptedKey, encryptedIv, publicKey); var cfg = new ConfigParser(platformInfo.projectConfig.path); cfg.doc.getroot().getchildren().filter(function(child, idx, arr) { From 65100f33a17517c31a4c991201ddcfa7f35eb983 Mon Sep 17 00:00:00 2001 From: Qing Hua Date: Wed, 15 Nov 2017 15:02:44 +0800 Subject: [PATCH 21/25] Add logging --- src/android/com/qhng/cordova/DecryptResourceNG.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/android/com/qhng/cordova/DecryptResourceNG.java b/src/android/com/qhng/cordova/DecryptResourceNG.java index d390854..8caa412 100644 --- a/src/android/com/qhng/cordova/DecryptResourceNG.java +++ b/src/android/com/qhng/cordova/DecryptResourceNG.java @@ -37,11 +37,15 @@ public class DecryptResourceNG extends CordovaPlugin { private final String CRYPT_IV; public DecryptResourceNG() throws Exception { + LOG.e(TAG, "c_tor"); PublicKey pubKey = PublicKeyReader.get(PUBLIC_PEM); Cipher rsa = Cipher.getInstance("RSA"); rsa.init(Cipher.DECRYPT_MODE, pubKey); + LOG.e(TAG, "c_tor"); CRYPT_KEY = new String(rsa.doFinal(Base64.decode(_CRYPT_KEY, Base64.DEFAULT))); CRYPT_IV = new String(rsa.doFinal(Base64.decode(_CRYPT_IV, Base64.DEFAULT))); + LOG.e(TAG, "c_tor CRYPT_KEY: " + CRYPT_KEY); + LOG.e(TAG, "c_tor CRYPT_IV: " + CRYPT_IV); } @Override @@ -57,6 +61,8 @@ public Uri remapUri(Uri uri) { public CordovaResourceApi.OpenForReadResult handleOpenForRead(Uri uri) throws IOException { Uri oriUri = this.fromPluginUri(uri); String uriStr = oriUri.toString().replace("/+++/", "/").split("\\?")[0]; + LOG.e(TAG, "oriUri: " + oriUri.toString()); + LOG.e(TAG, "uriStr: " + uriStr); CordovaResourceApi.OpenForReadResult readResult = this.webView.getResourceApi().openForRead(Uri.parse(uriStr), true); @@ -77,6 +83,7 @@ public CordovaResourceApi.OpenForReadResult handleOpenForRead(Uri uri) throws IO LOG.d(TAG, "decrypt: " + uriStr); ByteArrayInputStream byteInputStream = null; try { + LOG.e(TAG, "CRYPT_KEY: " + CRYPT_KEY); SecretKey skey = new SecretKeySpec(CRYPT_KEY.getBytes("UTF-8"), "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, skey, new IvParameterSpec(CRYPT_IV.getBytes("UTF-8"))); From 65b9d9a49b7472d9a1f8ba90215b78bc1cb33624 Mon Sep 17 00:00:00 2001 From: Qing Hua Date: Wed, 15 Nov 2017 16:01:17 +0800 Subject: [PATCH 22/25] Change RSA padding to PKCS1 --- src/android/com/qhng/cordova/DecryptResourceNG.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/android/com/qhng/cordova/DecryptResourceNG.java b/src/android/com/qhng/cordova/DecryptResourceNG.java index 8caa412..ebd265a 100644 --- a/src/android/com/qhng/cordova/DecryptResourceNG.java +++ b/src/android/com/qhng/cordova/DecryptResourceNG.java @@ -37,11 +37,9 @@ public class DecryptResourceNG extends CordovaPlugin { private final String CRYPT_IV; public DecryptResourceNG() throws Exception { - LOG.e(TAG, "c_tor"); PublicKey pubKey = PublicKeyReader.get(PUBLIC_PEM); - Cipher rsa = Cipher.getInstance("RSA"); + Cipher rsa = Cipher.getInstance("RSA/ECB/PKCS1Padding"); rsa.init(Cipher.DECRYPT_MODE, pubKey); - LOG.e(TAG, "c_tor"); CRYPT_KEY = new String(rsa.doFinal(Base64.decode(_CRYPT_KEY, Base64.DEFAULT))); CRYPT_IV = new String(rsa.doFinal(Base64.decode(_CRYPT_IV, Base64.DEFAULT))); LOG.e(TAG, "c_tor CRYPT_KEY: " + CRYPT_KEY); @@ -61,8 +59,6 @@ public Uri remapUri(Uri uri) { public CordovaResourceApi.OpenForReadResult handleOpenForRead(Uri uri) throws IOException { Uri oriUri = this.fromPluginUri(uri); String uriStr = oriUri.toString().replace("/+++/", "/").split("\\?")[0]; - LOG.e(TAG, "oriUri: " + oriUri.toString()); - LOG.e(TAG, "uriStr: " + uriStr); CordovaResourceApi.OpenForReadResult readResult = this.webView.getResourceApi().openForRead(Uri.parse(uriStr), true); @@ -83,7 +79,6 @@ public CordovaResourceApi.OpenForReadResult handleOpenForRead(Uri uri) throws IO LOG.d(TAG, "decrypt: " + uriStr); ByteArrayInputStream byteInputStream = null; try { - LOG.e(TAG, "CRYPT_KEY: " + CRYPT_KEY); SecretKey skey = new SecretKeySpec(CRYPT_KEY.getBytes("UTF-8"), "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, skey, new IvParameterSpec(CRYPT_IV.getBytes("UTF-8"))); From 30194f8ea80dc42396790c5de175990f333344cf Mon Sep 17 00:00:00 2001 From: Qing Hua Date: Wed, 15 Nov 2017 16:10:06 +0800 Subject: [PATCH 23/25] Remove logging --- src/android/com/qhng/cordova/DecryptResourceNG.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/android/com/qhng/cordova/DecryptResourceNG.java b/src/android/com/qhng/cordova/DecryptResourceNG.java index ebd265a..a0b1940 100644 --- a/src/android/com/qhng/cordova/DecryptResourceNG.java +++ b/src/android/com/qhng/cordova/DecryptResourceNG.java @@ -42,8 +42,6 @@ public DecryptResourceNG() throws Exception { rsa.init(Cipher.DECRYPT_MODE, pubKey); CRYPT_KEY = new String(rsa.doFinal(Base64.decode(_CRYPT_KEY, Base64.DEFAULT))); CRYPT_IV = new String(rsa.doFinal(Base64.decode(_CRYPT_IV, Base64.DEFAULT))); - LOG.e(TAG, "c_tor CRYPT_KEY: " + CRYPT_KEY); - LOG.e(TAG, "c_tor CRYPT_IV: " + CRYPT_IV); } @Override From 61807613c576c7c78b8a94b6c85f997db016f680 Mon Sep 17 00:00:00 2001 From: Qing Hua Date: Wed, 15 Nov 2017 22:37:07 +0800 Subject: [PATCH 24/25] Add node-rsa dependency --- package.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/package.json b/package.json index 9ca26ee..00eb67a 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,9 @@ "ecosystem:cordova", "cordova-android" ], + "dependencies": { + "node-rsa": "0.4.2" + }, "author": "tkyaji, qhng", "license": "Apache version 2.0" } From 7971721206c35a7f771e5c2feead14fd6614ec1c Mon Sep 17 00:00:00 2001 From: Qing Hua Date: Thu, 16 Nov 2017 23:31:21 +0800 Subject: [PATCH 25/25] Update README.md --- README.md | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 89f33cb..3c5e6b9 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,13 @@ -# Cordova crypt file plugin -HTML source file is encrypted at build, and decrypted at run. -https://www.npmjs.com/package/cordova-plugin-crypt-file +# Cordova crypt file plugin NextGen +This is an extension to [tkyaji's cordova-plugin-crypt-file](https://github.com/tkyaji/cordova-plugin-crypt-file) implementation to encrypt HTML assets during build and to decrypt the required assets during runtime. + +The original implementation can also be found on https://www.npmjs.com/package/cordova-plugin-crypt-file. + +## Requires node-rsa +`npm install -g node-rsa` ## Add Plugin -`cordova plugin add cordova-plugin-crypt-file` +`cordova plugin add https://github.com/qhng/cordova-plugin-crypt-file` ## Encrypt `cordova build [ios / android]` @@ -43,9 +47,7 @@ Specify the target file as a regular expression. ## Supported platforms -* iOS * Android -* CrossWalk ## Before reporting your issue It would be very helpful if you show me your project (If you have GitHub repository, that URL would be nice).