请问,C#中的加密函数(如下),怎么在QT中解密出来?
/// 加密文件函数
/// </summary>
/// <param name="myInFileName">需要加密的文件地址</param>
/// <param name="myOutFileName">加密后的文件地址与名称</param>
/// <param name="myKeyString">密钥:手动输入8~16位</param>
public static void EncryptedFiles(string myInFileName, string myOutFileName, string myKeyString)
{
try
{
//设定初始变量
byte[] myDESIV ={ 0x36, 0x61, 0x78, 0x12, 0x3F, 0x86, 0x8F, 0x66, 0x34, 0x111, 0x53, 0x56, 0x23, 0x56, 0x44, 0xAF };
byte[] myDESKey ={ };
switch (myKeyString.Length)
{
case 8:
myDESKey = new byte[] { (byte)myKeyString[0], (byte)myKeyString[1], (byte)myKeyString[2], (byte)myKeyString[3], (byte)myKeyString[4], (byte)myKeyString[5], (byte)myKeyString[6], (byte)myKeyString[7] };
break;
case 9:
myDESKey = new byte[] { (byte)myKeyString[0], (byte)myKeyString[1], (byte)myKeyString[2], (byte)myKeyString[3], (byte)myKeyString[4], (byte)myKeyString[5], (byte)myKeyString[6], (byte)myKeyString[7], (byte)myKeyString[8] };
break;
case 10:
myDESKey = new byte[] { (byte)myKeyString[0], (byte)myKeyString[1], (byte)myKeyString[2], (byte)myKeyString[3], (byte)myKeyString[4], (byte)myKeyString[5], (byte)myKeyString[6], (byte)myKeyString[7], (byte)myKeyString[8], (byte)myKeyString[9] };
break;
case 11:
myDESKey = new byte[] { (byte)myKeyString[0], (byte)myKeyString[1], (byte)myKeyString[2], (byte)myKeyString[3], (byte)myKeyString[4], (byte)myKeyString[5], (byte)myKeyString[6], (byte)myKeyString[7], (byte)myKeyString[8], (byte)myKeyString[9], (byte)myKeyString[10] };
break;
case 12:
myDESKey = new byte[] { (byte)myKeyString[0], (byte)myKeyString[1], (byte)myKeyString[2], (byte)myKeyString[3], (byte)myKeyString[4], (byte)myKeyString[5], (byte)myKeyString[6], (byte)myKeyString[7], (byte)myKeyString[8], (byte)myKeyString[9], (byte)myKeyString[10], (byte)myKeyString[11] };
break;
case 13:
myDESKey = new byte[] { (byte)myKeyString[0], (byte)myKeyString[1], (byte)myKeyString[2], (byte)myKeyString[3], (byte)myKeyString[4], (byte)myKeyString[5], (byte)myKeyString[6], (byte)myKeyString[7], (byte)myKeyString[8], (byte)myKeyString[9], (byte)myKeyString[10], (byte)myKeyString[11], (byte)myKeyString[12] };
break;
case 14:
myDESKey = new byte[] { (byte)myKeyString[0], (byte)myKeyString[1], (byte)myKeyString[2], (byte)myKeyString[3], (byte)myKeyString[4], (byte)myKeyString[5], (byte)myKeyString[6], (byte)myKeyString[7], (byte)myKeyString[8], (byte)myKeyString[9], (byte)myKeyString[10], (byte)myKeyString[11], (byte)myKeyString[12], (byte)myKeyString[13] };
break;
case 15:
myDESKey = new byte[] { (byte)myKeyString[0], (byte)myKeyString[1], (byte)myKeyString[2], (byte)myKeyString[3], (byte)myKeyString[4], (byte)myKeyString[5], (byte)myKeyString[6], (byte)myKeyString[7], (byte)myKeyString[8], (byte)myKeyString[9], (byte)myKeyString[10], (byte)myKeyString[11], (byte)myKeyString[12], (byte)myKeyString[13], (byte)myKeyString[14] };
break;
case 16:
myDESKey = new byte[] { (byte)myKeyString[0], (byte)myKeyString[1], (byte)myKeyString[2], (byte)myKeyString[3], (byte)myKeyString[4], (byte)myKeyString[5], (byte)myKeyString[6], (byte)myKeyString[7], (byte)myKeyString[8], (byte)myKeyString[9], (byte)myKeyString[10], (byte)myKeyString[11], (byte)myKeyString[12], (byte)myKeyString[13], (byte)myKeyString[14], (byte)myKeyString[15] };
break;
default:
if (myKeyString.Length > 8 && myKeyString.Length < 16)
{
myDESKey = new byte[] { (byte)myKeyString[0], (byte)myKeyString[1], (byte)myKeyString[2], (byte)myKeyString[3], (byte)myKeyString[4], (byte)myKeyString[5], (byte)myKeyString[6], (byte)myKeyString[7] };
}
else
{
myDESKey = new byte[] { (byte)myKeyString[0], (byte)myKeyString[1], (byte)myKeyString[2], (byte)myKeyString[3], (byte)myKeyString[4], (byte)myKeyString[5], (byte)myKeyString[6], (byte)myKeyString[7], (byte)myKeyString[8], (byte)myKeyString[9], (byte)myKeyString[10], (byte)myKeyString[11], (byte)myKeyString[12], (byte)myKeyString[13], (byte)myKeyString[14], (byte)myKeyString[15] };
}
break;
}
//创建输入和输出文件流
FileStream myInFileStream = new FileStream(myInFileName, FileMode.Open, FileAccess.Read);
FileStream myOutFileStream = new FileStream(myOutFileName, FileMode.OpenOrCreate, FileAccess.Write);
myOutFileStream.SetLength(0);
//每次的中间流
byte[] inSertData = new byte[100];
//代表已经加密流的大小
int completedLength = 0;
//代表要加密文件总的大小
long inFileSize = myInFileStream.Length;
//创建RijndaelManaged加密对象
RijndaelManaged myRijndael = new RijndaelManaged();
//创建加密流
CryptoStream enCrytoStream = new CryptoStream(myOutFileStream, myRijndael.CreateEncryptor(myDESKey, myDESIV), CryptoStreamMode.Write);
//从输入文件中读取流,然后加密到输出文件流
while (completedLength < inFileSize)
{
//每次写入加密文件的数据大小
int length = myInFileStream.Read(inSertData, 0, 100);
enCrytoStream.Write(inSertData, 0, length);
completedLength += length;
}
//关闭流
try
{
enCrytoStream.Close();
myInFileStream.Close();
myOutFileStream.Close();
}
catch (Exception)
{
myInFileStream.Close();
myOutFileStream.Close();
}
FileInfo fi = new FileInfo(myInFileName);
fi.Delete();//删除临时文件
}
catch (Exception ex)
{
WriteLogFile("加密文件:" + ex.ToString());
}
}
3 个解决方案
算法应该是抽象的,与具体实现语言应该没有关系吧!你去看下aes的解密实现下应该可以把!