public byte[] Decrypt3DES(string a_strString, string a_strKey) //解密
{
TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
DES.Key = Convert.FromBase64String(a_strKey);
DES.Mode = CipherMode.ECB;
DES.Padding = PaddingMode.PKCS7; ICryptoTransform DESDecrypt = DES.CreateDecryptor();
byte[] result;
try
{
byte[] Buffer = Encoding.UTF8.GetBytes(a_strString);
result = DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length);
}
catch (Exception e)
{
throw (new Exception("不是有效的 base64 字符串:{0}", e));
}
return result;
}
运行后报Length of the data to decrypt is invalid.的错误...求助啊...这个大概是因为啥引起的?有嘛解决办法啊?
JAVA的解密方式是:public static String desDecrypt(String cipherText, String keyValue) throws Exception { BASE64Decoder base64d = new BASE64Decoder();
DESedeKeySpec p8ksp = null;
p8ksp = new DESedeKeySpec(base64d.decodeBuffer(keyValue));
Key key = SecretKeyFactory.getInstance("DESede").generateSecret(p8ksp); Cipher cipher = null;
byte[] inPut = base64d.decodeBuffer(cipherText);
// “算法/模式/填充”
cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
SecretKeySpec myKey = new SecretKeySpec(key.getEncoded(), "DESede");
// IvParameterSpec ivspec = new IvParameterSpec(myIV);
cipher.init(2, myKey);
byte[] output = cipher.doFinal(inPut);
return new String(output, "UTF8");
}
{
TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
DES.Key = Convert.FromBase64String(a_strKey);
DES.Mode = CipherMode.ECB;
DES.Padding = PaddingMode.PKCS7; ICryptoTransform DESDecrypt = DES.CreateDecryptor();
byte[] result;
try
{
byte[] Buffer = Encoding.UTF8.GetBytes(a_strString);
result = DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length);
}
catch (Exception e)
{
throw (new Exception("不是有效的 base64 字符串:{0}", e));
}
return result;
}
运行后报Length of the data to decrypt is invalid.的错误...求助啊...这个大概是因为啥引起的?有嘛解决办法啊?
JAVA的解密方式是:public static String desDecrypt(String cipherText, String keyValue) throws Exception { BASE64Decoder base64d = new BASE64Decoder();
DESedeKeySpec p8ksp = null;
p8ksp = new DESedeKeySpec(base64d.decodeBuffer(keyValue));
Key key = SecretKeyFactory.getInstance("DESede").generateSecret(p8ksp); Cipher cipher = null;
byte[] inPut = base64d.decodeBuffer(cipherText);
// “算法/模式/填充”
cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
SecretKeySpec myKey = new SecretKeySpec(key.getEncoded(), "DESede");
// IvParameterSpec ivspec = new IvParameterSpec(myIV);
cipher.init(2, myKey);
byte[] output = cipher.doFinal(inPut);
return new String(output, "UTF8");
}
解决方案 »
- 用dataGridView控件获取数据库表信息,如何设置表列宽度?
- C#如何给HtmlTableRow添加双击事件
- [存储过程调用] sql2005带有Output参数的存储过程在C#中调用的问题
- 如何禁止键盘输入?
- 加密解密的时候的错误。Base-64 字符数组的无效长度。如果我加密的字符串很长的话,解密的时候,会出现这个错误,怎么解决?
- C#新手求指导
- C#中如何使用Socket传送视频文件啊??
- 求教关于统计问题
- 如何实现在.cs界面控制GridView的数据显示
- 怎样在C#中实现两个sql表之间的INSERT INTO ?
- window服务程序周数的问题
- C#中如何用JS写一个回车触发button的click事件 在线等
The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or a non-white space character among the padding characters.
我肯定是试过这样做的...如果用Convert.FromBase64String的话,这句就抛异常了,如果用Encoding.UTF8.GetBytes还可以执行到result = DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length);这说明转换出来的byte数组是正确的 而Convert出来的是错误的.
byte[] inPut = base64d.decodeBuffer(cipherText);看到JAVA怎么做了么?"The input is not a valid Base-64 string..."说明你的数据有问题。
/// DES 加密(数据加密标准,速度较快,适用于加密大量数据的场合)
/// </summary>
/// <param name="EncryptString">待加密的密文</param>
/// <param name="EncryptKey">加密的密钥</param>
/// <returns>returns</returns>
public static string DESEncrypt(string EncryptString, string EncryptKey)
{
if (string.IsNullOrEmpty(EncryptString)) { throw (new Exception("密文不得为空")); } if (string.IsNullOrEmpty(EncryptKey)) { throw (new Exception("密钥不得为空")); } if (EncryptKey.Length != 8) { throw (new Exception("密钥必须为8位")); } byte[] m_btIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; string m_strEncrypt = ""; DESCryptoServiceProvider m_DESProvider = new DESCryptoServiceProvider(); try
{
byte[] m_btEncryptString = Encoding.Default.GetBytes(EncryptString); MemoryStream m_stream = new MemoryStream(); CryptoStream m_cstream = new CryptoStream(m_stream, m_DESProvider.CreateEncryptor(Encoding.Default.GetBytes(EncryptKey), m_btIV), CryptoStreamMode.Write); m_cstream.Write(m_btEncryptString, 0, m_btEncryptString.Length); m_cstream.FlushFinalBlock(); m_strEncrypt = Convert.ToBase64String(m_stream.ToArray()); m_stream.Close(); m_stream.Dispose(); m_cstream.Close(); m_cstream.Dispose();
}
catch (IOException ex) { throw ex; }
catch (CryptographicException ex) { throw ex; }
catch (ArgumentException ex) { throw ex; }
catch (Exception ex) { throw ex; }
finally { m_DESProvider.Clear(); } return m_strEncrypt;
} /// <summary>
/// DES 解密(数据加密标准,速度较快,适用于加密大量数据的场合)
/// </summary>
/// <param name="DecryptString">待解密的密文</param>
/// <param name="DecryptKey">解密的密钥</param>
/// <returns>returns</returns>
public static string DESDecrypt(string DecryptString, string DecryptKey)
{
if (string.IsNullOrEmpty(DecryptString)) { throw (new Exception("密文不得为空")); } if (string.IsNullOrEmpty(DecryptKey)) { throw (new Exception("密钥不得为空")); } if (DecryptKey.Length != 8) { throw (new Exception("密钥必须为8位")); } byte[] m_btIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; string m_strDecrypt = ""; DESCryptoServiceProvider m_DESProvider = new DESCryptoServiceProvider(); try
{
byte[] m_btDecryptString = Convert.FromBase64String(DecryptString); MemoryStream m_stream = new MemoryStream();
CryptoStream m_cstream = new CryptoStream(m_stream, m_DESProvider.CreateDecryptor(Encoding.Default.GetBytes(DecryptKey), m_btIV), CryptoStreamMode.Write); m_cstream.Write(m_btDecryptString, 0, m_btDecryptString.Length); m_cstream.FlushFinalBlock(); m_strDecrypt = Encoding.Default.GetString(m_stream.ToArray()); m_stream.Close(); m_stream.Dispose(); m_cstream.Close(); m_cstream.Dispose();
}
catch (IOException ex) { throw ex; }
catch (CryptographicException ex) { throw ex; }
catch (ArgumentException ex) { throw ex; }
catch (Exception ex) { throw ex; }
finally { m_DESProvider.Clear(); } return m_strDecrypt;
}