本文整理汇总了C#中BigInteger.getBytes方法的典型用法代码示例。如果您正苦于以下问题:C# BigInteger.getBytes方法的具体用法?C# BigInteger.getBytes怎么用?C# BigInteger.getBytes使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BigInteger
的用法示例。
在下文中一共展示了BigInteger.getBytes方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Validate
public bool Validate()
{
BigInteger calculatedHash = new BigInteger(this.ToString(), _RADIX);
bool ret = true;
//Calculate the hash
calculatedHash = calculatedHash.modPow(new BigInteger(17), _n);
//Compare our real hash and the calculated hash
byte[] ourHash = _hash.getBytes();
byte[] myHash = calculatedHash.getBytes();
if (ourHash.Length == myHash.Length)
{
for (int i = 0; i <= ourHash.Length - 1; i++)
{
if (ourHash[i] != myHash[i])
{
ret = false;
break; // TODO: might not be correct. Was : Exit For
}
}
}
else
{
//Not even the right length, it's crap
ret = false;
}
return ret;
}
示例2: EncryptSymmetricKey
public string EncryptSymmetricKey(string symmKey, BigInteger e, BigInteger n)
{
BigInteger cryptoSymmKey = new BigInteger(Convert.FromBase64String(symmKey));
cryptoSymmKey = cryptoSymmKey.modPow(e,n);
string symmKeyString = Convert.ToBase64String(cryptoSymmKey.getBytes());
return symmKeyString;
}
示例3: SignWithSHA1
public byte[] SignWithSHA1(byte[] data) {
byte[] hash = new SHA1CryptoServiceProvider().ComputeHash(data);
byte[] buf = new byte[hash.Length + PKIUtil.SHA1_ASN_ID.Length];
Array.Copy(PKIUtil.SHA1_ASN_ID, 0, buf, 0, PKIUtil.SHA1_ASN_ID.Length);
Array.Copy(hash, 0, buf, PKIUtil.SHA1_ASN_ID.Length, hash.Length);
BigInteger x = new BigInteger(buf);
//Debug.WriteLine(x.ToHexString());
int padLen = (_publickey._n.bitCount() + 7) / 8;
x = RSAUtil.PKCS1PadType1(x, padLen);
byte[] result = Sign(x.getBytes());
return result;
}
示例4: WriteBigIntWithBits
public void WriteBigIntWithBits(BigInteger bi)
{
WriteInt32(bi.bitCount());
Write(bi.getBytes());
}
示例5: WriteBigInteger
//writes mpint in SSH2 format
public override void WriteBigInteger(BigInteger data)
{
byte[] t = data.getBytes();
int len = t.Length;
if (t[0] >= 0x80) {
WriteInt32(++len);
WriteByte((byte)0);
}
else
WriteInt32(len);
Write(t);
}
示例6: StripPKCS1Pad
public static BigInteger StripPKCS1Pad(BigInteger input, int type) {
byte[] strip = input.getBytes();
int i;
if (strip[0] != type)
throw new Exception(String.Format("Invalid PKCS1 padding {0}", type));
for (i = 1; i < strip.Length; i++) {
if (strip[i] == 0)
break;
if (type == 0x01 && strip[i] != (byte)0xff)
throw new Exception("Invalid PKCS1 padding, corrupt data");
}
if (i == strip.Length)
throw new Exception("Invalid PKCS1 padding, corrupt data");
byte[] val = new byte[strip.Length - i];
Array.Copy(strip, i, val, 0, val.Length);
return new BigInteger(val);
}
示例7: ProcessDataReceived
//.........这里部分代码省略.........
rb = Reverse( b );
BigInteger bi = new BigInteger( x );
BigInteger bi2 = new BigInteger( rN );
BigInteger g = new BigInteger( new byte[] { 7 } );
v = g.modPow( bi, bi2 );
K = new BigInteger( new Byte[] { 3 } );
BigInteger temp1 = K * v;
BigInteger temp2 = g.modPow( new BigInteger( rb ), new BigInteger( rN ) );
BigInteger temp3 = temp1 + temp2;
B = temp3 % new BigInteger( rN );
/* byte []ezfd= B.getBytes();
Console.WriteLine("B");
HexViewer.View( ezfd, 0, ezfd.Length );
BigInteger C = new BigInteger();
Console.WriteLine("C/Rn {0}", temp3/new BigInteger( rN ) );*/
//Console.WriteLine("temp1 {0}",temp1.ToHexString());
//Console.WriteLine("temp2 {0}",temp2.ToHexString());
//Console.WriteLine("temp3 {0}",temp3.ToHexString());
/* for(int ll = 0;ll < 6;ll++)
{
C = B;
C += new BigInteger( rN ) * ll;
C -= temp1;
Console.WriteLine("temp3 {0}",C.ToHexString());
}*/
byte []pack = new byte[ 118 ];
//byte[] pack = new byte[119];
pack[ 0 ] = pack[ 1 ] = 0;
byte []tB = Reverse( B.getBytes() );
for( t = 0;t < tB.Length ;t++ )
pack[ 3 + t ] = tB[ t ];
pack[ 35 ] = 1;// g_length
pack[ 36 ] = 7;// g
pack[ 37 ] = 32;// n_len
for( t = 0;t < N.Length;t++ )
pack[ 38 + t ] = N[ t ];
for( t = 0;t < salt.Length ;t++ )
pack[ 70 + t ] = salt[ t ];
for( t = 0;t < 16;t++ )
//for (t = 0; t < 17; t++)
pack[ 102 + t ] = 0;
return pack;
case 0x01:// Logon proof
{
//Console.WriteLine("Logon proof" );
byte []A = new byte[ 32 ];
for( t = 0;t < 32;t++ )
{
A[ t ] = data[ t + 1 ];
}
byte []kM1 = new byte[ 20 ];
for( t = 0;t < 20;t++ )
{
kM1[ t ] = data[ t + 1 + 32 ];
}
//A = new byte[] { 0x23, 0x2f, 0xb1, 0xb8, 0x85, 0x29, 0x64, 0x3d, 0x95, 0xb8, 0xdc, 0xe7, 0x8f, 0x27, 0x50, 0xc7, 0x5b, 0x2d, 0xf3, 0x7a, 0xcb, 0xa8, 0x73, 0xeb, 0x31, 0x07, 0x38, 0x39, 0xed, 0xa0, 0x73, 0x8d };
byte []rA = Reverse( A );
// B = new BigInteger( new byte[] { 0x64, 0x5d, 0x1f, 0x78, 0x97, 0x30, 0x73, 0x70, 0x1e, 0x12, 0xbc, 0x98, 0xaa, 0x38, 0xea, 0x99, 0xb4, 0xbc, 0x43, 0x5c, 0x32, 0xe8, 0x44, 0x7c, 0x73, 0xab, 0x07, 0x7a, 0xe4, 0xd7, 0x59, 0x64 } );
示例8: bigint_to_b64
// Convert a big integer to a base 64 string
private string bigint_to_b64 (BigInteger bi)
{
byte [] b = bi.getBytes ();
return Convert.ToBase64String (b);
}
示例9: DoPrivate
// Perform an RSA private key operation on input
public byte [] DoPrivate (byte [] input)
{
if (input.Length != this.keylen)
throw new ArgumentException ("input.Length does not match keylen");
if (ReferenceEquals (this.D, null))
throw new ArgumentException ("no private key set!");
BigInteger T = new BigInteger (input);
if (T >= this.N)
throw new ArgumentException ("input exceeds modulus");
T = T.modPow (this.D, this.N);
byte [] b = T.getBytes ();
return pad_bytes (b, this.keylen);
}
示例10: EncodeMessage
/// <summary>
/// Adds padding to the input data and returns the padded data.
/// </summary>
/// <param name="dataBytes">Data to be padded prior to encryption</param>
/// <param name="parameters">RSA Parameters used for padding computation</param>
/// <returns>Padded message</returns>
public byte[] EncodeMessage( byte[] dataBytes, RSAParameters parameters )
{
//Determine if we can add padding.
if( dataBytes.Length > GetMaxMessageLength( parameters ) ) {
throw new Exception( "Data length is too long. Increase your key size or consider encrypting less data." );
}
int padLength = parameters.N.Length - dataBytes.Length - 3;
BigInteger biRnd = new BigInteger();
biRnd.genRandomBits( padLength * 8, new Random( DateTime.Now.Millisecond ) );
byte[] bytRandom = null;
bytRandom = biRnd.getBytes();
int z1 = bytRandom.Length;
//Make sure the bytes are all > 0.
for( int i = 0; i <= bytRandom.Length - 1; i++ ) {
if( bytRandom[i] == 0x00 ) {
bytRandom[i] = 0x01;
}
}
byte[] result = new byte[parameters.N.Length];
//Add the starting 0x00 byte
result[0] = 0x00;
//Add the version code 0x02 byte
result[1] = 0x02;
for( int i = 0; i <= bytRandom.Length - 1; i++ ) {
z1 = i + 2;
result[z1] = bytRandom[i];
}
//Add the trailing 0 byte after the padding.
result[bytRandom.Length + 2] = 0x00;
//This starting index for the unpadded data.
int idx = bytRandom.Length + 3;
//Copy the unpadded data to the padded byte array.
dataBytes.CopyTo( result, idx );
return result;
}
示例11: SignWithSHA1
public byte[] SignWithSHA1([ReadOnlyArray()] byte[] data)
{
byte[] hash = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha1).HashData(data.AsBuffer()).ToArray();
byte[] buf = new byte[hash.Length + PKIUtil.SHA1_ASN_ID.Length];
Array.Copy(PKIUtil.SHA1_ASN_ID, 0, buf, 0, PKIUtil.SHA1_ASN_ID.Length);
Array.Copy(hash, 0, buf, PKIUtil.SHA1_ASN_ID.Length, hash.Length);
BigInteger x = new BigInteger(buf);
//Debug.WriteLine(x.ToHexString());
int padLen = (_publickey._n.bitCount() + 7) / 8;
x = RSAUtil.PKCS1PadType1(x, padLen);
byte[] result = Sign(x.getBytes());
return result;
}
示例12: pkcs1unpad2
private byte[] pkcs1unpad2(BigInteger m, int b)
{
byte[] bytes = m.getBytes();
int i = 0;
while (i < bytes.Length && bytes[i] == 0) ++i;
if (bytes.Length - i != (b - 1) || bytes[i] != 0x2)
{
return null;
}
while (bytes[i] != 0)
{
if (++i >= bytes.Length)
{
return null;
}
}
byte[] result = new byte[bytes.Length - i + 1];
int p = 0;
while (++i < bytes.Length)
{
result[p++] = bytes[i];
}
return result;
}
示例13: HandleLogonProof
private void HandleLogonProof(IPacket packet)
{
BinaryReader gr = packet.CreateReader();
var bi_A = new BigInteger(gr.ReadBytes(32).Reverse());
var bi_M1 = new BigInteger(gr.ReadBytes(20).Reverse());
byte[] u = H(bi_A.getBytes().Reverse().Concat(bi_B.getBytes().Reverse()));
var bi_u = new BigInteger(u.Reverse());
BigInteger bi_Temp2 = (bi_A * bi_v.modPow(bi_u, bi_N)) % bi_N; // v^u
BigInteger bi_S = bi_Temp2.modPow(bi_b, bi_N); // (Av^u)^b
byte[] S = bi_S.getBytes().Reverse();
var S1 = new byte[16];
var S2 = new byte[16];
for(int i = 0; i < 16; i++) {
S1[i] = S[i * 2];
S2[i] = S[i * 2 + 1];
}
var SS_Hash = new byte[40];
byte[] S1_Hash = H(S1);
byte[] S2_Hash = H(S2);
for(int i = 0; i < 20; i++) {
SS_Hash[i * 2] = S1_Hash[i];
SS_Hash[i * 2 + 1] = S2_Hash[i];
}
_account.SessionKey = (byte[])SS_Hash.Clone();
var accountRepository = IoC.Resolve<IAccountRepository>();
accountRepository.Save(_account);
accountRepository.SubmitChanges();
byte[] N_Hash = H(bi_N.getBytes().Reverse());
byte[] G_Hash = H(bi_g.getBytes().Reverse());
for(int i = 0; (i < 20); i++) {
N_Hash[i] ^= G_Hash[i];
}
byte[] userHash = H(Encoding.UTF8.GetBytes(ClientInfo.AccountName));
IEnumerable<byte> temp = N_Hash
.Concat(userHash)
.Concat(bi_s.getBytes().Reverse())
.Concat(bi_A.getBytes().Reverse())
.Concat(bi_B.getBytes().Reverse())
.Concat(SS_Hash);
var biM1Temp = new BigInteger(H(temp).Reverse());
if(biM1Temp != bi_M1) {
_client.Send(GetLogonProof());
return;
}
temp = bi_A.getBytes().Reverse()
.Concat(biM1Temp.getBytes().Reverse());
temp = temp.Concat(SS_Hash);
byte[] M2 = H(temp);
_client.Send(GetLogonProof(M2));
}