/* | |
* Copyright 2001-2008 The Apache Software Foundation. | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
* | |
*/ | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Security.Cryptography; | |
using System.Text; | |
namespace org.apache.juddi.v3.client.cryptor | |
{ | |
/// <summary> | |
/// An abstract class for AES functions | |
/// </summary> | |
/// <see cref="org.apache.juddi.v3.client.cryptor.AES128Cryptor"/> | |
/// <see cref="org.apache.juddi.v3.client.cryptor.AES256Cryptor"/> | |
/// <author><a href="mailto:alexoree@apache.org">Alex O'Ree</a></author> | |
public abstract class AESCryptor : Cryptor | |
{ | |
protected internal abstract int GetKeySize(); | |
protected internal abstract byte[] GetKey(); | |
protected internal abstract byte[] GetIV(); | |
protected internal abstract int GetBlockSize(); | |
public string encrypt(string str) | |
{ | |
// Encrypt the string to an array of bytes. | |
byte[] encryptedPassword = EncryptStringToBytes(str, GetKey(), GetKey()); | |
return Convert.ToBase64String(encryptedPassword); | |
} | |
public string decrypt(string str) | |
{ | |
byte[] encryptedPassword = Convert.FromBase64String(str); | |
string clear = DecryptStringFromBytes(encryptedPassword, GetKey(), GetKey()); | |
return clear; | |
} | |
internal byte[] EncryptStringToBytes(string plainText, byte[] key, byte[] iv) | |
{ | |
// Check arguments. | |
if (plainText == null || plainText.Length <= 0) | |
{ | |
throw new ArgumentNullException("plainText"); | |
} | |
if (key == null || key.Length <= 0) | |
{ | |
throw new ArgumentNullException("key"); | |
} | |
if (iv == null || iv.Length <= 0) | |
{ | |
throw new ArgumentNullException("key"); | |
} | |
byte[] encrypted; | |
// Create an RijndaelManaged object | |
// with the specified key and IV. | |
using (var rijAlg = new RijndaelManaged()) | |
{ | |
rijAlg.BlockSize = this.GetBlockSize(); | |
rijAlg.KeySize = this.GetKeySize(); | |
rijAlg.Key = key; | |
rijAlg.IV = iv; | |
// Create a decrytor to perform the stream transform. | |
ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV); | |
// Create the streams used for encryption. | |
using (var msEncrypt = new MemoryStream()) | |
{ | |
using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) | |
{ | |
using (var swEncrypt = new StreamWriter(csEncrypt)) | |
{ | |
//Write all data to the stream. | |
swEncrypt.Write(plainText); | |
} | |
encrypted = msEncrypt.ToArray(); | |
} | |
} | |
} | |
// Return the encrypted bytes from the memory stream. | |
return encrypted; | |
} | |
internal string DecryptStringFromBytes(byte[] cipherText, byte[] key, byte[] iv) | |
{ | |
// Check arguments. | |
if (cipherText == null || cipherText.Length <= 0) | |
throw new ArgumentNullException("cipherText"); | |
if (key == null || key.Length <= 0) | |
throw new ArgumentNullException("key"); | |
if (iv == null || iv.Length <= 0) | |
throw new ArgumentNullException("key"); | |
// Declare the string used to hold | |
// the decrypted text. | |
string plaintext; | |
// Create an RijndaelManaged object | |
// with the specified key and IV. | |
using (var rijAlg = new RijndaelManaged()) | |
{ | |
rijAlg.BlockSize = this.GetBlockSize(); | |
rijAlg.KeySize = this.GetKeySize(); | |
rijAlg.Key = key; | |
rijAlg.IV = iv; | |
// Create a decrytor to perform the stream transform. | |
ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV); | |
// Create the streams used for decryption. | |
using (var msDecrypt = new MemoryStream(cipherText)) | |
{ | |
using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) | |
{ | |
using (var srDecrypt = new StreamReader(csDecrypt)) | |
{ | |
// Read the decrypted bytes from the decrypting stream | |
// and place them in a string. | |
plaintext = srDecrypt.ReadToEnd(); | |
} | |
} | |
} | |
} | |
return plaintext; | |
} | |
} | |
} |