一、对称加密算法
对称加密算法又叫私钥加密算法,是将加密和解密使用相同的密钥。常见的对称加密算法有des、3des、aes等。
des算法des(data encryption standard)是一种经典的对称加密算法,它的密钥长度为56位。在使用des算法时,需要先生成密钥,然后使用密钥加密明文,得到密文;再使用密钥解密密文,得到明文。在java中,可以使用jce(java cryptography extension)提供的des加密解密功能。示例代码如下:
import javax.crypto.cipher;import javax.crypto.keygenerator;import javax.crypto.secretkey;import javax.crypto.spec.secretkeyspec;import java.security.nosuchalgorithmexception;public class desutil { private static final string algorithm = "des"; public static string encrypt(string content, string key) throws exception { secretkeyspec skeyspec = new secretkeyspec(key.getbytes(), algorithm); cipher cipher = cipher.getinstance(algorithm); byte[] bytecontent = content.getbytes(); cipher.init(cipher.encrypt_mode, skeyspec); byte[] result = cipher.dofinal(bytecontent); return parsebyte2hexstr(result); } public static string decrypt(string content, string key) throws exception { byte[] decryptfrom = parsehexstr2byte(content); secretkeyspec skeyspec = new secretkeyspec(key.getbytes(), algorithm); cipher cipher = cipher.getinstance(algorithm); cipher.init(cipher.decrypt_mode, skeyspec); byte[] result = cipher.dofinal(decryptfrom); return new string(result); } private static string parsebyte2hexstr(byte[] buf) { stringbuilder sb = new stringbuilder(); for (byte b : buf) { string hex = integer.tohexstring(b & 0xff); if (hex.length() == 1) { hex = '0' + hex; } sb.append(hex.touppercase()); } return sb.tostring(); } private static byte[] parsehexstr2byte(string hexstr) { if (hexstr.length() < 1) { return null; } byte[] result = new byte[hexstr.length() / 2]; for (int i = 0; i < hexstr.length() / 2; i++) { int high = integer.parseint(hexstr.substring(i * 2, i * 2 + 1), 16); int low = integer.parseint(hexstr.substring(i * 2 + 1, i * 2 + 2), 16); result[i] = (byte) (high * 16 + low); } return result; } public static void main(string[] args) throws exception { string content = "hello world!"; string key = "12345678"; string encrypt = encrypt(content, key); system.out.println("加密后:" + encrypt); string decrypt = decrypt(encrypt, key); system.out.println("解密后:" + decrypt); }}
3des算法3des(triple des)算法是在des算法的基础上加强的一种加密算法,密钥长度为168位。3des算法的加密解密过程类似于des算法,可以使用java提供的jce库进行实现。示例代码如下:
import javax.crypto.cipher;import javax.crypto.keygenerator;import javax.crypto.secretkey;import javax.crypto.spec.secretkeyspec;import java.security.nosuchalgorithmexception;public class tripledesutil { private static final string algorithm = "desede"; private static byte[] initkey() throws nosuchalgorithmexception { keygenerator kg = keygenerator.getinstance(algorithm); kg.init(168); // 3des的密钥长度为168位 secretkey secretkey = kg.generatekey(); return secretkey.getencoded(); } public static string encrypt(string content, byte[] key) throws exception { secretkeyspec skeyspec = new secretkeyspec(key, algorithm); cipher cipher = cipher.getinstance(algorithm); byte[] bytecontent = content.getbytes(); cipher.init(cipher.encrypt_mode, skeyspec); byte[] result = cipher.dofinal(bytecontent); return parsebyte2hexstr(result); } public static string decrypt(string content, byte[] key) throws exception { byte[] decryptfrom = parsehexstr2byte(content); secretkeyspec skeyspec = new secretkeyspec(key, algorithm); cipher cipher = cipher.getinstance(algorithm); cipher.init(cipher.decrypt_mode, skeyspec); byte[] result = cipher.dofinal(decryptfrom); return new string(result); } private static string parsebyte2hexstr(byte[] buf) { stringbuilder sb = new stringbuilder(); for (byte b : buf) { string hex = integer.tohexstring(b & 0xff); if (hex.length() == 1) { hex = '0' + hex; } sb.append(hex.touppercase()); } return sb.tostring(); } private static byte[] parsehexstr2byte(string hexstr) { if (hexstr.length() < 1) { return null; } byte[] result = new byte[hexstr.length() / 2]; for (int i = 0; i < hexstr.length() / 2; i++) { int high = integer.parseint(hexstr.substring(i * 2, i * 2 + 1), 16); int low = integer.parseint(hexstr.substring(i * 2 + 1, i * 2 + 2), 16); result[i] = (byte) (high * 16 + low); } return result; } public static void main(string[] args) throws exception { string content = "hello world!"; byte[] key = initkey(); string encrypt = encrypt(content, key); system.out.println("加密后:" + encrypt); string decrypt = decrypt(encrypt, key); system.out.println("解密后:" + decrypt); }}
aes算法aes(advanced encryption standard)算法是一种高级加密标准,是目前最流行的对称加密算法之一。aes算法的密钥长度一般为128位、192位或256位。在java中,也可以使用jce库提供的aes加密解密功能。示例代码如下:
import javax.crypto.cipher;import javax.crypto.keygenerator;import javax.crypto.secretkey;import javax.crypto.spec.secretkeyspec;import java.security.nosuchalgorithmexception;public class aesutil { private static final string algorithm = "aes"; private static byte[] initkey() throws nosuchalgorithmexception { keygenerator kg = keygenerator.getinstance(algorithm); kg.init(128); // aes的密钥长度为128位、192位、256位 secretkey secretkey = kg.generatekey(); return secretkey.getencoded(); } public static string encrypt(string content, byte[] key) throws exception { secretkeyspec skeyspec = new secretkeyspec(key, algorithm); cipher cipher = cipher.getinstance(algorithm); byte[] bytecontent = content.getbytes(); cipher.init(cipher.encrypt_mode, skeyspec); byte[] result = cipher.dofinal(bytecontent); return parsebyte2hexstr(result); } public static string decrypt(string content, byte[] key) throws exception { byte[] decryptfrom = parsehexstr2byte(content); secretkeyspec skeyspec = new secretkeyspec(key, algorithm); cipher cipher = cipher.getinstance(algorithm); cipher.init(cipher.decrypt_mode, skeyspec); byte[] result = cipher.dofinal(decryptfrom); return new string(result); } private static string parsebyte2hexstr(byte[] buf) { stringbuilder sb = new stringbuilder(); for (byte b : buf) { string hex = integer.tohexstring(b & 0xff); if (hex.length() == 1) { hex = '0' + hex; } sb.append(hex.touppercase()); } return sb.tostring(); } private static byte[] parsehexstr2byte(string hexstr) { if (hexstr.length() < 1) { return null; } byte[] result = new byte[hexstr.length() / 2]; for (int i = 0; i < hexstr.length() / 2; i++) { int high = integer.parseint(hexstr.substring(i * 2, i * 2 + 1), 16); int low = integer.parseint(hexstr.substring(i * 2 + 1, i * 2 + 2), 16); result[i] = (byte) (high * 16 + low); } return result; } public static void main(string[] args) throws exception { string content = "hello world!"; byte[] key = initkey(); string encrypt = encrypt(content, key); system.out.println("加密后:" + encrypt); string decrypt = decrypt(encrypt, key); system.out.println("解密后:" + decrypt); }}
二、非对称加密算法
非对称加密算法又叫公钥加密算法,是加密和解密使用不同的密钥。常见的非对称加密算法有rsa算法。
rsa算法rsa算法是一种非对称加密算法,是一种基于大整数的数论加密算法。rsa算法的特点是公钥可以公开,密钥是私有的,因此安全性较高。在java中,也可以使用jce库提供的rsa加密解密功能。示例代码如下:
import javax.crypto.cipher;import java.security.keypair;import java.security.keypairgenerator;import java.security.nosuchalgorithmexception;import java.security.privatekey;import java.security.publickey;public class rsautil { private static final string algorithm = "rsa"; private static keypair getkeypair(int keysize) throws nosuchalgorithmexception { keypairgenerator keypairgenerator = keypairgenerator.getinstance(algorithm); keypairgenerator.initialize(keysize); return keypairgenerator.generatekeypair(); } public static string encrypt(string content, publickey publickey) throws exception { cipher cipher = cipher.getinstance(algorithm); cipher.init(cipher.encrypt_mode, publickey); byte[] bytecontent = content.getbytes(); byte[] result = cipher.dofinal(bytecontent); return parsebyte2hexstr(result); } public static string decrypt(string content, privatekey privatekey) throws exception { byte[] decryptfrom = parsehexstr2byte(content); cipher cipher = cipher.getinstance(algorithm); cipher.init(cipher.decrypt_mode, privatekey); byte[] result = cipher.dofinal(decryptfrom); return new string(result); } private static string parsebyte2hexstr(byte[] buf) { stringbuilder sb = new stringbuilder(); for (byte b : buf) { string hex = integer.tohexstring(b & 0xff); if (hex.length() == 1) { hex = '0' + hex; } sb.append(hex.touppercase()); } return sb.tostring(); } private static byte[] parsehexstr2byte(string hexstr) { if (hexstr.length() < 1) { return null; } byte[] result = new byte[hexstr.length() / 2]; for (int i = 0; i < hexstr.length() / 2; i++) { int high = integer.parseint(hexstr.substring(i * 2, i * 2 + 1), 16); int low = integer.parseint(hexstr.substring(i * 2 + 1, i * 2 + 2), 16); result[i] = (byte) (high * 16 + low); } return result; } public static void main(string[] args) throws exception { string content = "hello world!"; keypair keypair = getkeypair(1024); publickey publickey = keypair.getpublic(); privatekey privatekey = keypair.getprivate(); string encrypt = encrypt(content, publickey); system.out.println("加密后:" + encrypt); string decrypt = decrypt(encrypt, privatekey); system.out.println("解密后:" + decrypt); }}
以上就是几种常见的java实现的加密解密算法。在实际的应用中,需要根据实际情况选择适合自己的加密解密算法,并根据具体的需求进行调整和优化。
以上就是java实现的常见加密解密算法的详细内容。
