今天研究了加密与解密问题:
查了很久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
using System;2
using System.Security.Cryptography;3
using System.Text;4

5
class RSACSPSample6
{7

8
static void Main()9
{10
try11
{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 generate27
//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 did52
//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
try62
{ 63
//Create a new instance of RSACryptoServiceProvider.64
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();65

66
//Import the RSA Key information. This only needs67
//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 or72
//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
try89
{90
//Create a new instance of RSACryptoServiceProvider.91
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();92

93
//Import the RSA Key information. This needs94
//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 or99
//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



浙公网安备 33010602011771号