You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
1.9 KiB
54 lines
1.9 KiB
3 years ago
|
package com.fr.plugin.tools;
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
import javax.crypto.*;
|
||
|
import javax.crypto.spec.DESKeySpec;
|
||
|
|
||
|
import sun.misc.BASE64Encoder;
|
||
|
|
||
|
import java.io.IOException;
|
||
|
import java.security.InvalidKeyException;
|
||
|
import java.security.NoSuchAlgorithmException;
|
||
|
import java.security.SecureRandom;
|
||
|
import java.security.spec.InvalidKeySpecException;
|
||
|
|
||
|
public class EncryptTool
|
||
|
{
|
||
|
private byte[] encryptString(byte[] data, byte[] rawKeyData)
|
||
|
throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, BadPaddingException, IllegalBlockSizeException, IllegalStateException
|
||
|
{
|
||
|
SecureRandom sr = new SecureRandom();
|
||
|
DESKeySpec dks = new DESKeySpec(rawKeyData);
|
||
|
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
|
||
|
SecretKey key = keyFactory.generateSecret(dks);
|
||
|
Cipher cipher = Cipher.getInstance("DES");
|
||
|
cipher.init(1, key, sr);
|
||
|
byte[] encryptedData = cipher.doFinal(data);
|
||
|
return encryptedData;
|
||
|
}
|
||
|
|
||
|
public String encryptString(String rawStr, String rawKey)
|
||
|
throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, BadPaddingException, IllegalBlockSizeException, IllegalStateException, IOException
|
||
|
{
|
||
|
byte[] rawStrData = rawStr.getBytes();
|
||
|
byte[] rawKeyData = rawKey.getBytes();
|
||
|
byte[] encryptData = encryptString(rawStrData, rawKeyData);
|
||
|
String encryptStr = new BASE64Encoder().encode(encryptData);
|
||
|
byte[] c = encryptStr.getBytes();
|
||
|
StringBuffer strBuf = new StringBuffer();
|
||
|
for (int i = 0; i < c.length; ++i)
|
||
|
{
|
||
|
int t = c[i];
|
||
|
String str = Integer.toHexString(t);
|
||
|
if (str.length() == 1)
|
||
|
strBuf.append(0);
|
||
|
strBuf.append(Integer.toHexString(t));
|
||
|
}
|
||
|
return strBuf.toString();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|