我使用以下内容在 .net 和 java 之间进行加密
在.net中我使用:
/// <summary>
/// DES Encryption method - used to encryp password for the java.
/// </summary>
/// <param name="plainText"></param>
/// <returns></returns>
public string EncryptData(string plainText)
{
DES des = new DESCryptoServiceProvider();
des.Mode = CipherMode.ECB;
des.Padding = PaddingMode.PKCS7;
des.Key = Encoding.UTF8.GetBytes(_secretPhrase.Substring(0, 8));
des.IV = Encoding.UTF8.GetBytes(_secretPhrase.Substring(0, 8));
byte[] bytes = Encoding.UTF8.GetBytes(plainText);
byte[] resultBytes = des.CreateEncryptor().TransformFinalBlock(bytes, 0, bytes.Length);
return Convert.ToBase64String(resultBytes);
}
/// <summary>
/// DES Decryption method - used the decrypt password encrypted in java
/// </summary>
/// <param name="encryptedText"></param>
/// <returns></returns>
public string DecryptData(string encryptedText)
{
DES des = new DESCryptoServiceProvider();
des.Mode = CipherMode.ECB;
des.Padding = PaddingMode.PKCS7;
des.Key = Encoding.UTF8.GetBytes(_secretPhrase.Substring(0, 8));
des.IV = System.Text.Encoding.UTF8.GetBytes(_secretPhrase.Substring(0, 8));
byte[] bytes = Convert.FromBase64String(encryptedText);
byte[] resultBytes = des.CreateDecryptor().TransformFinalBlock(bytes, 0, bytes.Length);
return Encoding.UTF8.GetString(resultBytes);
}
在java中我使用:
公共类 CryptoUtil {
public static final Logger LOG = Logger.getLogger(CryptoUtil.class);
private Cipher cipher = null;
private SecretKey key = null;
// This variable holds a string based on which a unique key will be generated
private static final String SECRET_PHRASE = "SECRET PHRASE GOES HERE";
// Charset will be used to convert between String and ByteArray
private static final String CHARSET = "UTF8";
// The algorithm to be used for encryption/decryption DES(Data Encryption Standard)
private static final String ALGORITHM = "DES";
public CryptoUtil() throws DDICryptoException {
try {
// generate a key from SecretKeyFactory
DESKeySpec keySpec = new DESKeySpec(SECRET_PHRASE.getBytes(CHARSET));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
key = keyFactory.generateSecret(keySpec);
cipher = Cipher.getInstance(ALGORITHM);
} catch (Exception e) {
LOG.error(e);
throw new DDICryptoException(e);
}
}
/**
* This method takes a plain text string and returns encrypted string using DES algorithm
* @param plainText
* @return String
* @throws DDICryptoException
*/
public String encrypt(String plainText) throws DDICryptoException {
String encryptedString = null;
try {
// initializes the cipher with a key.
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] plainTextAsUTF8 = plainText.getBytes(CHARSET);
// decrypts data in a single-part or multi-part operation
byte[] encryptedBytes = cipher.doFinal(plainTextAsUTF8);
encryptedString = new sun.misc.BASE64Encoder().encode(encryptedBytes);
} catch (Exception e) {
LOG.error(e);
throw new DDICryptoException(e);
}
return encryptedString;
}
/**
* This method takes a plain text string and returns encrypted string using DES algorithm
* @param encryptedString
* @return
* @throws DDICryptoException
*/
public String decrypt(String encryptedString) throws DDICryptoException {
String decryptedString = null;
try {
byte[] decodedString = new sun.misc.BASE64Decoder().decodeBuffer(encryptedString);
// initializes the cipher with a key.
cipher.init(Cipher.DECRYPT_MODE, key);
// decrypts data in a single-part or multi-part operation
byte[] decryptedBytes = cipher.doFinal(decodedString);
decryptedString = new String(decryptedBytes, CHARSET);
} catch (Exception e) {
LOG.error(e);
throw new DDICryptoException(e);
}
return decryptedString;
}
}