0
Answer

Triple DES and passing in the secret key

Joseph Shanahan

Joseph Shanahan

17y
2.4k
1
I've got the code below which worked well for DES while testing. Then im impreded a Cryptographic provider hardware which stores a secret key which I use to encrypt the data.
But to get the secret key from the card and pass it in exposes it.

Any ides on how to approach this.


CspParameters cp = new CspParameters(1, "Full Cryptographic Provider");

cp.KeyContainerName = "MyContainer";

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cp);

//byte[] SecretKey = get this from eracom card container

MemoryStream ms = null;

CryptoStream encStream = null;

TripleDESCryptoServiceProvider enc = null;

try

{

enc = new TripleDESCryptoServiceProvider();

enc.Mode = CipherMode.ECB;

enc.KeySize = 128;

enc.Padding = PaddingMode.Zeros;

byte[] IV = enc.GenerateIV; //This is not required but we'll pass something in anyhow

 

ms = new MemoryStream(CypherText);

encStream = new CryptoStream(ms,

enc.CreateDecryptor(SecretKey, IV),

CryptoStreamMode.Read);

byte[] fromEncrypt = new byte[CypherText.Length];

encStream.Read(fromEncrypt, 0, fromEncrypt.Length);

return fromEncrypt;

}


Cheers


Joe