12,397,027 members (68,811 online)
Rate this:
See more:
Hi.. I have a question.. My teacher gave me an assignment to encrypt files using RSA.. I understand the system very well.. I converted the file to byte[].. And when I do the encryption, I get values larger than the values permitted for the type byte and the program stops.. Is there any way to avoid that?

```Sub encrypt()
For i = 0 To bytes.Length - 1
bytes(i) = fast(bytes(i), ee)
Next
End Sub```

```Function fast(ByVal aa As Object, ByVal zz As Integer) As Integer
Dim xx As Integer
xx = 1
While zz <> 0
While zz Mod 2 = 0
zz = zz \ 2
aa = (aa * aa) Mod n
End While
zz = zz - 1
xx = (xx * aa) Mod n
End While
Return xx
End Function
```

It stops after the fast function ends.. Means after it returns the resulted value...
Posted 19-Apr-13 8:07am
Updated 19-Apr-13 9:03am
v2
Nelek 19-Apr-13 14:11pm

ryanb31 19-Apr-13 14:20pm

Need more info. What are you trying to encrypt that is so big it won't fit into byte[]?
OriginalGriff 19-Apr-13 14:37pm

How are you doing the encrypt? show the relevant code fragment.
(Use the "Improve question" widget to edit your question and provide better information.)

Rate this:

## Solution 1

You can't possibly get values larger than byte if you were doing the encryption properly. Now, if you created an array of bytes to hold the resulting encrypted data, THEN you might get some kind of array overflow.

Without seeing your code, it's impossible to tell you what you did wrong.

I voted 4 (I mean, not 5) by the following reason: in an array of byte, it is impossible to values larger than byte even of encryption is done improperly or even completely failed. :-)
—SA
Dave Kreskowiak 20-Apr-13 8:20am

Yeah, look at his code. He's getting the "overflow" (not even the real error) because his array is Byte, but his "fast" function is returning Integer. He's trying to stuff an Intger into a Byte.
Sergey Alexandrovich Kryukov 20-Apr-13 20:29pm

I see, thank you for the explanation of what could be perceived as "overflow"; this is just the usual wrapping of the value of the maximum. I only say that this "overflow" has nothing to do with the validity of encryption.
—SA
Rate this:

## Solution 2

Top Experts
Last 24hrsThis month
 Karthik Bangalore 170 OriginalGriff 165 Richard MacCutchan 135 BillWoodruff 95 ppolymorphe 85
 OriginalGriff 6,138 Karthik Bangalore 2,497 ppolymorphe 2,445 F-ES Sitecore 1,877 Richard MacCutchan 1,812