简体   繁体   English

将加密算法从VB.NET转换为Java(Android)

[英]Convert Encryption Algorithm from VB.NET to Java (Android)

i'm using the following algorithm to encrypt and decrypt a string in VB.NET and wanted to do that same method in Android also. 我正在使用以下算法对VB.NET中的字符串进行加密和解密,并希望在Android中也执行相同的方法。 can anyone tell me the similar algorithm for Android (Java) 谁能告诉我类似的Android(Java)算法

Encryption: 加密:

Private Function decryptStr(ByVal key As String, ByVal enc As String) As String
    Try
        DES.Key = Hash.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(key))
        DES.Mode = System.Security.Cryptography.CipherMode.ECB
        Dim DESDecrypter As System.Security.Cryptography.ICryptoTransform = DES.CreateDecryptor
        Dim Buffer As Byte() = Convert.FromBase64String(enc)
        respass = System.Text.ASCIIEncoding.ASCII.GetString(DESDecrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
        Return respass
    Catch ex As Exception
        Return enc
    End Try
End Function

Decryption: 解密:

Public Function decryptStr(ByVal encrypted As String, ByVal key As String) As String
    Try
        DES.Key = Hash.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(key))
        DES.Mode = System.Security.Cryptography.CipherMode.ECB
        Dim DESEncrypter As System.Security.Cryptography.ICryptoTransform = DES.CreateEncryptor
        Dim Buffer As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(encrypted)
        respass = Convert.ToBase64String(DESEncrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
        Return respass
    Catch ex As Exception
        Return encrypted
    End Try
End Function
  1. For the Cipher, use Cipher.getInstance("DES/ECB/PKCS5Padding") 对于密码,请使用Cipher.getInstance("DES/ECB/PKCS5Padding")
  2. For getting the bytes to input in the hash function to get the key, use String.getBytes(key, Charset.forName("ASCII")) and new String(keyData, Charset.forName("ASCII")) the other way round 为了使字节输入到哈希函数中以获取String.getBytes(key, Charset.forName("ASCII"))使用String.getBytes(key, Charset.forName("ASCII"))new String(keyData, Charset.forName("ASCII"))
  3. Use the (as yet unspecified) hash function. 使用(尚未指定)哈希函数。 Make sure you set the key size to 8 bytes afterwards, as DES only needs 8 characters for the key. 之后请确保将密钥大小设置为8个字节,因为DES密钥仅需要8个字符。 Something like MessageDigest.getInstance("MD5") or "SHA1" should do the trick. 应该使用MessageDigest.getInstance("MD5")"SHA1"之类的技巧。
  4. Create the key by simply performing new SecretKeySpec(<my 8 byte byte array>, "DES") 只需执行new SecretKeySpec(<my 8 byte byte array>, "DES") ,即可创建密钥
  5. Base 64 encoding/Decoding is not installed by default, look into commons-codec from Apache to do this. 默认情况下未安装Base 64编码/解码,请查看Apache的commons-codec进行此操作。

If you use character encodings at several places, just create a constant: 如果您在多个地方使用字符编码,只需创建一个常量:

private static final Charset ASCII = Charset.forName("ASCII");

The new and improved way of getting part of a byte array is: 获取字节数组的一部分的新方法是:

Arrays.copyOfRange(byte[] original, int from, int to): byte[]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM