国内购物平台都有哪些,优化网站图片,wordpress调用分类目录插件,医疗网站开发pptCipher本身可以加解密byte[]#xff0c;而且可以设定key和algorithm#xff0c;安全性高。但是不能直接用于文本。因为UTF-8是可变长度的编码#xff0c;有的字符需要用多个字节来表示#xff0c;转换之后会出现byte[]数组长度、内容不一致的情况。
Base64也是对称加解密的…Cipher本身可以加解密byte[]而且可以设定key和algorithm安全性高。但是不能直接用于文本。因为UTF-8是可变长度的编码有的字符需要用多个字节来表示转换之后会出现byte[]数组长度、内容不一致的情况。
Base64也是对称加解密的可以解决byte[]转文本的问题但是无法设置key和algorithm安全性差一些。
Cipher和Base64一起使用可以有效的处理安全性和文本转换的问题。 //加密public static String encrypt(String str) {try {SecretKeySpec secretKeySpec new SecretKeySpec(key.getBytes(), AES);Cipher cipher Cipher.getInstance(AES);cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);byte[] bytes cipher.doFinal(str.getBytes());String encryptedText Base64.getEncoder().encodeToString(bytes);//System.out.println(Encrypted Text: encryptedText);return encryptedText;} catch (Exception e) {e.printStackTrace();}return null;}// 解密public static String decrypt(String str) {try {SecretKeySpec secretKeySpec new SecretKeySpec(key.getBytes(), AES);Cipher cipher Cipher.getInstance(AES);cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);byte[] bytes cipher.doFinal(Base64.getDecoder().decode(str));String decryptedText new String(bytes);//System.out.println(Decrypted Text: decryptedText);return decryptedText;} catch (Exception e) {e.printStackTrace();}return null;}