110,534
社区成员
发帖
与我相关
我的任务
分享
BE be = new BE ();
string key = "EB2A38568661887FA180BDDB5CABD5F21C7BFD59C090CB2D245A87AC253062882729293E5506350508E7F9AA3BB77F4333231490F915F6D63C55FE2F08A49B353F444AD3993CACC02DB784ABBB8E42A9B1BBFFFB38BE18D78E87A0E41B9B8F73A928EE0CCEE1F6739884B9777E4FE9E88A1BBE495927AC4A799B3181D6442443";
string s = BE.Encrypt("AA", key, "10001");
int l = s.Length;
package cn.zju.topicproject.crawler;
import java.math.BigInteger;
import java.net.URLEncoder;
import java.util.Random;
import org.apache.commons.codec.binary.Base64;
public class SinaEncoder {
private static BigInteger n = null;
private static BigInteger e = null;
private static BigInteger pkcs1pad2(String s, int n){
if(n < s.length() + 11) { // TODO: fix for utf-8
System.out.println("Message too long for ");
return null;
}
byte[] ba = new byte[n];
int i = s.length()-1;
while(i >= 0 && n > 0) {
int c = s.codePointAt(i--);
if(c < 128) { // encode using utf-8
ba[--n] = new Byte(String.valueOf(c));
}
else if((c > 127) && (c < 2048)) {
ba[--n] = new Byte(String.valueOf((c & 63) | 128));
ba[--n] = new Byte(String.valueOf((c >> 6) | 192));
}
else {
ba[--n] = new Byte(String.valueOf((c & 63) | 128));
ba[--n] = new Byte(String.valueOf(((c >> 6) & 63) | 128));
ba[--n] = new Byte(String.valueOf((c >> 12) | 224));
}
}
ba[--n] = new Byte("0");
byte[] temp = new byte[1];
Random rdm = new Random(47L);
while(n > 2) { // random non-zero pad
temp[0] = new Byte("0");
while(temp[0] == 0)
rdm.nextBytes(temp);
ba[--n] = temp[0];
}
ba[--n] = 2;
ba[--n] = 0;
return new BigInteger(ba);
}
private static BigInteger DoPublic(BigInteger x){
return x.modPow(e, n);
}
public static String Encrypt(String pwd, String nStr, String eStr){
n = new BigInteger(nStr,16);
e = new BigInteger(eStr,16);
BigInteger r = DoPublic(pkcs1pad2(pwd,(n.bitLength()+7)>>3));
String sp = r.toString(16);
if((sp.length()&1) != 0 )
sp = "0" + sp;
return sp;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Numerics;
using System.Text;
namespace Spider.Test
{
/// <summary>
///Class1 的摘要说明
/// </summary>
public class BE
{
public ()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
private static BigInteger n = new BigInteger();
private static BigInteger e = new BigInteger();
private static BigInteger pkcs1pad2(String s, int n)
{
if (n < s.Length + 11)
{ // TODO: fix for utf-8
//System.out.println("Message too long for ");
return 0;
}
byte[] ba = new byte[n];
int i = s.Length - 1;
while (i >= 0 && n > 0)
{
int c = s.IndexOf((i--).ToString());
if (c < 128)
{ // encode using utf-8
ba[--n] = byte.Parse(c.ToString());
}
else if ((c > 127) && (c < 2048))
{
ba[--n] = byte.Parse(((c & 63) | 128).ToString());
ba[--n] = byte.Parse(((c >> 6) | 192).ToString());
}
else
{
ba[--n] = byte.Parse(((c & 63) | 128).ToString());
ba[--n] = byte.Parse((((c >> 6) & 63) | 128).ToString());
ba[--n] = byte.Parse(((c >> 12) | 224).ToString());
}
} ba[--n] = byte.Parse("0");
byte[] temp = new byte[1];
Random rdm = new Random(47);
while (n > 2)
{ // random non-zero pad
temp[0] = byte.Parse("0");
while (temp[0] == 0)
rdm.NextBytes(temp);
ba[--n] = temp[0];
} ba[--n] = 2;
ba[--n] = 0;
return new BigInteger(ba);
}
private static BigInteger DoPublic(BigInteger x)
{
return BigInteger.ModPow(x, e, n);
}
/// <summary>
/// Base64加密
/// </summary>
/// <param name="codeName">加密采用的编码方式</param>
/// <param name="source">待加密的明文</param>
/// <returns></returns>
public static string EncodeBase64(Encoding encode, string source)
{
byte[] bytes = encode.GetBytes(source);
try
{
source = Convert.ToBase64String(bytes);
}
catch
{
source = source;
}
return source;
}
/// <summary>
/// Base64加密,采用utf8编码方式加密
/// </summary>
/// <param name="source">待加密的明文</param>
/// <returns>加密后的字符串</returns>
public static string EncodeBase64(string source)
{
return EncodeBase64(Encoding.UTF8, source);
}
public static String Encrypt(String pwd, String nStr, String eStr)
{
n = BigInteger.Parse(nStr, System.Globalization.NumberStyles.AllowHexSpecifier);
e = BigInteger.Parse(eStr, System.Globalization.NumberStyles.AllowHexSpecifier);
BigInteger r = DoPublic(pkcs1pad2(pwd, (n.ToByteArray().Length + 7) >> 3));
String sp = r.ToString(System.Globalization.NumberStyles.AllowHexSpecifier.ToString());
if ((sp.Length & 1) != 0)
sp = "0" + sp;
return sp;
}
}
}