2012-04-23 15 views
-1

これらのコードを使用して、vb2008のプレーンテキストを暗号化します。
私はアンドロイドで私のアプリで生成されたファイルを復号化して使用します。 私はファイルをassetsフォルダに入れて使用することを知っています。
このコードでは、暗号化に暗号化を使用しています。 どのように私はアンドロイドアプリで生成されたファイルを解読することができます。 と私はどのように私のアンドロイドアプリでそれらを使用します。 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 JCE APIを使用してコードを変換する必要があります。基本的にあなたが使用MessageDigest.getInstance("SHA1")

  1. を希望SHA1実装
  2. ハッシュを取得するには、鍵のバイトおよびIV
  3. 使用を取得するためのパスワード3DES実装
  4. は、暗号化のための暗号を初期化し得るためにCipher.getInstance("3DES/CBC/PKCS5Padding")のようなものCipher.init()
  5. を使用して、キーとIVを使用してCipher.doFinal()
を使用してデータを暗号化

VBと同じキーとIVを最初に取得してから、暗号化を続行する必要があります。 .NETでIVを取得するには、空の文字列をハッシュしているようですが、それが何を提供するのかはわかりません。また、TripleDESCryptoServiceProviderのデフォルトのパディングが何であるかを調べるには、.NETのドキュメントをチェックしてください。

+0

これは初心者で、私のコードはサイトのソースコードからのコピーです。私の前にplzタイプのサンプル。ありがとう –

+0

あなたはこれを最初に読んで、何かをまとめてみてください。あなたが立ち往生したときに具体的な質問をしてください。 http://docs.oracle.com/javase/6/docs/technotes/guides/security/crypto/CryptoSpec.html –

+0

私は助けが必要です。 PLZ –