android des 加密
程序员文章站
2022-06-15 13:20:29
...
对称 , 没有随机码(或者是使用系统内部的随机码),加密内容一对一,不会随时改变;
加密key byte lengh = 8 ,不可加长或者变短, 易碰撞破解;
比较简单 ,性能高点;
package widget; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; import android.util.Base64; public class CipherDes { private static final String Algorithm = "DES"; //定义 加密算法,可用 DES,DESede,Blowfish // 加密字符串 public String encryptMode(byte[] keybyte, String input) { checkKey(keybyte); try { // 生成密钥 SecretKey deskey = new SecretKeySpec(keybyte, Algorithm); // 加密 Cipher c1 = Cipher.getInstance(Algorithm); c1.init(Cipher.ENCRYPT_MODE, deskey); byte[] results = c1.doFinal(input.getBytes("utf-8")); String encrypted = Base64.encodeToString(results, Base64.DEFAULT); return encrypted; } catch (java.security.NoSuchAlgorithmException e1) { e1.printStackTrace(); } catch (javax.crypto.NoSuchPaddingException e2) { e2.printStackTrace(); } catch (java.lang.Exception e3) { e3.printStackTrace(); } return null; } // 解密字符串 public String decryptMode(byte[] keybyte, String encrypted) { checkKey(keybyte); try { // 生成密钥 byte[] src = Base64.decode(encrypted, Base64.DEFAULT); SecretKey deskey = new SecretKeySpec(keybyte, Algorithm); // 解密 Cipher c1 = Cipher.getInstance(Algorithm); c1.init(Cipher.DECRYPT_MODE, deskey); return new String(c1.doFinal(src),"utf-8"); } catch (java.security.NoSuchAlgorithmException e1) { e1.printStackTrace(); } catch (javax.crypto.NoSuchPaddingException e2) { e2.printStackTrace(); } catch (java.lang.Exception e3) { e3.printStackTrace(); } return null; } private void checkKey(byte key[]){ if(key.length != 8) throw new IllegalArgumentException("key byte lenght should = 8"); } }
上一篇: java对称加密DES/3DES/AES
下一篇: JAVA实现DES加密