TDES で大きな文字列を暗号化し、復号化しようとしています。何が悪いのか理解できません。復号化で READ 機能を実行すると、暗号化例外から「Bad Data」が発生します。(コメントで行をマークしました)
文字列を生成して暗号化するには:
public void MssGenerateKeyPair(string ssSymmetricKey, out string ssCipheredKeyPair, out string ssPublicKey) {
ssCipheredKeyPair = string.Empty;
ssPublicKey = string.Empty;
//Symmetric key is the hash of user's signing password
SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
byte[] tempKey = sha1.ComputeHash(Encoding.UTF8.GetBytes(ssSymmetricKey));
byte[] key = new byte[24];
tempKey.CopyTo(key, 0);
//if symmetric keys is < 24
for (int index = 0, i = tempKey.Length; i < key.Length; index++, i++)
{
key[i] = tempKey[index];
}
//define symmetric encryption data
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = key;
tdes.IV = new byte[8];
tdes.Mode = CipherMode.CBC;
tdes.Padding = PaddingMode.PKCS7;
//define assymetric stuff
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream, tdes.CreateEncryptor(), CryptoStreamMode.Write);
byte[] toChipher = Encoding.UTF8.GetBytes(rsa.ToXmlString(true));
cryptoStream.Write(toChipher, 0, toChipher.Length);
byte[] cipheredKeyPair = memoryStream.ToArray();
ssCipheredKeyPair = Convert.ToBase64String(cipheredKeyPair);
ssPublicKey = rsa.ToXmlString(false);
cryptoStream.Close();
memoryStream.Close();
} // MssGenerateKeyPair
文字列を復号化するには
public void MssSignData(string ssSymmetricKey, byte[] ssDataToSign, byte[] ssCipheredKeyPair, out byte[] ssSignature, out byte[] ssSignedData) {
ssSignature = new byte[] {};
ssSignedData = new byte[] {};
//Symmetric key is the hash of user's signing password
SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
byte[] tempKey = sha1.ComputeHash(Encoding.UTF8.GetBytes(ssSymmetricKey));
byte[] key = new byte[24];
tempKey.CopyTo(key, 0);
//if symmetric keys is < 24
for (int index = 0, i = tempKey.Length; i < key.Length; index++, i++)
{
key[i] = tempKey[index];
}
//define symmetric encryption data
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = key;
tdes.IV = new byte[8];
tdes.Mode = CipherMode.CBC;
tdes.Padding = PaddingMode.PKCS7;
MemoryStream memoryStream = new MemoryStream(ssCipheredKeyPair);
CryptoStream cryptoStream = new CryptoStream(memoryStream, tdes.CreateDecryptor(), CryptoStreamMode.Read);
MemoryStream plainMemoryStream = new MemoryStream();
byte[] tempPlainBytes = new byte[1024];
int read = 0;
int totalRead = 0;
do
{
//################################################################
//ERROR IN THE FOLLOWING LINE
//################################################################
read = cryptoStream.Read(tempPlainBytes, 0, tempPlainBytes.Length);
totalRead += read;
plainMemoryStream.Write(tempPlainBytes, 0, read);
} while (read > 0);
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(Encoding.UTF8.GetString(plainMemoryStream.ToArray()));
ssSignature = rsa.SignData(ssDataToSign, new SHA1CryptoServiceProvider());
ssSignedData = ssDataToSign;
cryptoStream.Close();
memoryStream.Close();
plainMemoryStream.Close();
} // MssSignData
テストするコード
private void button1_Click(object sender, EventArgs e)
{
string key;
string pkey;
byte[] sig;
byte[] avs;
OutSystems.NssPseudoCertificates.CssPseudoCertificates c = new OutSystems.NssPseudoCertificates.CssPseudoCertificates();
c.MssGenerateKeyPair("xpto",out key, out pkey);
c.MssSignString("xpto", "hello", key, out sig, out avs);
}