[英]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
Cipher.getInstance("DES/ECB/PKCS5Padding")
对于密码,请使用Cipher.getInstance("DES/ECB/PKCS5Padding")
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"))
MessageDigest.getInstance("MD5")
or "SHA1"
should do the trick. 应该使用MessageDigest.getInstance("MD5")
或"SHA1"
之类的技巧。 new SecretKeySpec(<my 8 byte byte array>, "DES")
只需执行new SecretKeySpec(<my 8 byte byte array>, "DES")
,即可创建密钥 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.