今天研究了加密与解密问题:
查了很久MSDN,调式了很久都没能解决下面一下问题,请大家来帮忙一下:
当不注释第31 \32 行代码是会发生异常:提示信息:"先项错误"
哪位能帮忙找一下原因:谢谢了
1
using System;
2
using System.Security.Cryptography;
3
using System.Text;
4
5
class RSACSPSample
6
{
7
8
static void Main()
9
{
10
try
11
{
12
//Create a UnicodeEncoder to convert between byte array and string.
13
UnicodeEncoding ByteConverter = new UnicodeEncoding();
14
15
//Create byte arrays to hold original, encrypted, and decrypted data.
16
17
byte[] dataToEncrypt = ByteConverter.GetBytes("Data to Encrypt");
18
byte[] encryptedData;
19
byte[] decryptedData;
20
byte[] PublicKey = {214,46,220,83,160,73,40,39,201,155,19,202,3,11,191,178,56,
21
147};
22
23
byte[] Exponent = {1,0,1};
24
25
26
//Create a new instance of RSACryptoServiceProvider to generate
27
//public and private key data.
28
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
29
RSAParameters RSAKeyInfo = RSA.ExportParameters(true);;
30
//为什么加了下面那断就出现错误
31
// RSAKeyInfo.Modulus = PublicKey;
32
// RSAKeyInfo.Exponent = Exponent;
33
34
//Pass the data to ENCRYPT, the public key information
35
//(using RSACryptoServiceProvider.ExportParameters(false),
36
//and a boolean flag specifying no OAEP padding.
37
//加密
38
// encryptedData = RSAEncrypt(dataToEncrypt,RSA.ExportParameters(true), false);
39
encryptedData = RSAEncrypt(dataToEncrypt,RSAKeyInfo, false);
40
//Pass the data to DECRYPT, the private key information
41
//(using RSACryptoServiceProvider.ExportParameters(true),
42
//and a boolean flag specifying no OAEP padding.
43
//解密
44
decryptedData = RSADecrypt(encryptedData,RSAKeyInfo, false);
45
46
//Display the decrypted plaintext to the console.
47
Console.WriteLine("Decrypted plaintext: {0}", ByteConverter.GetString(decryptedData));
48
}
49
catch(ArgumentNullException)
50
{
51
//Catch this exception in case the encryption did
52
//not succeed.
53
Console.WriteLine("Encryption failed.");
54
55
}
56
Console.Read();
57
}
58
//加密
59
static public byte[] RSAEncrypt(byte[] DataToEncrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding)
60
{
61
try
62
{
63
//Create a new instance of RSACryptoServiceProvider.
64
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
65
66
//Import the RSA Key information. This only needs
67
//toinclude the public key information.
68
RSA.ImportParameters(RSAKeyInfo);
69
70
//Encrypt the passed byte array and specify OAEP padding.
71
//OAEP padding is only available on Microsoft Windows XP or
72
//later.
73
return RSA.Encrypt(DataToEncrypt, DoOAEPPadding);
74
}
75
//Catch and display a CryptographicException
76
//to the console.
77
catch(CryptographicException e)
78
{
79
Console.WriteLine(e.Message);
80
81
return null;
82
}
83
84
}
85
//解密:
86
static public byte[] RSADecrypt(byte[] DataToDecrypt, RSAParameters RSAKeyInfo,bool DoOAEPPadding)
87
{
88
try
89
{
90
//Create a new instance of RSACryptoServiceProvider.
91
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
92
93
//Import the RSA Key information. This needs
94
//to include the private key information.
95
RSA.ImportParameters(RSAKeyInfo);
96
97
//Decrypt the passed byte array and specify OAEP padding.
98
//OAEP padding is only available on Microsoft Windows XP or
99
//later.
100
return RSA.Decrypt(DataToDecrypt, DoOAEPPadding);
101
}
102
//Catch and display a CryptographicException
103
//to the console.
104
catch(CryptographicException e)
105
{
106
Console.WriteLine(e.ToString());
107
108
return null;
109
}
110
111
}
112
}
113

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113
