本文介绍了如何在客户端加密和在服务器端解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我使用以下代码在客户端加密 function Encrypt(key,str){ var s = [],j = 0 ,x,res = ' '; for ( var i = 0 ; i < 256 ; i ++){s [i] = i; } for (i = 0 ; i < 256 ; i ++){j =(j + s [i] + key.charCodeAt(i%key。长度))% 256 ; x = s [i]; s [i] = s [j]; s [j] = x; } i = 0 ; j = 0 ; for ( var y = 0 ; y < str.length; y ++){i =(i + 1 )% 256 ; j =(j + s [i])% 256 ; x = s [i]; s [i] = s [j]; s [j] = x; res + = String .fromCharCode(str.charCodeAt(y)^ s [(s [i] + s [j])% 256 ]); } return res; } 现在我想在服务器端解密加密值以上的值,我使用下面的代码 string decryptValue = string .Empty; UTF8Encoding encodepwd = new UTF8Encoding(); Decoder Decode = encodepwd.GetDecoder(); byte [] todecode_byte = Convert.FromBase64String(encryptValue); int charCount = Decode.GetCharCount(todecode_byte, 0 ,todecode_byte.Length); char [] decoding_char = new char [charCount]; Decode.GetChars(todecode_byte, 0 ,todecode_byte.Length,decoding_char, 0 ); decryptValue = new String (decoding_char); 我必须在服务器端代码中给出以下错误 输入不是有效的Base-64字符串,因为它包含非基本64个字符,两个以上的填充字符或填充字符中的非空白字符。 解决方案 因为你的加密代码中没有,你创建一个base64字符串。 你好你能试试吗? http://ntt.cc/2008/01/19/base64-encoder-decoder-with-javascript.html [ ^ ] http://stackoverflow.com/questions/2820249/base64-encoding-and-decoding-in-client-side-javascript [ ^ ] I have encrypt on client side using following codefunction Encrypt(key, str) {var s = [], j = 0, x, res = '';for (var i = 0; i < 256; i++) {s[i] = i;}for (i = 0; i < 256; i++) {j = (j + s[i] + key.charCodeAt(i % key.length)) % 256;x = s[i];s[i] = s[j];s[j] = x;}i = 0;j = 0;for (var y = 0; y < str.length; y++) {i = (i + 1) % 256;j = (j + s[i]) % 256;x = s[i];s[i] = s[j];s[j] = x;res += String.fromCharCode(str.charCodeAt(y) ^ s[(s[i] + s[j]) % 256]);}return res;}now I want to decrypt value of encrypted above value on server side and I have use following codestring decryptValue = string.Empty;UTF8Encoding encodepwd = new UTF8Encoding();Decoder Decode = encodepwd.GetDecoder();byte[] todecode_byte = Convert.FromBase64String(encryptValue);int charCount = Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);char[] decoded_char = new char[charCount];Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);decryptValue = new String(decoded_char);I have to give following error in server side codeThe input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or a non-white space character among the padding characters. 解决方案 Because nowhere in your "encryption" code, you create a base64 string.Hi Can you try this ?http://ntt.cc/2008/01/19/base64-encoder-decoder-with-javascript.html[^]http://stackoverflow.com/questions/2820249/base64-encoding-and-decoding-in-client-side-javascript[^] 这篇关于如何在客户端加密和在服务器端解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-20 07:48
查看更多