几种加密解密的方法(仅代码)
来源:互联网 发布:淘宝美工和ui设计师 编辑:程序博客网 时间:2024/05/22 23:06
1.Base64
编码:
byte[] encode = Base64.encode(str.getBytes(), Base64.DEFAULT);new String(encode)
解码:
byte[] decode = Base64.decode(result, Base64.DEFAULT);
new String(decode)
2.URLEncoder编码:
String encode = URLEncoder.encode(content, "UTF-8");
解码:
String decode = URLDecoder.decode(result, "UTF-8");
3.Des
加密:
try { // 初始化 设置加密解密的类型 Cipher cipher = Cipher.getInstance("DES"); // 初始化密码 SecretKeySpec keySpec = new SecretKeySpec(str.getBytes(),"DES"); try { //设置此次操作是加密还是解密 cipher.init(Cipher.ENCRYPT_MODE,keySpec); // 加密 final byte[] finalResult = cipher.doFinal(password.getBytes()); //经过base64处理避免出现乱码情况 byte[] encode = Base64.encode(finalResult, Base64.DEFAULT); String result = new String(encode); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); }} catch (NoSuchAlgorithmException e) { e.printStackTrace();} catch (NoSuchPaddingException e) { e.printStackTrace();}解密:try { Cipher cipherDecode = Cipher.getInstance("DES"); SecretKeySpec spec = new SecretKeySpec(password.getBytes(),"DES"); cipherDecode.init(Cipher.DECRYPT_MODE,spec); // 获取textView加密后的结果 byte[] decode = Base64.decode(result, Base64.DEFAULT); byte[] doFinal = cipherDecode.doFinal(decode); String result = new String(doFinal);} catch (NoSuchAlgorithmException e) { e.printStackTrace();} catch (NoSuchPaddingException e) { e.printStackTrace();} catch (InvalidKeyException e) { e.printStackTrace();} catch (BadPaddingException e) { e.printStackTrace();} catch (IllegalBlockSizeException e) { e.printStackTrace();}4.Aes
加密:
try { // 初始化 设置加密解密的类型 Cipher cipher = Cipher.getInstance("AES"); // 初始化密码 SecretKeySpec keySpec = new SecretKeySpec(password.getBytes(),"AES"); try { //设置此次操作是加密还是解密 cipher.init(Cipher.ENCRYPT_MODE,keySpec); // 加密 final byte[] finalResult = cipher.doFinal(str.getBytes()); //经过base64处理避免出现乱码情况 byte[] encode = Base64.encode(finalResult, Base64.DEFAULT); String result = new String(encode); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); }} catch (NoSuchAlgorithmException e) { e.printStackTrace();} catch (NoSuchPaddingException e) { e.printStackTrace();}
解密:try { Cipher cipherDecode = Cipher.getInstance("AES"); SecretKeySpec spec = new SecretKeySpec(passwords.getBytes(),"AES"); cipherDecode.init(Cipher.DECRYPT_MODE,spec); // 获取textView加密后的结果 byte[] decode = Base64.decode(result, Base64.DEFAULT); byte[] doFinal = cipherDecode.doFinal(decode); String result = new String(doFinal);} catch (NoSuchAlgorithmException e) { e.printStackTrace();} catch (NoSuchPaddingException e) { e.printStackTrace();} catch (InvalidKeyException e) { e.printStackTrace();} catch (BadPaddingException e) { e.printStackTrace();} catch (IllegalBlockSizeException e) { e.printStackTrace();}5.RSA
private PublicKey mPublicKey;private PrivateKey mPrivateKey;//生成公钥和密钥try { // 初始化密码对生成器 KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA"); // 设置密码的长度 generator.initialize(1024); // 生成密码对 KeyPair keyPair = generator.generateKeyPair(); // 获取公钥 mPublicKey = keyPair.getPublic(); //获取私钥 mPrivateKey = keyPair.getPrivate(); //////////////////////////把私钥存储String到本地////////////////////////////// byte[] mPrivateKeyEncoded = mPrivateKey.getEncoded(); byte[] encode = Base64.encode(mPrivateKeyEncoded, Base64.DEFAULT); String key = new String(encode); SharedPreferences sp = getSharedPreferences("day02", Context.MODE_PRIVATE); SharedPreferences.Editor edit = sp.edit(); edit.putString("privateKey",key); edit.commit();} catch (NoSuchAlgorithmException e) { e.printStackTrace();}加密:
try {
Cipher cipher = Cipher.getInstance("RSA"); // 设置加密或者解密模式 ,传入公钥 cipher.init(Cipher.ENCRYPT_MODE,mPublicKey); byte[] encode = cipher.doFinal(content.getBytes()); byte[] bytes = Base64.encode(encode, Base64.DEFAULT); String result = new String(bytes);} catch (NoSuchAlgorithmException e) { e.printStackTrace();} catch (NoSuchPaddingException e) { e.printStackTrace();} catch (InvalidKeyException e) { e.printStackTrace();} catch (BadPaddingException e) { e.printStackTrace();} catch (IllegalBlockSizeException e) { e.printStackTrace();}解密:String result = mTextView.getText().toString();byte[] decode = Base64.decode(result, Base64.DEFAULT);try { Cipher ciphers = Cipher.getInstance("RSA"); ciphers.init(Cipher.DECRYPT_MODE,mPrivateKey); byte[] bytes = ciphers.doFinal(decode); String result = new String(bytes);} catch (NoSuchAlgorithmException e) { e.printStackTrace();} catch (NoSuchPaddingException e) { e.printStackTrace();} catch (InvalidKeyException e) { e.printStackTrace();} catch (BadPaddingException e) { e.printStackTrace();} catch (IllegalBlockSizeException e) { e.printStackTrace();}通过服务器给我们的字符串来解密
6.MD5摘要// 1 通过字符串生成私钥 SharedPreferences sharedPreferences = getSharedPreferences("wenjian", Context.MODE_PRIVATE); String stringKey = sharedPreferences.getString("privateKey", ""); if (TextUtils.isEmpty(stringKey)){ Log.d("TAG", "onClick: 私钥没有存成功"); return; }else { Log.d("TAG", "onClick: StringKey == "+stringKey); } //因为我们存私钥的经过Base64编码过,所以我们必须解码回去 byte[] bytesKey = Base64.decode(stringKey, Base64.DEFAULT); // 通过byte数组生成公钥或者私钥 PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(bytesKey); KeyFactory factory = KeyFactory.getInstance("RSA"); PrivateKey privateKey = factory.generatePrivate(spec); // 2 解密 Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE,privateKey); byte[] decode = Base64.decode(result, Base64.DEFAULT); byte[] doFinal = cipher.doFinal(decode); mTextView.setText(new String(doFinal)); isEncode = false;} catch (NoSuchAlgorithmException e) { e.printStackTrace();} catch (NoSuchPaddingException e) { e.printStackTrace();} catch (InvalidKeyException e) { e.printStackTrace();} catch (BadPaddingException e) { e.printStackTrace();} catch (IllegalBlockSizeException e) { e.printStackTrace();} catch (InvalidKeySpecException e) { e.printStackTrace();}//摘要try { // 初始化 MessageDigest digest = MessageDigest.getInstance("MD5"); //指定 byte数组来进行摘要 digest.update(str.getBytes()); // 获取摘要的结果 byte[] results = digest.digest(); // 创建Buffer来累加字符串 StringBuffer buffer = new StringBuffer(); for (int i = 0; i < results.length; i++) { byte result = results[i]; // 把得到结果 传化成16进制的字符串 String s = Integer.toHexString(result & 0xFF); if (s.length() == 1) { buffer.append("0"); } buffer.append(s); } String result = buffer.toString();} catch (NoSuchAlgorithmException e) { e.printStackTrace();}7.Sha1
//摘要try { // 初始化 MessageDigest digest = MessageDigest.getInstance("SHA1"); //指定 byte数组来进行摘要 digest.update(content.getBytes()); // 获取摘要的结果 byte[] results = digest.digest(); // 创建Buffer来累加字符串 StringBuffer buffer = new StringBuffer(); for (int i = 0; i < results.length; i++) { byte result = results[i]; // 把得到结果 传化成16进制的字符串 String s = Integer.toHexString(result & 0xFF); // if (s.length()==1){ buffer.append("0"); } buffer.append(s); } String result = buffer.toString();} catch (NoSuchAlgorithmException e) { e.printStackTrace();}这里仅仅是几种加密解密的代码,具体介绍有时间再做,基本也就这么多东西,能用就行了。
阅读全文
0 0
- 几种加密解密的方法(仅代码)
- 几种字符串加密解密的方法
- C#字符串的几种加密解密方法
- shell几种字符串加密解密的方法
- .net 中的几种加密解密方法
- .net 中的几种加密解密方法
- jquery实现登录加密的几种方法以及cookie存放加密/解密
- PHP的几种加密解密算法
- .net中常用的几种加密解密
- .net的加密解密方法
- 常用的加密解密方法
- 简单的加密解密方法
- 简易的加密解密方法
- 加密解密的一些方法
- JAVASCRIPT加密方法,JS加密解密综述(7种)
- JAVASCRIPT加密方法,JS加密解密综述(7种)
- JAVASCRIPT加密方法,JS加密解密综述(7种)
- iOS代码加密的几种方式
- 常用移动应用 Packagename 和 URL Scheme
- java 1.8 hashmap 源码分析
- Interrupts on the Zynq SoC
- Maven+Jenkins+Svn+Tomcat
- 苹果被拒原因总结
- 几种加密解密的方法(仅代码)
- 雇员管理系统ssh之准备2
- RecyclerView:java.lang.IndexOutOfBoundsException: Invalid item position
- Bug生命周期
- OSS阿里云对象存储服务前端JS下载日志文件
- list倒序,获取URL的参数 .val() 判断空值
- https在weblogic下访问报错,jboss和tomcat下好使
- Python爬煎蛋网的图片——老司机的第一步
- 06--MySQL自学教程:DML(Data Manipulation Language:数据库操作语言),只操作表