what's wrong in this RSA why the results of encryption/decryption are different?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















enter code herethis is p,q,n,phi,e,d and there is no any error in code and all the arthimitic operations are correct and I am sure, but what probably I mistake here?



  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click  'encrypt
ee = Convert.ToDouble(txtE.Text)
n = Convert.ToDouble(txtN.Text)
m = Convert.ToDouble(txtMessage.Text)

txtCipher.Text = powermod(m, ee, n)
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click 'decrypt
dd = Convert.ToDouble(txtD.Text)
n = Convert.ToDouble(txtN.Text)
c = Convert.ToDouble(txtCipher.Text)

txtMessageRecovery.Text = powermod(c, dd, n)


End Sub



Public Function powermod(base As Double, exponent As Double, modulus As Double) As Double
If base < 1 Or exponent < 0 Or modulus < 1 Then Return -1
Dim result As Double = 1
While (exponent > 0)
If ((exponent Mod 2) = 1) Then
result = (result * base) Mod modulus
End If
base = (base * base) Mod modulus
exponent = Floor(exponent / 2)
End While
Return result
End Function


the numbers in this image










share|improve this question




















  • 2





    The double type is not accurate enough to do this correctly, integer math is required. Use the System.Security.Cryptography namespace.

    – Hans Passant
    Nov 23 '18 at 15:05











  • thanks hans, but what's mean by integer math?, do you mean Integer data type? @HansPassant

    – Burier Elia
    Nov 23 '18 at 16:08






  • 2





    Consider googling "floating point math vs integer math" to find stuff to help you understand the difference.

    – Hans Passant
    Nov 23 '18 at 16:10











  • @HansPassant I use double because it doesn't have maximum range

    – Burier Elia
    Nov 23 '18 at 16:14






  • 2





    It trades range for precision. Double has only 53 bits to represent the value, the simplest RSA encryption key needs 330 bits. System.Security.Cryptograph has the code that can manipulate that many bits.

    – Hans Passant
    Nov 23 '18 at 16:23


















0















enter code herethis is p,q,n,phi,e,d and there is no any error in code and all the arthimitic operations are correct and I am sure, but what probably I mistake here?



  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click  'encrypt
ee = Convert.ToDouble(txtE.Text)
n = Convert.ToDouble(txtN.Text)
m = Convert.ToDouble(txtMessage.Text)

txtCipher.Text = powermod(m, ee, n)
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click 'decrypt
dd = Convert.ToDouble(txtD.Text)
n = Convert.ToDouble(txtN.Text)
c = Convert.ToDouble(txtCipher.Text)

txtMessageRecovery.Text = powermod(c, dd, n)


End Sub



Public Function powermod(base As Double, exponent As Double, modulus As Double) As Double
If base < 1 Or exponent < 0 Or modulus < 1 Then Return -1
Dim result As Double = 1
While (exponent > 0)
If ((exponent Mod 2) = 1) Then
result = (result * base) Mod modulus
End If
base = (base * base) Mod modulus
exponent = Floor(exponent / 2)
End While
Return result
End Function


the numbers in this image










share|improve this question




















  • 2





    The double type is not accurate enough to do this correctly, integer math is required. Use the System.Security.Cryptography namespace.

    – Hans Passant
    Nov 23 '18 at 15:05











  • thanks hans, but what's mean by integer math?, do you mean Integer data type? @HansPassant

    – Burier Elia
    Nov 23 '18 at 16:08






  • 2





    Consider googling "floating point math vs integer math" to find stuff to help you understand the difference.

    – Hans Passant
    Nov 23 '18 at 16:10











  • @HansPassant I use double because it doesn't have maximum range

    – Burier Elia
    Nov 23 '18 at 16:14






  • 2





    It trades range for precision. Double has only 53 bits to represent the value, the simplest RSA encryption key needs 330 bits. System.Security.Cryptograph has the code that can manipulate that many bits.

    – Hans Passant
    Nov 23 '18 at 16:23














0












0








0








enter code herethis is p,q,n,phi,e,d and there is no any error in code and all the arthimitic operations are correct and I am sure, but what probably I mistake here?



  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click  'encrypt
ee = Convert.ToDouble(txtE.Text)
n = Convert.ToDouble(txtN.Text)
m = Convert.ToDouble(txtMessage.Text)

txtCipher.Text = powermod(m, ee, n)
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click 'decrypt
dd = Convert.ToDouble(txtD.Text)
n = Convert.ToDouble(txtN.Text)
c = Convert.ToDouble(txtCipher.Text)

txtMessageRecovery.Text = powermod(c, dd, n)


End Sub



Public Function powermod(base As Double, exponent As Double, modulus As Double) As Double
If base < 1 Or exponent < 0 Or modulus < 1 Then Return -1
Dim result As Double = 1
While (exponent > 0)
If ((exponent Mod 2) = 1) Then
result = (result * base) Mod modulus
End If
base = (base * base) Mod modulus
exponent = Floor(exponent / 2)
End While
Return result
End Function


the numbers in this image










share|improve this question
















enter code herethis is p,q,n,phi,e,d and there is no any error in code and all the arthimitic operations are correct and I am sure, but what probably I mistake here?



  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click  'encrypt
ee = Convert.ToDouble(txtE.Text)
n = Convert.ToDouble(txtN.Text)
m = Convert.ToDouble(txtMessage.Text)

txtCipher.Text = powermod(m, ee, n)
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click 'decrypt
dd = Convert.ToDouble(txtD.Text)
n = Convert.ToDouble(txtN.Text)
c = Convert.ToDouble(txtCipher.Text)

txtMessageRecovery.Text = powermod(c, dd, n)


End Sub



Public Function powermod(base As Double, exponent As Double, modulus As Double) As Double
If base < 1 Or exponent < 0 Or modulus < 1 Then Return -1
Dim result As Double = 1
While (exponent > 0)
If ((exponent Mod 2) = 1) Then
result = (result * base) Mod modulus
End If
base = (base * base) Mod modulus
exponent = Floor(exponent / 2)
End While
Return result
End Function


the numbers in this image







vb.net






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 14:39







Burier Elia

















asked Nov 23 '18 at 14:21









Burier EliaBurier Elia

61




61








  • 2





    The double type is not accurate enough to do this correctly, integer math is required. Use the System.Security.Cryptography namespace.

    – Hans Passant
    Nov 23 '18 at 15:05











  • thanks hans, but what's mean by integer math?, do you mean Integer data type? @HansPassant

    – Burier Elia
    Nov 23 '18 at 16:08






  • 2





    Consider googling "floating point math vs integer math" to find stuff to help you understand the difference.

    – Hans Passant
    Nov 23 '18 at 16:10











  • @HansPassant I use double because it doesn't have maximum range

    – Burier Elia
    Nov 23 '18 at 16:14






  • 2





    It trades range for precision. Double has only 53 bits to represent the value, the simplest RSA encryption key needs 330 bits. System.Security.Cryptograph has the code that can manipulate that many bits.

    – Hans Passant
    Nov 23 '18 at 16:23














  • 2





    The double type is not accurate enough to do this correctly, integer math is required. Use the System.Security.Cryptography namespace.

    – Hans Passant
    Nov 23 '18 at 15:05











  • thanks hans, but what's mean by integer math?, do you mean Integer data type? @HansPassant

    – Burier Elia
    Nov 23 '18 at 16:08






  • 2





    Consider googling "floating point math vs integer math" to find stuff to help you understand the difference.

    – Hans Passant
    Nov 23 '18 at 16:10











  • @HansPassant I use double because it doesn't have maximum range

    – Burier Elia
    Nov 23 '18 at 16:14






  • 2





    It trades range for precision. Double has only 53 bits to represent the value, the simplest RSA encryption key needs 330 bits. System.Security.Cryptograph has the code that can manipulate that many bits.

    – Hans Passant
    Nov 23 '18 at 16:23








2




2





The double type is not accurate enough to do this correctly, integer math is required. Use the System.Security.Cryptography namespace.

– Hans Passant
Nov 23 '18 at 15:05





The double type is not accurate enough to do this correctly, integer math is required. Use the System.Security.Cryptography namespace.

– Hans Passant
Nov 23 '18 at 15:05













thanks hans, but what's mean by integer math?, do you mean Integer data type? @HansPassant

– Burier Elia
Nov 23 '18 at 16:08





thanks hans, but what's mean by integer math?, do you mean Integer data type? @HansPassant

– Burier Elia
Nov 23 '18 at 16:08




2




2





Consider googling "floating point math vs integer math" to find stuff to help you understand the difference.

– Hans Passant
Nov 23 '18 at 16:10





Consider googling "floating point math vs integer math" to find stuff to help you understand the difference.

– Hans Passant
Nov 23 '18 at 16:10













@HansPassant I use double because it doesn't have maximum range

– Burier Elia
Nov 23 '18 at 16:14





@HansPassant I use double because it doesn't have maximum range

– Burier Elia
Nov 23 '18 at 16:14




2




2





It trades range for precision. Double has only 53 bits to represent the value, the simplest RSA encryption key needs 330 bits. System.Security.Cryptograph has the code that can manipulate that many bits.

– Hans Passant
Nov 23 '18 at 16:23





It trades range for precision. Double has only 53 bits to represent the value, the simplest RSA encryption key needs 330 bits. System.Security.Cryptograph has the code that can manipulate that many bits.

– Hans Passant
Nov 23 '18 at 16:23












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448408%2fwhats-wrong-in-this-rsa-why-the-results-of-encryption-decryption-are-different%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448408%2fwhats-wrong-in-this-rsa-why-the-results-of-encryption-decryption-are-different%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Academy of Television Arts & Sciences

L'Équipe

1995 France bombings