## csss

``````    private void decrypt_Click(object sender, EventArgs e)
{
v = Convert.ToDouble (txtencrypt.Text);
t = Math.Pow(v, d);
msg = Convert.ToInt16(Math.Pow(v,d) % Convert.ToDouble(n));
txtdecrypt.Text = Convert.ToString(msg);
}

This is the code im doing for decryption in rsa algorithm but i got a problem in power operation.
For a small power value,the power operation performed well but when im using highest value as a power i got exponential value as a output.
``````

Can we have some example values for `v`, `n` and `d`?

## mshauny 25

Please explain what exactly are using v,n and d for. are they IV's and Keys ?
Did you know you can use .NET building Encryption and Decryption algorithms ?

.NET RSA security

Hope you find it useful.

## tinstaafl 1,045

try this, `(long)Math.Pow(v, d)`

## csss

the value is
v=11
d=23
n=187

When running those figures I recieve:
`t` = 8.95430243255237E+23
`msg` = 149

I presume the issue is coming from the result of `t`?

@tinstaafl using your suggestion just results in the two results above being flipped to negatives and values changed, i.e:
`t` = -9.22337203685478E+18
`msg` = -162

Tested using the code:

``````            double v = 11;
double d = 23;
double n = 187;
double t = (long)Math.Pow(v, d);
Console.WriteLine(t);
Int16 msg = Convert.ToInt16(t % Convert.ToDouble(n));
Console.WriteLine(Convert.ToString(msg));
``````

Note the line `Convert.ToInt16(t % Convert.ToDouble(n));` where I have removed the second power function and used the result of your variable `t` as your just getting the same value twice when it isn't needed.

## tinstaafl 1,045

Using your original code and the numbers you gave, by changing `t` to `decimal` and casting the power function to `decimal` I get `895430243255237000000000` for `t` and `58` for `msg`

``````            double v = 11;
decimal t = (decimal)Math.Pow(v, 23);
double msg = Convert.ToInt16(t % Convert.ToDecimal(187));
Console.WriteLine(Convert.ToString(msg)+" "+t.ToString());
``````

## csss

plain text is 88
cipher text is 11(i.e. "v" value)
d=23,n=187
but while doing the power operation with long data type,im getting the plain text as -162

## csss

im also getting the value 895430243255237000000000 but i need an exact values instead of 000000000. But the normal calculation the answer is 895430243255237`372246531`

## ddanbe 2,720

Perhaps you could consider this.

## tinstaafl 1,045

Good idea ddanbe, this produces the results you want:

First you have to be targeting atleast Framework 4, which means VS 2010 or later, then you have to add a reference to `System.Numerics.dll`, then you have include it `using System.Numerics;`

``````            BigInteger v = 11;
BigInteger t = BigInteger.Pow(v,23);
BigInteger msg = (BigInteger.Remainder(t,187));
Console.WriteLine(Convert.ToString(msg)+" "+t.ToString());
``````

I think the problem with using the `Math.Pow` method is, it only returns a double, which limits it's accuracy on really large number like this.

## TnTinMN 418

If you can live with the 28 digit accuracy of the decimal type for the result of the exponentiation, then you can implement your own power function.

``````    private void test()
{
Int32 v = 11;
Int32 d = 23;
Int32 n = 187;

decimal exp = DecPower(v, d);

decimal m = decimal.Remainder(exp, n);
string msg = m.ToString();

}

private decimal DecPower(decimal Num, Int32 power)
{
decimal ret = 1;
for (Int32 i = 1; i <= power; i++)
{
ret = decimal.Multiply(ret, Num);
}
return ret;
}
``````