대칭키 암호 알고리즘 (AES 128/192/256)을 지원하는 보안모듈
– 고급 암호화 표준(Advanced Encryption Standard, AES)는 미국 표준 기술 연구소에 의해 제정된 암호화 방식이며, 모바일 환경에서 가장 많이 사용하는 대칭키 암호 알고리즘
# npm install –save react-native-aes-cipher
# cd ios
# pod install
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
// 선언부 import { NativeModules } from 'react-native'; const AesCipher = NativeModules.Aes const generateKey = (password: string, salt: string, cost: number, length: number) => AesCipher.pbkdf2(password, salt, cost, length) const encryptData = (text: string, key: any) => { return AesCipher.randomKey(16).then((iv: any) => { return AesCipher.encrypt(text, key, iv).then((cipher: any) => ({ cipher, iv, })) }) } const encryptDataIV = (text: string, key: any, iv:any) => { return AesCipher.encrypt(text, key, iv).then((cipher: any) => ({ cipher, iv, })) } const decryptData = (encryptedData: { cipher: any; iv: any; }, key: any) => AesCipher.decrypt(encryptedData.cipher, key, encryptedData.iv) const iv_string = '0123456789abcdef0123456789abcdef'; let encrypt_key:any = ""; let encrypt_string:any = ""; let plain_string:any = "1234567890"; let encrypt_iv:any = ""; |
1 2 3 4 5 6 7 8 9 10 11 12 |
// 키생성 private AESKey () { try { generateKey('nixstory@gmail.com', 'SALT', 1000, 256).then((key: any) => { encrypt_key = key; console.log ("encrypt key : " + encrypt_key); }) } catch (e) { console.error(e) } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// 암호화 private AESEncrypt () { const key = encrypt_key; try { encryptDataIV(plain_string, key, iv_string).then(({ cipher, iv }) => { encrypt_iv = iv; encrypt_string = cipher; console.log ("[encrypt] plain text : " + plain_string); console.log ("[encrypt] encrypt key : " + encrypt_key); console.log ("[encrypt] iv : " + encrypt_iv); console.log ("[encrypt] encrypt text : " + encrypt_string); }).catch((error: any) => {}) } catch (e) { console.error(e) } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// 복호화 private async AESDecrypt () { const key = encrypt_key; const iv = encrypt_iv; const cipher = encrypt_string; try { var decrypt_string = await decryptData({ cipher, iv }, key); console.log ("[decrypt] encrypt text : " + cipher); console.log ("[decrypt] encrypt key : " + key); console.log ("[decrypt] iv : " + iv); console.log ("[decrypt] plain text : " + decrypt_string); } catch (e) { console.error(e) } } |