2012-04-23 103 views
-1

我使用這些代碼來加密vb2008中的任何純文本。
我想在我的應用程序在android中解密和使用生成的文件。 我知道把文件放在資產文件夾中,我使用它。
此代碼使用加密來加密。 我如何解密我的android應用程序中生成的文件。 以及我如何在我的Android應用程序中使用它們。 有什麼想法?VB2008和Android之間的加密/解密

Imports System.Security.Cryptography 

Public NotInheritable Class Simple3Des 

    Private TripleDes As New TripleDESCryptoServiceProvider 

    Private Function TruncateHash(ByVal key As String, ByVal length As Integer) As Byte() 

     Dim sha1 As New SHA1CryptoServiceProvider 

     ' Hash the key. 
     Dim keyBytes() As Byte = System.Text.Encoding.Unicode.GetBytes(key) 
     Dim hash() As Byte = sha1.ComputeHash(keyBytes) 

     ' Truncate or pad the hash. 
     ReDim Preserve hash(length - 1) 
     Return hash 
    End Function 

    Sub New(ByVal key As String) 
     ' Initialize the crypto provider. 
     TripleDes.Key = TruncateHash(key, TripleDes.KeySize \ 8) 
     TripleDes.IV = TruncateHash("", TripleDes.BlockSize \ 8) 
    End Sub 

    Public Function EncryptData(ByVal plaintext As String) As String 

     ' Convert the plaintext string to a byte array. 
     Dim plaintextBytes() As Byte = System.Text.Encoding.Unicode.GetBytes(plaintext) 

     ' Create the stream. 
     Dim ms As New System.IO.MemoryStream 
     ' Create the encoder to write to the stream. 
     Dim encStream As New CryptoStream(ms, TripleDes.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write) 

     ' Use the crypto stream to write the byte array to the stream. 
     encStream.Write(plaintextBytes, 0, plaintextBytes.Length) 
     encStream.FlushFinalBlock() 

     ' Convert the encrypted stream to a printable string. 
     Return Convert.ToBase64String(ms.ToArray) 
    End Function 

    Public Function DecryptData(ByVal encryptedtext As String) As String 

     ' Convert the encrypted text string to a byte array. 
     Dim encryptedBytes() As Byte = Convert.FromBase64String(encryptedtext) 

     ' Create the stream. 
     Dim ms As New System.IO.MemoryStream 
     ' Create the decoder to write to the stream. 
     Dim decStream As New CryptoStream(ms, TripleDes.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write) 

     ' Use the crypto stream to write the byte array to the stream. 
     decStream.Write(encryptedBytes, 0, encryptedBytes.Length) 
     decStream.FlushFinalBlock() 

     ' Convert the plaintext stream to a string. 
     Return System.Text.Encoding.Unicode.GetString(ms.ToArray) 
    End Function 

    End Class 

,並在我的表單類

Sub TestEncoding() 
     Dim plainText As String = InputBox("Enter the plain text:") 
     Dim password As String = InputBox("Enter the password:") 

     Dim wrapper As New Simple3Des(password) 
     Dim cipherText As String = wrapper.EncryptData(plainText) 

     MsgBox("The cipher text is: " & cipherText) 
     My.Computer.FileSystem.WriteAllText(My.Computer.FileSystem.SpecialDirectories.Desktop & "\cipherText.txt", cipherText, False) 
    End Sub 

回答

0

這不是具體到Android,你需要使用Java API的JCE翻譯你的代碼。基本上你會

  1. 使用MessageDigest.getInstance("SHA1")獲得SHA1實現
  2. 散列你的密碼,以獲得密鑰字節和IV
  3. 使用類似Cipher.getInstance("3DES/CBC/PKCS5Padding")獲得3DES實現
  4. 初始化加密的密碼使用密鑰和IV使用Cipher.init()
  5. 加密數據使用Cipher.doFinal()

您需要確保獲得與VB中相同的密鑰和IV,然後繼續進行加密。你似乎正在哈希一個空字符串來獲得在.NET中的IV,不知道是什麼給你。另請檢查.NET文檔以檢查TripleDESCryptoServiceProvider的默認填充是什麼。

+0

我是這個初學者,我的代碼是從一個網站的源代碼複製。 PLZ型樣品在我身邊。謝謝 – 2012-04-23 14:03:51

+0

您可能想先閱讀本文並嘗試將某些內容放在一起。遇到困難時詢問具體問題。 http://docs.oracle.com/javase/6/docs/technotes/guides/security/crypto/CryptoSpec.html – 2012-04-23 14:17:28

+0

我需要幫助。 PLZ – 2012-04-27 17:06:45