如何修复类型错误:无法读取未定义的属性(读取“单词”)?

问题描述 投票:0回答:2

我在我的应用程序中实现了 crypto-J,用于本地存储加密和解密。效果很好。

问题::刷新时,页面变黑,控制台抛出以下错误:

TypeError: Cannot read properties of undefined (reading 'words')
    at Object._doReset (aes.js:103:1)
    at Object.reset (cipher-core.js:119:1)
    at Object.reset (cipher-core.js:461:1)
    at Object.init (cipher-core.js:104:1)
    at subtype.init (core.js:149:1)
    at subtype.init (core.js:149:1)
    at subtype.init (core.js:149:1)
    at Object.create (core.js:176:1)
    at Object.createDecryptor (cipher-core.js:81:1)
    at Object.decrypt (cipher-core.js:728:1)

注意:如果我清除本地存储并再次尝试,它会起作用。但是每当我刷新页面时,就会发生此错误并且页面会变为空白。

我想向您展示我所使用的加密和解密功能,如下:

................... imports .............
import * as CryptoJs from 'crypto-js';

let key: string;

const SECURE_DATA = {
  encrypt: (state: string) => CryptoJs.AES.encrypt(state, key).toString(),
  decrypt: (state: string) => CryptoJs.AES.decrypt(state, key).toString(CryptoJs.enc.Utf8),
};

const STORE_KEYS_TO_PERSIST = [
  { auth: SECURE_DATA },
 ...

];

export interface StoreState {
  auth: User;
....

}

export const reducers: ActionReducerMap<StoreState> = {
  auth: authReducer,
....
};

export function localStorageSyncReducer(
  reducer: ActionReducer<StoreState>
): ActionReducer<StoreState> {
  return localStorageSync({
    keys: STORE_KEYS_TO_PERSIST,
    rehydrate: true,
    syncCondition(state) {
      key = state.auth.token;
      return true;
    },
  })(reducer);
}

export function clearState(reducer) {
  return function (state, action) {
    if (action.type === ActionTypes.LOG_OUT) {
      state = undefined;
    }
    return reducer(state, action);
  };
}

export const metaReducers: Array<MetaReducer<StoreState, Action>> = [
  localStorageSyncReducer,
  clearState,
];

javascript angular encryption typeerror cryptojs
2个回答
1
投票

我认为将字符串转换为 CryptoJS wordArray 会生成它正在搜索的单词。

const encrypted =  CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse(key), 'phrase');

另外,比较一下值。你得到什么?它是未定义的还是一个值?从这里,您将知道缺少哪个值。


0
投票

我在工作中使用了cryptojs,我造成了严重的错误,我忘记将secret_key传输到crypt

© www.soinside.com 2019 - 2024. All rights reserved.