项目总能遇到各种各样的加密方式,Blowfish ECB + Base64 加密,已验证可直接用
public static String encodingToBlowfishBase64(String key, String data) {
try {
Cipher cipher = Cipher.getInstance("Blowfish/ECB/NoPadding");
int blockSize = cipher.getBlockSize();
byte[] dataBytes = data.getBytes();
int length = dataBytes.length;
if (length % blockSize != 0) {
length = length + (blockSize - (length % blockSize));
}
byte[] plaintext = new byte[length];
System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "Blowfish");
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
return Base64.encodeToString(cipher.doFinal(plaintext),Base64.NO_WRAP);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
网友评论