The java version and the C# version can encrypt and decrypt each other.
Among them, PasswordUtil.java and PasswordUtil.cs can encrypt and decrypt each other.
DesUtil.java and DesUtil.cs can encrypt and decrypt each other.
1. The following two encryption methods are the same
method one:
Security.addProvider(new com.sun.crypto.provider.SunJCE()); byte[] arrB = new byte[8]; // Convert raw byte array to 8 bits for (int i = 0; i < key.getBytes().length && i < arrB.length; i++) { arrB[i] = key.getBytes()[i]; } // generate key Key desKey = new javax.crypto.spec.SecretKeySpec(arrB, "DES"); Cipher encryptCipher = Cipher.getInstance("DES"); encryptCipher.init(Cipher.ENCRYPT_MODE, desKey); byte[] resultByte = encryptCipher.doFinal(message.getBytes());
2. Method 2:
Cipher cipher = Cipher.getInstance("DES");
byte[] arrBTmp = key.getBytes();
byte[] arrB = new byte[8];
// Convert raw byte array to 8 bits
for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
arrB [i] = arrBTmp [i];
}
DESKeySpec desKeySpec = new DESKeySpec(arrB);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encodeByte = cipher.doFinal(message.getBytes());