-
-
[求助]现有解密算法(源码)自己加密的时候,总是提示长度不正确
-
发表于:
2011-11-24 09:16
5364
-
[求助]现有解密算法(源码)自己加密的时候,总是提示长度不正确
//需要using
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualBasic;
using Microsoft.VisualBasic.CompilerServices;
using System.Security.Cryptography;
using System.IO;
using System.Data;
using System.Security;
using System.Diagnostics;
using System.Collections;
private static TripleDESCryptoServiceProvider TDES=new TripleDESCryptoServiceProvider();
private static SHA256Managed sha256=new SHA256Managed();
private static string Decode(byte[] p0, byte[] p1)
{
ICryptoTransform transform = null;
string str;
byte[] buffer = Convert.FromBase64String(Encoding.UTF8.GetString(p0));
using (MemoryStream stream = new MemoryStream(buffer))
{
byte[] buffer2 = new byte[buffer.Length + 1];
transform = TDES.CreateDecryptor(p1, ConvertToByteArray(p1, TDES.IV.Length));
using (CryptoStream stream2 = new CryptoStream(stream, transform, CryptoStreamMode.Read))
{
stream2.Read(buffer2, 0, buffer2.Length);
}
str = Encoding.UTF8.GetString(buffer2).Replace("\0", "");
}
return str;
}
private static byte[] ConvertToByteArray(byte[] p0)
{
//int length = 111;
int length = TDES.Key.Length;
if (p0.Length == length)
{
return p0;
}
byte[] destinationArray = new byte[(length - 1) + 1];
Array.Copy(sha256.ComputeHash(p0), destinationArray, (int)(length - 1));
return destinationArray;
}
private static byte[] ConvertToByteArray(byte[] p0, int p1)
{
byte[] destinationArray = new byte[(p1 - 1) + 1];
Array.Copy(sha256.ComputeHash(Encoding.UTF8.GetBytes(Encoding.UTF8.GetString(p0))), destinationArray, (int)(p1 - 1));
return destinationArray;
}
//解密调用
Decode(Encoding.UTF8.GetBytes("字符串"), ConvertToByteArray(Encoding.UTF8.GetBytes("test")))
[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!