JAVA-RSA/DES加密实现备忘.
1. RsaEncoder.java
public class DesEncoder {private static final String DES = "DES";private static final String PADDING = "DES/ECB/PKCS5Padding";private static final String DEFAULT_ENCODING = "utf-8";public final static String encrypt(String code, String key) {try {return Base64.encodeBase64String(encrypt(code.getBytes(DEFAULT_ENCODING), key.getBytes(DEFAULT_ENCODING)));} catch (Exception e) {//}return null;}public static byte[] encrypt(byte[] src, byte[] key) throws Exception {SecureRandom sr = new SecureRandom();DESKeySpec dks = new DESKeySpec(key);SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);SecretKey securekey = keyFactory.generateSecret(dks);Cipher cipher = Cipher.getInstance(PADDING);cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);return cipher.doFinal(src);}public final static String decrypt(String data, String key) {try {//base64,default-charset is UTF-8return new String(decrypt(Base64.decodeBase64(data),key.getBytes(DEFAULT_ENCODING)), DEFAULT_ENCODING);} catch (Exception e) {//}return null;}public static byte[] decrypt(byte[] src, byte[] key) throws Exception {SecureRandom sr = new SecureRandom();DESKeySpec dks = new DESKeySpec(key);SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);SecretKey securekey = keyFactory.generateSecret(dks);Cipher cipher = Cipher.getInstance(PADDING);cipher.init(Cipher.DECRYPT_MODE, securekey, sr);return cipher.doFinal(src);}public static void main(String[] args) {String tmp = DesEncoder.encrypt("821", "yc2JffcREheFQlYFIAY5f9sY7uflgBTo"); System.out.print(DesEncoder.decrypt(tmp, "yc2JffcREheFQlYFIAY5f9sY7uflgBTo"));}}