我正在为两个应用程序创建一个接口,该接口将使用.txt文件中的加密文本进行通信。 vb.net应用程序将加密,java将解密。
vb代码
Dim nget As String = System.IO.File.ReadAllText(sInputFilename)
Dim textBytes As Byte() = System.Text.Encoding.Unicode.GetBytes(nget)
Dim Passphrase As String = sKey
Dim UTF8 As New System.Text.UTF8Encoding()
Dim HashProvider As New MD5CryptoServiceProvider()
Dim TDESKey As Byte() = HashProvider.ComputeHash(UTF8.GetBytes(Passphrase))
Dim TDESAlgorithm As New TripleDESCryptoServiceProvider()
TDESAlgorithm.Key = TDESKey
TDESAlgorithm.Mode = CipherMode.ECB
TDESAlgorithm.Padding = PaddingMode.PKCS7
Dim ms As New System.IO.MemoryStream()
Dim encStream As New CryptoStream(ms, TDESAlgorithm.CreateEncryptor(), CryptoStreamMode.Write)
encStream.Write(textBytes, 0, textBytes.Length)
encStream.FlushFinalBlock()
Dim strSave As String = Convert.ToBase64String(ms.ToArray())
My.Computer.FileSystem.WriteAllText(sOutputFilename, strSave, False)
java代码
public static void main(String[] args) {
String codedtext = null;
try {
codedtext = readFile("ect.txt", StandardCharsets.UTF_8);
} catch (IOException e1) {
e1.printStackTrace();
}
String decodedtext = null;
try {
decodedtext = _decrypt(codedtext,"abcdefgh");
} catch (Exception e) {
e.printStackTrace();
}
}
private static String readFile(String path, Charset encoding)
throws IOException
{
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}
private static String _encrypt(String message, String secretKey) throws Exception {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] digestOfPassword = md.digest(secretKey.getBytes("utf-8"));
byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
SecretKey key = new SecretKeySpec(keyBytes, "DESede");
Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] plainTextBytes = message.getBytes("utf-8");
byte[] buf = cipher.doFinal(plainTextBytes);
byte [] base64Bytes = Base64.encodeBase64(buf);
String base64EncryptedString = new String(base64Bytes);
return base64EncryptedString;
}
private static String _decrypt(String encryptedText,String secretKey)抛出异常{
byte[] message = Base64.decodeBase64(encryptedText.getBytes("utf-8"));
//byte[] message = encryptedText.getBytes("utf-8");
MessageDigest md = MessageDigest.getInstance("SHA-1");
byte[] digestOfPassword = md.digest(secretKey.getBytes("utf-8"));
byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
SecretKey key = new SecretKeySpec(keyBytes, "DESede");
//Cipher decipher = Cipher.getInstance("DESede");
Cipher decipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
decipher.init(Cipher.DECRYPT_MODE, key);
byte[] plainText = decipher.doFinal(message);
return new String(plainText, "UTF-8");
}
但是我收到了错误
at java.lang.ArrayIndexOutOfBoundsException: -17
at org.apache.commons.codec.binary.Base64.isBase64(Base64.java:137)
at org.apache.commons.codec.binary.Base64.discardNonBase64(Base64.java:478)
at org.apache.commons.codec.binary.Base64.decodeBase64(Base64.java:374)
at Test.MyDec._decrypt(MyDec.java:74)
at Test.MyDec.main(MyDec.java:33)
从base64解码时