-
-
[求助]现有解密算法(源码)自己加密的时候,总是提示长度不正确
-
发表于:
2011-11-24 09:16
5355
-
[求助]现有解密算法(源码)自己加密的时候,总是提示长度不正确
//需要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")))
[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课