本文整理汇总了C#中BigInteger.modPow方法的典型用法代码示例。如果您正苦于以下问题:C# BigInteger.modPow方法的具体用法?C# BigInteger.modPow怎么用?C# BigInteger.modPow使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BigInteger
的用法示例。
在下文中一共展示了BigInteger.modPow方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Encrypt
public byte[] Encrypt(byte[] source)
{
BigInteger d = new BigInteger(paramsters.D);
BigInteger n = new BigInteger(paramsters.Modulus);
int sug = 127;
int len = source.Length;
int cycle = 0;
if ((len % sug) == 0) cycle = len / sug; else cycle = len / sug + 1;
ArrayList temp = new ArrayList();
int blockLen = 0;
for (int i = 0; i < cycle; i++)
{
if (len >= sug) blockLen = sug; else blockLen = len;
byte[] context = new byte[blockLen];
int po = i * sug;
Array.Copy(source, po, context, 0, blockLen);
BigInteger biText = new BigInteger(context);
BigInteger biEnText = biText.modPow(d, n);
byte[] b = biEnText.getBytes();
temp.AddRange(b);
len -= blockLen;
}
return (byte[])temp.ToArray(typeof(byte));
}
示例2: 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;
}
示例3: Decrypt
public byte[] Decrypt(byte[] source)
{
BigInteger e = new BigInteger(paramsters.Exponent);
BigInteger n = new BigInteger(paramsters.Modulus);
int bk = 128;
int len = source.Length;
int cycle = 0;
if ((len % bk) == 0) cycle = len / bk; else cycle = len / bk + 1;
ArrayList temp = new ArrayList();
int blockLen = 0;
for (int i = 0; i < cycle; i++)
{
if (len >= bk) blockLen = bk; else blockLen = len;
byte[] context = new byte[blockLen];
int po = i * bk;
Array.Copy(source, po, context, 0, blockLen);
BigInteger biText = new BigInteger(context);
BigInteger biEnText = biText.modPow(e, n);
byte[] b = biEnText.getBytes();
temp.AddRange(b);
len -= blockLen;
}
return (byte[])temp.ToArray(typeof(byte));
}
示例4: ModSqrt
//функция вычисления квадратоного корня по модулю простого числа q
public BigInteger ModSqrt(BigInteger a, BigInteger q)
{
BigInteger b = new BigInteger();
do
{
b.genRandomBits(255, new Random());
} while (Legendre(b, q) == 1);
BigInteger s = 0;
BigInteger t = q - 1;
while ((t & 1) != 1)
{
s++;
t = t >> 1;
}
BigInteger InvA = a.modInverse(q);
BigInteger c = b.modPow(t, q);
BigInteger r = a.modPow(((t + 1) / 2), q);
BigInteger d = new BigInteger();
for (int i = 1; i < s; i++)
{
BigInteger temp = 2;
temp = temp.modPow((s - i - 1), q);
d = (r.modPow(2, q) * InvA).modPow(temp, q);
if (d == (q - 1))
r = (r * c) % q;
c = c.modPow(2, q);
}
return r;
}
示例5: RSATest
//***********************************************************************
// Tests the correct implementation of the modulo exponential function
// using RSA encryption and decryption (using pre-computed encryption and
// decryption keys).
//***********************************************************************
public static void RSATest(int rounds)
{
Random rand = new Random(1);
byte[] val = new byte[64];
// private and public key
BigInteger bi_e = new BigInteger("a932b948feed4fb2b692609bd22164fc9edb59fae7880cc1eaff7b3c9626b7e5b241c27a974833b2622ebe09beb451917663d47232488f23a117fc97720f1e7", 16);
BigInteger bi_d = new BigInteger("4adf2f7a89da93248509347d2ae506d683dd3a16357e859a980c4f77a4e2f7a01fae289f13a851df6e9db5adaa60bfd2b162bbbe31f7c8f828261a6839311929d2cef4f864dde65e556ce43c89bbbf9f1ac5511315847ce9cc8dc92470a747b8792d6a83b0092d2e5ebaf852c85cacf34278efa99160f2f8aa7ee7214de07b7", 16);
BigInteger bi_n = new BigInteger("e8e77781f36a7b3188d711c2190b560f205a52391b3479cdb99fa010745cbeba5f2adc08e1de6bf38398a0487c4a73610d94ec36f17f3f46ad75e17bc1adfec99839589f45f95ccc94cb2a5c500b477eb3323d8cfab0c8458c96f0147a45d27e45a4d11d54d77684f65d48f15fafcc1ba208e71e921b9bd9017c16a5231af7f", 16);
Console.WriteLine("e =\n" + bi_e.ToString(10));
Console.WriteLine("\nd =\n" + bi_d.ToString(10));
Console.WriteLine("\nn =\n" + bi_n.ToString(10) + "\n");
for(int count = 0; count < rounds; count++)
{
// generate data of random length
int t1 = 0;
while(t1 == 0)
t1 = (int)(rand.NextDouble() * 65);
bool done = false;
while(!done)
{
for(int i = 0; i < 64; i++)
{
if(i < t1)
val[i] = (byte)(rand.NextDouble() * 256);
else
val[i] = 0;
if(val[i] != 0)
done = true;
}
}
while(val[0] == 0)
val[0] = (byte)(rand.NextDouble() * 256);
Console.Write("Round = " + count);
// encrypt and decrypt data
BigInteger bi_data = new BigInteger(val, t1);
BigInteger bi_encrypted = bi_data.modPow(bi_e, bi_n);
BigInteger bi_decrypted = bi_encrypted.modPow(bi_d, bi_n);
// compare
if(bi_decrypted != bi_data)
{
Console.WriteLine("\nError at round " + count);
Console.WriteLine(bi_data + "\n");
return;
}
Console.WriteLine(" <PASSED>.");
}
}
示例6: Calculate_Ri
public static BigInteger Calculate_Ri(
BigInteger argG,
BigInteger argP,
BigInteger argTi
)
{
return argG.modPow(argTi, argP);
}
示例7: BI_Generate_Yi
public static BigInteger BI_Generate_Yi(
BigInteger argP,
BigInteger argG,
BigInteger argKi
)
{
return argG.modPow(argKi, argP);
}
示例8: verifySig
public static bool verifySig(BigInteger m, BigInteger s, BigInteger e, BigInteger n)
{
BigInteger mtest = new BigInteger(s.modPow(e, n));
if (m == mtest)
return true;
else
return false;
}
示例9: Calculate_Si
public static BigInteger Calculate_Si(
BigInteger argTi,
BigInteger argHi,
BigInteger argKi,
BigInteger argE,
BigInteger argQ
)
{
return (argTi - (argHi.modPow(1, argQ) * argKi.modPow(1, argQ) * argE.modPow(1, argQ)).modPow(1, argQ));
}
示例10: 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;
}
示例11: verifySig
public static bool verifySig(BigInteger m, BigInteger s, BigInteger e, BigInteger n)
{
// Console.WriteLine("\nE: " + e);
// Console.WriteLine("\nN: " + n);
BigInteger mtest = new BigInteger(s.modPow(e, n));
// Console.WriteLine("\nM: " + m);
// Console.WriteLine("\nMtest: " + mtest);
if (m == mtest)
return true;
else
return false;
}
示例12: DecryptSymmetricKey
public string DecryptSymmetricKey(string encrSymmKey, BigInteger d, BigInteger n)
{
BigInteger encryptedSymmKey = new BigInteger(Convert.FromBase64String(encrSymmKey));
//can be redone with RSA for efficiency
BigInteger decryptedSymmKey = encryptedSymmKey.modPow(d, n);
byte[] symmKeyBytes = decryptedSymmKey.getBytes();
string symmKey = Encoding.ASCII.GetString(symmKeyBytes);
return symmKey;
}
示例13: createSig
public static BigInteger createSig(BigInteger m, rsakey rk)
{
BigInteger d = rk.getk();
BigInteger n = rk.getn();
return m.modPow(d, n);
}
示例14: RSATest2
//***********************************************************************
// Tests the correct implementation of the modulo exponential and
// inverse modulo functions using RSA encryption and decryption. The two
// pseudoprimes p and q are fixed, but the two RSA keys are generated
// for each round of testing.
//***********************************************************************
public static void RSATest2(int rounds)
{
Random rand = new Random();
byte[] val = new byte[64];
byte[] pseudoPrime1 = {
(byte)0x85, (byte)0x84, (byte)0x64, (byte)0xFD, (byte)0x70, (byte)0x6A,
(byte)0x9F, (byte)0xF0, (byte)0x94, (byte)0x0C, (byte)0x3E, (byte)0x2C,
(byte)0x74, (byte)0x34, (byte)0x05, (byte)0xC9, (byte)0x55, (byte)0xB3,
(byte)0x85, (byte)0x32, (byte)0x98, (byte)0x71, (byte)0xF9, (byte)0x41,
(byte)0x21, (byte)0x5F, (byte)0x02, (byte)0x9E, (byte)0xEA, (byte)0x56,
(byte)0x8D, (byte)0x8C, (byte)0x44, (byte)0xCC, (byte)0xEE, (byte)0xEE,
(byte)0x3D, (byte)0x2C, (byte)0x9D, (byte)0x2C, (byte)0x12, (byte)0x41,
(byte)0x1E, (byte)0xF1, (byte)0xC5, (byte)0x32, (byte)0xC3, (byte)0xAA,
(byte)0x31, (byte)0x4A, (byte)0x52, (byte)0xD8, (byte)0xE8, (byte)0xAF,
(byte)0x42, (byte)0xF4, (byte)0x72, (byte)0xA1, (byte)0x2A, (byte)0x0D,
(byte)0x97, (byte)0xB1, (byte)0x31, (byte)0xB3,
};
byte[] pseudoPrime2 = {
(byte)0x99, (byte)0x98, (byte)0xCA, (byte)0xB8, (byte)0x5E, (byte)0xD7,
(byte)0xE5, (byte)0xDC, (byte)0x28, (byte)0x5C, (byte)0x6F, (byte)0x0E,
(byte)0x15, (byte)0x09, (byte)0x59, (byte)0x6E, (byte)0x84, (byte)0xF3,
(byte)0x81, (byte)0xCD, (byte)0xDE, (byte)0x42, (byte)0xDC, (byte)0x93,
(byte)0xC2, (byte)0x7A, (byte)0x62, (byte)0xAC, (byte)0x6C, (byte)0xAF,
(byte)0xDE, (byte)0x74, (byte)0xE3, (byte)0xCB, (byte)0x60, (byte)0x20,
(byte)0x38, (byte)0x9C, (byte)0x21, (byte)0xC3, (byte)0xDC, (byte)0xC8,
(byte)0xA2, (byte)0x4D, (byte)0xC6, (byte)0x2A, (byte)0x35, (byte)0x7F,
(byte)0xF3, (byte)0xA9, (byte)0xE8, (byte)0x1D, (byte)0x7B, (byte)0x2C,
(byte)0x78, (byte)0xFA, (byte)0xB8, (byte)0x02, (byte)0x55, (byte)0x80,
(byte)0x9B, (byte)0xC2, (byte)0xA5, (byte)0xCB,
};
BigInteger bi_p = new BigInteger(pseudoPrime1);
BigInteger bi_q = new BigInteger(pseudoPrime2);
BigInteger bi_pq = (bi_p-1)*(bi_q-1);
BigInteger bi_n = bi_p * bi_q;
for(int count = 0; count < rounds; count++)
{
// generate private and public key
BigInteger bi_e = bi_pq.genCoPrime(512, rand);
BigInteger bi_d = bi_e.modInverse(bi_pq);
Console.WriteLine("\ne =\n" + bi_e.ToString(10));
Console.WriteLine("\nd =\n" + bi_d.ToString(10));
Console.WriteLine("\nn =\n" + bi_n.ToString(10) + "\n");
// generate data of random length
int t1 = 0;
while(t1 == 0)
t1 = (int)(rand.NextDouble() * 65);
bool done = false;
while(!done)
{
for(int i = 0; i < 64; i++)
{
if(i < t1)
val[i] = (byte)(rand.NextDouble() * 256);
else
val[i] = 0;
if(val[i] != 0)
done = true;
}
}
while(val[0] == 0)
val[0] = (byte)(rand.NextDouble() * 256);
Console.Write("Round = " + count);
// encrypt and decrypt data
BigInteger bi_data = new BigInteger(val, t1);
BigInteger bi_encrypted = bi_data.modPow(bi_e, bi_n);
BigInteger bi_decrypted = bi_encrypted.modPow(bi_d, bi_n);
// compare
if(bi_decrypted != bi_data)
{
Console.WriteLine("\nError at round " + count);
Console.WriteLine(bi_data + "\n");
return;
}
Console.WriteLine(" <PASSED>.");
}
}
示例15: SolovayStrassenTest
//***********************************************************************
// Probabilistic prime test based on Solovay-Strassen (Euler Criterion)
//
// p is probably prime if for any a < p (a is not multiple of p),
// a^((p-1)/2) mod p = J(a, p)
//
// where J is the Jacobi symbol.
//
// Otherwise, p is composite.
//
// Returns
// -------
// True if "this" is a Euler pseudoprime to randomly chosen
// bases. The number of chosen bases is given by the "confidence"
// parameter.
//
// False if "this" is definitely NOT prime.
//
//***********************************************************************
public bool SolovayStrassenTest(int confidence)
{
BigInteger thisVal;
if((this.data[maxLength-1] & 0x80000000) != 0) // negative
thisVal = -this;
else
thisVal = this;
if(thisVal.dataLength == 1)
{
// test small numbers
if(thisVal.data[0] == 0 || thisVal.data[0] == 1)
return false;
else if(thisVal.data[0] == 2 || thisVal.data[0] == 3)
return true;
}
if((thisVal.data[0] & 0x1) == 0) // even numbers
return false;
int bits = thisVal.bitCount();
BigInteger a = new BigInteger();
BigInteger p_sub1 = thisVal - 1;
BigInteger p_sub1_shift = p_sub1 >> 1;
Random rand = new Random();
for(int round = 0; round < confidence; round++)
{
bool done = false;
while(!done) // generate a < n
{
int testBits = 0;
// make sure "a" has at least 2 bits
while(testBits < 2)
testBits = (int)(rand.NextDouble() * bits);
a.genRandomBits(testBits, rand);
int byteLen = a.dataLength;
// make sure "a" is not 0
if(byteLen > 1 || (byteLen == 1 && a.data[0] != 1))
done = true;
}
// check whether a factor exists (fix for version 1.03)
BigInteger gcdTest = a.gcd(thisVal);
if(gcdTest.dataLength == 1 && gcdTest.data[0] != 1)
return false;
// calculate a^((p-1)/2) mod p
BigInteger expResult = a.modPow(p_sub1_shift, thisVal);
if(expResult == p_sub1)
expResult = -1;
// calculate Jacobi symbol
BigInteger jacob = Jacobi(a, thisVal);
//Console.WriteLine("a = " + a.ToString(10) + " b = " + thisVal.ToString(10));
//Console.WriteLine("expResult = " + expResult.ToString(10) + " Jacob = " + jacob.ToString(10));
// if they are different then it is not prime
if(expResult != jacob)
return false;
}
return true;
}