JS使用base62加密,在c#解密
JS代码:
var Base62 = {
encode: function(str) //加密
{
var lst=[]; //字符串转成数组---我的是des3加密返回的字符串
var leadingZeroCount = 0;
var kr = 0;
for (var i = 0; i < str.length; i++) {
lst.push(str.charCodeAt(i));
if (str.charCodeAt(i) == 0 && kr == 0) {
leadingZeroCount = leadingZeroCount + 1;
} else {
kr = 1;
}
}
var out = '';
var shu = []; //转62进制数组
var count;
var ci=0;
while ((count = lst.length) > 0){
var shu1=[];
var aoc=0;
for (var i = 0; i != count; i++) {
var o1=lst[i]+aoc*256;
var o2=parseInt(o1/62);
aoc=o1%62;
if(shu1.length>0 || o2>0){
shu1.push(o2);
}
}
ci=ci+1;
shu.insert(0,aoc);
lst=shu1;
}
//首多个为0递增
for (var i = 0; i < leadingZeroCount; i++) {
shu.insert(0, 0);
}
//对应字符转换
for(var i=0;i<shu.length;i++){
out+=this.string10to62(shu[i]);
}
return out;
},
string10to62(number) {
var chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ'.split(''),
radix = chars.length,
qutient = +number,
arr = [];
do {
mod = qutient % radix;
qutient = (qutient - mod) / radix;
arr.unshift(chars[mod]);
} while (qutient);
return arr.join('');
}
};
Array.prototype.insert = function(index, value){ this.splice(index,0, value); }
使用:Base62.encode(ji); ji为des3加密返回的字符串
c#部分:
public static class Base62
{
private const string DefaultCharacterSet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ";
private const string InvertedCharacterSet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ";
/// <summary>
/// Encode a byte array with Base62
/// </summary>
/// <param name="original">Byte array</param>
/// <param name="inverted">Use inverted character set</param>
/// <returns>Base62 string</returns>
public static string ToBase62(byte[] original, bool inverted = false)
{
var characterSet = inverted ? InvertedCharacterSet : DefaultCharacterSet;
var arr = Array.ConvertAll(original, t => (int)t);
var converted = BaseConvert(arr, 256, 62);
var builder = new StringBuilder();
foreach (var t in converted)
{
builder.Append(characterSet[t]);
}
return builder.ToString();
}
/// <summary>
/// Decode a base62-encoded string
/// </summary>
/// <param name="base62">Base62 string</param>
/// <param name="inverted">Use inverted character set</param>
/// <returns>Byte array</returns>
public static byte[] FromBase62(string base62, bool inverted = false)
{
if (string.IsNullOrWhiteSpace(base62))
{
throw new ArgumentNullException(nameof(base62));
}
var characterSet = inverted ? InvertedCharacterSet : DefaultCharacterSet;
var arr = Array.ConvertAll(base62.ToCharArray(), characterSet.IndexOf);
var converted = BaseConvert(arr, 62, 256);
return Array.ConvertAll(converted, Convert.ToByte);
}
private static int[] BaseConvert(int[] source, int sourceBase, int targetBase)
{
var result = new List<int>();
var leadingZeroCount = Math.Min(source.TakeWhile(x => x == 0).Count(), source.Length - 1);
int count;
while ((count = source.Length) > 0)
{
var quotient = new List<int>();
var remainder = 0;
for (var i = 0; i != count; i++)
{
var accumulator = source[i] + remainder * sourceBase;
var digit = accumulator / targetBase;
remainder = accumulator % targetBase;
if (quotient.Count > 0 || digit > 0)
{
quotient.Add(digit);
}
}
result.Insert(0, remainder);
source = quotient.ToArray();
}
result.InsertRange(0, Enumerable.Repeat(0, leadingZeroCount));
return result.ToArray();
}
}
使用:
// 字节数组转base62
var guid = Guid.Parse("40943a11e5434d2f869d4e566e48c4f2");
Console.WriteLine(Base62.ToBase62(guid.ToByteArray()));
// base62转字节数组
var str = "WVdhFoDbSqBBYSKzptnko";
var bytes = Base62.FromBase62(str);
var guid = new Guid(bytes);
Console.WriteLine(guid.ToString("N"));