1
A
回答
2
最後我找到了這個問題的解決方案。 我將添加幫助需要的代碼。
加密方法:
Public Function EncryptStream(ByVal input As Byte()) As Byte()
Dim rijn As New RijndaelManaged()
Dim encrypted As Byte()
Dim key As Byte() = New Byte() {&H22, &HC0, &H6D, &HCB, &H23, &HA6, _
&H3, &H1B, &H5A, &H1D, &HD3, &H9F, _
&H85, &HD, &HC1, &H72, &HED, &HF4, _
&H54, &HE6, &HBA, &H65, &HC, &H22, _
&H62, &HBE, &HF3, &HEC, &H14, &H81, _
&HA8, &HA}
'32
Dim IV As Byte() = New Byte() {&H43, &HB1, &H93, &HB, &H1A, &H87, _
&H52, &H62, &HFB, &H8, &HD, &HC0, _
&HCA, &H40, &HC2, &HDB}
'16
'Get an encryptor.
Dim encryptor As ICryptoTransform = rijn.CreateEncryptor(key, IV)
'Encrypt the data.
Dim msEncrypt As New MemoryStream()
Dim csEncrypt As New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)
'Write all data to the crypto stream and flush it.
csEncrypt.Write(input, 0, input.Length)
csEncrypt.FlushFinalBlock()
'Get encrypted array of bytes.
encrypted = msEncrypt.ToArray()
Return encrypted
End Function
解密方法:
Public Function DecryptStream(ByVal input As Byte()) As Byte()
Dim rijn As New RijndaelManaged()
Dim decrypted As Byte()
Dim key As Byte() = New Byte() {&H22, &HC0, &H6D, &HCB, &H23, &HA6, _
&H3, &H1B, &H5A, &H1D, &HD3, &H9F, _
&H85, &HD, &HC1, &H72, &HED, &HF4, _
&H54, &HE6, &HBA, &H65, &HC, &H22, _
&H62, &HBE, &HF3, &HEC, &H14, &H81, _
&HA8, &HA}
'32
Dim IV As Byte() = New Byte() {&H43, &HB1, &H93, &HB, &H1A, &H87, _
&H52, &H62, &HFB, &H8, &HD, &HC0, _
&HCA, &H40, &HC2, &HDB}
'16
'Get a decryptor that uses the same key and IV as the encryptor.
Dim decryptor As ICryptoTransform = rijn.CreateDecryptor(key, IV)
'Now decrypt the previously encrypted message using the decryptor
' obtained in the above step.
Dim msDecrypt As New MemoryStream(input)
Dim csDecrypt As New CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)
decrypted = New Byte(input.Length - 1) {}
'Read the data out of the crypto stream.
csDecrypt.Read(decrypted, 0, decrypted.Length)
Return decrypted
End Function
4
包含這些命名空間
using System.IO;
using System.Security.Cryptography;
加密創建以下功能:
private void EncryptFile(string inputFile, string outputFile)
{
try
{
string password = @"myKey123"; // Your Key Here
UnicodeEncoding UE = new UnicodeEncoding();
byte[] key = UE.GetBytes(password);
string cryptFile = outputFile;
FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create);
RijndaelManaged RMCrypto = new RijndaelManaged();
CryptoStream cs = new CryptoStream(fsCrypt,
RMCrypto.CreateEncryptor(key, key),
CryptoStreamMode.Write);
FileStream fsIn = new FileStream(inputFile, FileMode.Open);
int data;
while ((data = fsIn.ReadByte()) != -1)
cs.WriteByte((byte)data);
fsIn.Close();
cs.Close();
fsCrypt.Close();
}
catch
{
MessageBox.Show("Encryption failed!", "Error");
}
}
解密創建以下功能:
private void DecryptFile(string inputFile, string outputFile)
{
{
string password = @"myKey123"; // Your Key Here
UnicodeEncoding UE = new UnicodeEncoding();
byte[] key = UE.GetBytes(password);
FileStream fsCrypt = new FileStream(inputFile, FileMode.Open);
RijndaelManaged RMCrypto = new RijndaelManaged();
CryptoStream cs = new CryptoStream(fsCrypt,
RMCrypto.CreateDecryptor(key, key),
CryptoStreamMode.Read);
FileStream fsOut = new FileStream(outputFile, FileMode.Create);
int data;
while ((data = cs.ReadByte()) != -1)
fsOut.WriteByte((byte)data);
fsOut.Close();
cs.Close();
fsCrypt.Close();
}
}
你可以這樣調用
EncryptFile(@"D:\OriginalImage.png", @"D:\VizioEncrypted.png"); //To Encrypt
DecryptFile(@"D:\VizioEncrypted.png", @"D:\VizioDecrypted.png"); //To Decrypt
這將有助於
+0
感謝您的努力,但我需要加密到圖像的二進制數據,以保存圖像加密到sql server數據庫。 – 2010-09-29 14:53:23
相關問題
- 1. RSA加密用Java/.NET和解密.NET
- 2. 圖像加密和解密
- 3. 使用.NET加密和解密數字
- 4. Java RSA加密 - 解密.NET
- 5. RSA .NET加密Java解密
- 6. 試圖加密和解密vigenere密碼
- 7. 在.Net中加密/解密Sql Server 2005加密列NET
- 8. 加密和解密
- 9. 加密和解密
- 10. 加密和解密
- 11. 加密和解密
- 12. 加密和解密密碼
- 13. 如何在android中加密和解密圖片?
- 14. Facebook隱藏 - 圖像加密和解密
- 15. 加密/解密大型文件(.NET)
- 16. 加密在SQL Server /解密在.Net 4
- 17. .Net RSA加密,Java RSA解密
- 18. RSA加密在PHP中解密.NET
- 19. 加密/ .NET 2.0中解密餅乾(C#)
- 20. 加密在SQL Server中的.NET /解密
- 21. C#(加密)和Java(解密)之間的AES加密/解密
- 22. VB.NET加密和解密
- 23. 加密和解密在iphonesdk
- 24. C#加密和解密
- 25. 加密和解密文件
- 26. PHP加密和VB.net解密
- 27. 加密和解密餅乾
- 28. 加密和解密IP
- 29. 加密和解密對象
- 30. php加密和解密
「圖像」 作爲一個圖像,或可執行? – GvS 2010-09-29 14:41:45
作爲字節數組的圖像我想加密它時,我將它保存到sql server – 2010-09-29 14:51:19