简体   繁体   English

如何使用 base64 加密在 react-native 中加密密码,我是 react-native 的新手

[英]how do i encrypt the password in react-native using base64 encryption ,i am new to react-native

This is my code for the file named LoginBg<\/code> in which I have to encrypt the password.这是我必须在其中加密密码的名为LoginBg<\/code>的文件的代码。 How can I get the text from the input field of password and encrypt that using base64 encoding?如何从密码的输入字段中获取文本并使用 base64 编码对其进行加密?

        export default class LoginBg extends React.Component {

    constructor(props) {
        super(props);
        this.state ={
            username:"",
            password:"",
        }

      }

      _handlePress() {
        console.log(this.state.username);
        console.log(this.state.password);
     }


    render(){

    return (
        <KeyboardAvoidingView behavior="position" style={styles.container}>
            <Text style={styles.heading}>- Login Page -</Text>
            <TextInput keyboardType='email-address' underlineColorAndroid='transparent'  onChangeText={(text) => this.setState({username:text})} placeholder="Email"  placeholderTextColor="#eaf4fc" style={styles.input}/>
            <TextInput secureTextEntry underlineColorAndroid='transparent' onChangeText={(text) => this.setState({password:text})} placeholder="Password" placeholderTextColor="#eaf4fc" style={styles.input}/>
            <Button onPress={() => this._handlePress()}>Login</Button>
            <Button>Register</Button>
        </KeyboardAvoidingView>
    );
    }
   }

You should not do this, Base64 is an encoding, it is not encryption! 您不应该这样做,Base64是编码,不是加密! You will not have added any meaningful security this way. 您将不会以这种方式添加任何有意义的安全性。 Passwords are encrypted server-side (if at all, see side-note below), not in the client. 密码是在服务器端加密的(如果有的话,请参阅下面的旁注),而不是在客户端。 Even if you would actually have encrypted the password client-side and sent that then essentially the encrypted password becomes the new plain-text password. 即使您实际上已经加密了客户端的密码并发送了该密码,从本质上讲,加密的密码也变成了新的纯文本密码。 If any of this is not clear to you, do some more research before implementing security. 如果您不清楚其中的任何一个,请在实施安全性之前进行更多研究。

Side note: Even the use for encrypting passwords server-side is limited, generally only if you need it to interact with other systems so you encrypt it before storing it and decrypt before using it. 旁注:即使仅在需要与服务器端交互时才限制服务器端用于加密密码,因此在存储前先对其进行加密,然后在使用前对其进行解密。 It doesn't add a lot of security if the decryption key is also stored in the same environment. 如果解密密钥也存储在同一环境中,则不会增加很多安全性。 For your own users that log into your system, the passwords should be hashed instead which is a 1-way function unlike encryption. 对于您自己的登录系统的用户,应该对密码进行哈希处理 ,这是一种不同于加密的单向功能。

You can use a third-party base64 module in this case 在这种情况下,您可以使用第三方base64模块

Installation 安装

$ npm install --save js-base64 $ npm install-保存js-base64

Usage 用法

import { Base64 } from 'js-base64';

Base64.encode('dankogai');  // ZGFua29nYWk=
yarn add js-base64<\/code><\/pre><\/blockquote>
 import { Base64 } from 'js-base64'; const encode = Base64.encode('hello'); console.log(encode); \/\/ encoded value const decode = Base64.decode(encode); console.log(decode); \/\/ hello<\/code><\/pre>

If you are using a database like firebase, I would recommend saving your data in a variable like this:如果您使用的是像 firebase 这样的数据库,我建议您将数据保存在这样的变量中:

 const encode = function (day) { return Base64.encode(day); }; const sendMessage = () => { addDoc(collection(db, 'matches', matchDetails.id, 'messages'), { timestamp: serverTimestamp(), userId: user.uid, displayName: user.displayName, photoURL: matchDetails.users[user.uid].photoURL, messageEncrypted: encode(input), }); setInput(''); };<\/code><\/pre>

To decode it simply use a helper function like this:要对其进行解码,只需使用如下辅助函数:

 const decode = function (week) { return Base64.decode(week); };<\/code><\/pre>

Then decode your data by calling the helper function like so:然后通过调用辅助函数来解码您的数据,如下所示:

 <Text style={tw('text-white')}>{decode(message.messageEncrypted)}<\/Text><\/code><\/pre>"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 React-Native 中将 base64 url 转换为图像(png/jpg)文件? - How can I convert a base64 url to an image(png/jpg) file, in React-Native? 我如何在无状态函数 React-Native 中使用 refs,我正在使用 React-Native 0.62.2 - How can i user refs in stateless functions React-Native, am using React-Native 0.62.2 React-Native:将图像 url 转换为 base64 字符串 - React-Native: Convert image url to base64 string React-Native(或JS)解码Base64为ZIP文件 - React-Native (or JS) Decode Base64 to ZIP File React-Native:下载映像并将其转换为Base64映像 - React-Native: Download Image and convert it to Base64 Image 在 React-Native(Expo 托管)中给定 base64 字符串,如何在图像/位图的特定坐标处获取像素的颜色? - How can I get a pixel's color at a specific coordinate of an Image/Bitmap given a base64 string in React-Native (Expo managed)? 我如何在react-native中自定义下划线代码 - how do i customize underline code in react-native 如何将值设置为字符串React-Native? - How do I set a value as a string React-Native? 如何更改标记图像本机? - How do I change marker image react-native? React-Native:如何将参数传递给Stacknavigator组件 - React-native: How do i pass parameters to Stacknavigator component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM