encryption.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. define(function(require, exports, module) {
  2. var _oCommon, _oUtils, _oBase64;
  3. require("websdk");
  4. _oCommon = require("common");
  5. _oUtils = require("utils");
  6. _oBase64 = require("base64");
  7. function EncryptionFac () {}
  8. EncryptionFac.prototype.encrypt = function (oStr, iRSABits, bBase64Encrypted, cbFun) {
  9. //var that = this;
  10. var iBits = 1024;
  11. if (iRSABits) {
  12. iBits = iRSABits;
  13. }
  14. var szPassPhrase = new Date() + "",
  15. szMattsRSAkey = cryptico.generateRSAKey(szPassPhrase, iBits),
  16. szPublicKeyString = cryptico.publicKeyString(szMattsRSAkey),
  17. szXml = "<?xml version='1.0' encoding='UTF-8'?><PublicKey><key>" + _oBase64.encode(szPublicKeyString) + "</key></PublicKey>",
  18. oXmlDoc = _oUtils.parseXmlFromStr(szXml);
  19. WebSDK.WSDK_Request(_oCommon.m_szHostName, _oCommon.m_iHttpProtocal, _oCommon.m_iHttpPort, {
  20. cmd: "challenge",
  21. type: "POST",
  22. data: oXmlDoc,
  23. success: function (status, xmlDoc) {
  24. var szDecryptionResult = cryptico.decrypt(_oBase64.decode(_oUtils.nodeValue(xmlDoc, "key")), szMattsRSAkey);
  25. if(szDecryptionResult.plaintext != null) {
  26. var szKey,szEncryptPassword;
  27. var aResult = [];
  28. if (iBits === 256) {
  29. szKey = _oUtils.toHex(szDecryptionResult.plaintext);
  30. } else {
  31. szKey = szDecryptionResult.plaintext;
  32. }
  33. szEncryptPassword = aes_encrypt(szDecryptionResult.plaintext.substring(0, 16), szKey, true);
  34. if("function" === typeof cbFun) {
  35. if("string" === typeof oStr) {
  36. bBase64Encrypted && ( oStr = _oBase64.encode(oStr) );
  37. cbFun( _oBase64.encode( szEncryptPassword + _oUtils.encodeAES(oStr, szKey, "", "ecb") ) );
  38. } else if("[object Array]" === Object.prototype.toString.call(oStr)) {
  39. for(var i = 0, iArrayLen = oStr.length; i < iArrayLen; i++) {
  40. bBase64Encrypted && ( oStr[i] = _oBase64.encode(oStr[i]) );
  41. var szTemp = szEncryptPassword + _oUtils.encodeAES(oStr[i], szKey, "", "ecb");
  42. aResult.push( _oBase64.encode(szTemp) );
  43. }
  44. cbFun(aResult);
  45. }
  46. }
  47. }
  48. }
  49. });
  50. };
  51. EncryptionFac.prototype.encryptSync = function (oStr, iRSABits, bBase64Encrypted) {
  52. //var that = this;
  53. var iBits = 1024;
  54. var aResult = [];
  55. if (iRSABits) {
  56. iBits = iRSABits;
  57. }
  58. var szPassPhrase = new Date() + "",
  59. szMattsRSAkey = cryptico.generateRSAKey(szPassPhrase, iBits),
  60. szPublicKeyString = cryptico.publicKeyString(szMattsRSAkey),
  61. szXml = "<?xml version='1.0' encoding='UTF-8'?><PublicKey><key>" + _oBase64.encode(szPublicKeyString) + "</key></PublicKey>",
  62. oXmlDoc = _oUtils.parseXmlFromStr(szXml);
  63. WebSDK.WSDK_Request(_oCommon.m_szHostName, _oCommon.m_iHttpProtocal, _oCommon.m_iHttpPort, {
  64. cmd: "challenge",
  65. type: "POST",
  66. async: false,
  67. data: oXmlDoc,
  68. success: function (status, xmlDoc) {
  69. var szDecryptionResult = cryptico.decrypt(_oBase64.decode(_oUtils.nodeValue(xmlDoc, "key")), szMattsRSAkey);
  70. if(szDecryptionResult.plaintext != null) {
  71. var szKey, szEncryptPassword;
  72. if (iBits === 256) {
  73. szKey = _oUtils.toHex(szDecryptionResult.plaintext);
  74. } else {
  75. szKey = szDecryptionResult.plaintext;
  76. }
  77. szEncryptPassword = aes_encrypt(szDecryptionResult.plaintext.substring(0, 16), szKey, true);
  78. if("string" === typeof oStr) {
  79. bBase64Encrypted && ( oStr = _oBase64.encode(oStr) );
  80. aResult.push( _oBase64.encode( szEncryptPassword + _oUtils.encodeAES(oStr, szKey, "", "ecb") ) );
  81. } else {
  82. for(var i = 0, iArrayLen = oStr.length; i < iArrayLen; i++) {
  83. bBase64Encrypted && ( oStr[i] = _oBase64.encode(oStr[i]) );
  84. var szTemp = szEncryptPassword + _oUtils.encodeAES(oStr[i], szKey, "", "ecb");
  85. aResult.push( _oBase64.encode(szTemp) );
  86. }
  87. }
  88. }
  89. }
  90. });
  91. if("string" === typeof oStr) {
  92. return aResult[0];
  93. } else {
  94. return aResult;
  95. }
  96. };
  97. module.exports = new EncryptionFac();
  98. });