国密算法,明文、密文、密码、密钥、对称加密、非对称加密简单理解

国密算法,明⽂、密⽂、密码、密钥、对称加密、⾮对称加密
简单理解
⽬录
android des加密string:
shi;
import android.util.Log;
import com.blankj.utilcode.util.StringUtils;
import java.security.SecureRandom;
pto.Cipher;
pto.SecretKey;
pto.SecretKeyFactory;
pto.spec.DESKeySpec;
/**
* @ProjectName: ceshi
* @Package: shi
* @ClassName: DESEncryptUtil
* @Description: java类作⽤描述
* @Author: 作者名
* @CreateDate: 2020/7/20 0020 下午 4:14
* @UpdateUser: 更新者:
* @UpdateDate: 2020/7/20 0020 下午 4:14
*/
public class DESEncryptUtil {
public static final String PASS_WORD = "f7f0f67e45";
private static SecretKeyFactory keyFactory = null;
private static Cipher cipher = null;
static {
try {
keyFactory = Instance("DES");
} catch (Exception e) {
keyFactory = null;
}
try {
cipher = Instance("DES");
} catch (Exception e) {
cipher = null;
}
}
/**
* DES加密
*
* @param password 密码,长度要是8的倍数
* @return
*/
海陵路小学
public static String encrypt(String data, String password) throws Exception {
try {
SecureRandom random = new SecureRandom();
DESKeySpec desKey = new Bytes());
// 创建⼀个密匙⼯⼚,然后⽤它把DESKeySpec转换成
if (keyFactory == null)
keyFactory = Instance("DES");
SecretKey securekey = ateSecret(desKey);
// Cipher对象实际完成加密操作虾球转
if (cipher == null)
cipher = Instance("DES");
/
/ ⽤密匙初始化Cipher对象
cipher.init(Cipher.ENCRYPT_MODE, securekey, random);
// 现在,获取数据并加密
// 正式执⾏加密操作
return ByteStringConvertUtil.byte2HexStr(cipher.Bytes()));        } catch (Exception e) {
throw new Exception("DES解密失败!");
}
}
public static String decrypt(String data, String password) throws Exception {
try {
// DES算法要求有⼀个可信任的随机数源
SecureRandom random = new SecureRandom();
// 创建⼀个DESKeySpec对象
DESKeySpec desKey = new Bytes());
// 创建⼀个密匙⼯⼚
if (keyFactory == null)
keyFactory = Instance("DES");
银蒿// 将DESKeySpec对象转换成SecretKey对象
SecretKey securekey = ateSecret(desKey);
// Cipher对象实际完成解密操作
if (cipher == null)
cipher = Instance("DES");
/
/ ⽤密匙初始化Cipher对象
cipher.init(Cipher.DECRYPT_MODE, securekey, random);
// 真正开始解密操作
return new String(cipher.doFinal(ByteStringConvertUtil.hexStr2Byte(data)));        } catch (Exception e) {
throw new Exception("DES解密失败!");
}
}
}
shi;
public class ByteStringConvertUtil {
/**
* 字符串转换为字节数据
*
* @param hexStr
* @return
*/
public static byte[] hexStr2Byte(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;
}
/**
* 字节数组转换为字符串
*
* @param buf
* @return
*/
public static String byte2HexStr(byte buf[]) {
StringBuffer sb = new StringBuffer();
读写结合
for (int i = 0; i < buf.length; i++) {
String hex = HexString(buf[i] & 255);
if (hex.length() == 1)
hex = (new StringBuilder(String.valueOf('0'))).append(hex).toString();
sb.LowerCase());
}
String();
}
}
测试代码:
@Test
public void ceshia() throws Exception {
String s = "a77a7aaaa";
byte [] data_Bytes();
if(data_Voucherno.length % 8 != 0){ //not a multiple of 8
//create a new array with a size which is a multiple of 8
byte[] padded = new byte[data_Voucherno.length + 8 - (data_Voucherno.length % 8)];            //copy the old array into it
System.arraycopy(data_Voucherno, 0, padded, 0, data_Voucherno.length);
data_Voucherno = padded;
}
String key = "CCIC2020";
String pt(s,key);
String undata=DESEncryptUtil.decrypt(data,key);
System.out.println(data+11);
System.out.println(undata+11);
}
还有⼀种:
shi;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
pto.BadPaddingException;
pto.Cipher;
pto.IllegalBlockSizeException;
pto.NoSuchPaddingException;
pto.SecretKey;
pto.SecretKeyFactory;
pto.spec.DESKeySpec;
/**
* @ProjectName: ceshi
* @Package: shi
* @ClassName: Des
* @Description: java类作⽤描述
* @Author: 作者名
* @CreateDate: 2020/7/20 0020 下午 4:01
* @UpdateUser: 更新者:
* @UpdateDate: 2020/7/20 0020 下午 4:01
*/
public class Des {
/**
* 加密(使⽤DES算法)
*
* @param txt
*            需要加密的⽂本
* @param key
*            密钥
* @return 成功加密的⽂本
* @throws InvalidKeySpecException
* @throws InvalidKeyException
* @throws NoSuchPaddingException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
*/
public static String enCrypto(String txt, String key)
throws InvalidKeySpecException, InvalidKeyException,
NoSuchPaddingException, IllegalBlockSizeException,
BadPaddingException {
StringBuffer sb = new StringBuffer();
DESKeySpec desKeySpec = new Bytes());
SecretKeyFactory skeyFactory = null;
Cipher cipher = null;
try {
skeyFactory = Instance("DES");
cipher = Instance("DES");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
SecretKey deskey = ateSecret(desKeySpec);
cipher.init(Cipher.ENCRYPT_MODE, deskey);
byte[] cipherText = cipher.Bytes());
for (int n = 0; n < cipherText.length; n++) {
String stmp = (java.HexString(cipherText[n] & 0XFF));            if (stmp.length() == 1) {
if (stmp.length() == 1) {
sb.append("0" + stmp);
} else {
sb.append(stmp);
}
}
String().toUpperCase();
}
/**
* 解密(使⽤DES算法)
*
* @param txt
pdm
*            需要解密的⽂本
* @param key
*            密钥
* @return 成功解密的⽂本
* @throws InvalidKeyException
* @throws InvalidKeySpecException
* @throws NoSuchPaddingException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
*/
public static String deCrypto(String txt, String key)
throws InvalidKeyException, InvalidKeySpecException,
NoSuchPaddingException, IllegalBlockSizeException,
BadPaddingException {
DESKeySpec desKeySpec = new Bytes());
SecretKeyFactory skeyFactory = null;
伦理电线在2019Cipher cipher = null;
try {
skeyFactory = Instance("DES");
cipher = Instance("DES");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
SecretKey deskey = ateSecret(desKeySpec);
cipher.init(Cipher.DECRYPT_MODE, deskey);
byte[] btxts = new byte[txt.length() / 2];
for (int i = 0, count = txt.length(); i < count; i += 2) {
btxts[i / 2] = (byte) Integer.parseInt(txt.substring(i, i + 2), 16);
}
return (new String(cipher.doFinal(btxts)));
}
public static void main(String[] args) throws InvalidKeyException,
NoSuchAlgorithmException, InvalidKeySpecException,
NoSuchPaddingException, IllegalBlockSizeException,
BadPaddingException {
String soureTxt = "这⾥是要加密的⽂本内容";
String key = "这⾥是⾃定义的密钥";
String str = null;
System.out.println("明⽂:" + soureTxt);
str = enCrypto(soureTxt, key);
System.out.println("加密:" + str);
System.out.println("解密:" + deCrypto(str, key));
System.out.println(str + "====" + key);
}
}
国密算法,明⽂、密⽂、密码、密钥、对称加密、⾮对称加密简单理解

本文发布于:2024-09-21 22:51:35,感谢您对本站的认可!

本文链接:https://www.17tex.com/xueshu/327306.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:加密   对称   对象   算法   操作   密钥   转换   字节
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议