1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.Applications
.MindDump
.Services
19 using System
.Security
.Cryptography
;
22 public class EncryptionService
24 private readonly byte[] _key
;
25 private readonly byte[] _initVector
;
27 private DESCryptoServiceProvider _provider
;
29 public EncryptionService()
31 _provider
= new DESCryptoServiceProvider();
32 _key
= Encoding
.Default
.GetBytes("MyKeyCaS");
33 _initVector
= Encoding
.Default
.GetBytes("itVector");
36 public string Encrypt(string contents
)
38 ICryptoTransform transform
= _provider
.CreateEncryptor(_key
, _initVector
);
40 byte[] bArray
= Encoding
.Default
.GetBytes(contents
);
41 byte[] bOutput
= transform
.TransformFinalBlock( bArray
, 0, bArray
.Length
);
43 return Convert
.ToBase64String( bOutput
);
46 public string Decrypt(string contents
)
48 ICryptoTransform transform
= _provider
.CreateDecryptor(_key
, _initVector
);
50 // byte[] bArray = Encoding.Default.GetBytes( Convert.FromBase64String( contents) );
51 byte[] bArray
= Convert
.FromBase64String(contents
);
52 byte[] bOutput
= transform
.TransformFinalBlock( bArray
, 0, bArray
.Length
);
54 return Encoding
.Default
.GetString( bOutput
);