I am trying to write some symmetric cryptography for holding an FtpCredential in the database, unfortunately I am having a few problems, one of which is getting back gobbley gook from a crypto decoder. I have trimmed it down to just the bare minimum code.
Please look at the SetPassword method and the GetPassword method.
/*Author: Cameron Block*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.IO;
using System.Net;
using System.Diagnostics;
using System.Configuration;
namespace Reporting.Models {
/// <summary>
/// FtpCredential stores passwords in the database, and uploads files to remote FTP sites.
/// </summary>
public class FtpCredential {
private const int CHARS_AVAILABLE = 32;
public FtpCredential() {
PasswordEncrypted = new byte[GetSymmetricAlgorithim().BlockSize * GetEncodingNumBytes() * CHARS_AVAILABLE];
}
/// <summary>
/// The encrypted password of the ftp site.
/// </summary>
public byte[] PasswordEncrypted {
get; set;
}
/// <summary>
/// Sets the password for this FtpCredential.
/// </summary>
/// <param name="password"></param>
public void SetPassword(String password) {
using (SymmetricAlgorithm crypt = Rijndael.Create()) {
//crypt.Padding = PaddingMode.Zeros;
int keySize = crypt.LegalKeySizes[0].MinSize / 8;
int ivSize = crypt.BlockSize / 8;
byte[] key = Encoding.UTF8.GetBytes(System.Configuration.ConfigurationManager.AppSettings["dEncryptionKey"].PadRight(keySize, '\0').ToCharArray(), 0, keySize);
byte[] iv = Encoding.UTF8.GetBytes(System.Configuration.ConfigurationManager.AppSettings["EncryptionIV"].PadRight(ivSize, '\0').ToCharArray(), 0, ivSize);
ICryptoTransform transform = crypt.CreateEncryptor(key, iv);
MemoryStream ms = new MemoryStream();
CryptoStream csWriter = new CryptoStream(ms, crypt.CreateEncryptor(), CryptoStreamMode.Write);
StreamWriter writer = new StreamWriter(csWriter);
writer.Write(password);
writer.Flush();
csWriter.FlushFinalBlock();
PasswordEncrypted = ms.ToArray();
writer.Close();
}
}//end method
/// <summary>
/// Un-encrypts the password for this FtpCredential.
/// </summary>
/// <returns></returns>
public String GetPassword() {
using (SymmetricAlgorithm crypt = GetSymmetricAlgorithim()) {
//crypt.Padding = PaddingMode.Zeros;
int keySize = crypt.LegalKeySizes[0].MinSize / 8;
int ivSize = crypt.BlockSize / 8;
byte[] key = Encoding.UTF8.GetBytes(System.Configuration.ConfigurationManager.AppSettings["EncryptionKey"].PadRight(keySize, '\0').ToCharArray(), 0, keySize);
byte[] iv = Encoding.UTF8.GetBytes(System.Configuration.ConfigurationManager.AppSettings["EncryptionIV"].PadRight(ivSize, '\0').ToCharArray(), 0, ivSize);
ICryptoTransform transform = crypt.CreateDecryptor(key, iv);
MemoryStream ms = new MemoryStream(PasswordEncrypted);
CryptoStream csReader = new CryptoStream(ms, crypt.CreateDecryptor(), CryptoStreamMode.Read);
StreamReader reader = new StreamReader(csReader);
return reader.ReadToEnd();
}
}//end method
/// <summary>
/// Gets the underlying symmetric algorithim for the object's password capability.
/// </summary>
/// <returns></returns>
public SymmetricAlgorithm GetSymmetricAlgorithim() {
return Rijndael.Create();
}
/// <summary>
/// Gets the number of bytes for the encoding used by the credential object.
/// </summary>
/// <returns></returns>
public int GetEncodingNumBytes() {
return Encoding.UTF8.GetByteCount("1");
}
}//end class
}//end namespace