[英]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. 对于您自己的登录系统的用户,应该对密码进行哈希处理 ,这是一种不同于加密的单向功能。
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.