「.key文件java」key文件如何编辑
本篇文章给大家谈谈.key文件java,以及key文件如何编辑对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、老师你好,在ssl证书中,cer是公钥证书,key是私钥证书,crt是证书链?对吗?
- 2、Java 怎么根据key删除properties文件中的属性
- 3、JAVA基础 key在这里干什么?搞不懂
- 4、android 打包的key在哪个文件
- 5、java中如何实现对文件和字符串加密. 解密?
- 6、如何在由openssl生成的java中使用.key和.crt文件
老师你好,在ssl证书中,cer是公钥证书,key是私钥证书,crt是证书链?对吗?
key是SSL证书私钥。而.cer和.crt是证书链,其中,.cer是服务器证书,.crt是中间证书。
一般上传的时候都是合着上传写入的,比如百度云加速上传SSL证书的时候,在证书输入框输入.cer的文本,再回车输入.crt的文件,即可完成证书链。
ssl证书优点:
一般说来,在网上进行电子商务交易时,交易双方需要使用数字签名来表明自己的身份,并使用数字签名来进行有关的交易操作。随着电子商务的盛行,数字签章的颁发机构 CA中心将为电子商务的发展提供可靠的安全保障。
一个有效、可信的 SSL 数字证书包括一个公共密钥和一个私用密钥。公共密钥用于加密信息,私用密钥用于解译加密的信息。因此,浏览器指向一个安全域时,SSL 将同步确认服务器和客户端,并创建一种加密方式和一个唯一的会话密钥。
一般而言,由 CA 业界发出的数字证书,有别于国内浏览器业者比对域名信息等方式,采取更为严格的企业及所有权验证,为电商环境树立更为可信的运作环境。
如果您的网站使用 SSL 证书 (SSL Certificates),并显示了签章 (Secured Seal),您的客户就知道他们的交易安全可靠,并且充分信赖您的网站。
Java 怎么根据key删除properties文件中的属性
java 根据key删除properties文件中的属性可以使用文件流先加载,然后读取key,修改制定的值,如下代码:
/**
*写入properties信息
* @param parameterName 配置文件属性名
* @param parameterValue 需要写入的配置文件的信息
*/
public static void writeProperties(String parameterName,
String parameterValue) throws
IOException {
Properties prop = new Properties();
try {
InputStream fis = new FileInputStream(ctxRealPath);
//从输入流中读取属性列表(键和元素对)
prop.load(fis);
//调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。
//强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
OutputStream fos = new FileOutputStream(ctxRealPath);
prop.put(parameterName, parameterValue);
//以适合使用 load 方法加载到 Properties 表中的格式,
//将此 Properties 表中的属性列表(键和元素对)写入输出流
prop.store(fos, " Update '" + parameterName + "' value");
setLastUpdateBalanceStat(parameterValue);
}
catch (IOException e) {
// Print.print("ConfigInfoError","Visit "+filePath+" for updating "+parameterName+" value error");
System.err.println("**********************");
System.err.println("\r\n write BalanceStat configuration failed,please check "+ctxRealPath+" is writer . thank you \n\n");
System.err.println("**********************");
// throw e;
}
}
调用的时候你只要传入
writeProperties(key2,123456);
JAVA基础 key在这里干什么?搞不懂
key在这里是你要查找的内容(文字列)。
举个例子
String str = "abcdef";
String key = "cde";
int index = 1;
这时
str.indexOf(key, index)
就返回2。因为我们从"abcdef"的index=1(b的位置)开始找,找到了"cde","cde"在str中的index=2。
此时,我们要继续找到话就要从 str 的 index = 2 + 3 的地方开始找。
2代表的是 "cde"在str中的index
3代表的是"cde"的长度,也就是key.length()。
android 打包的key在哪个文件
第一种利用 java jdk 来生成
第二种. 利用eclipse 来生成
a.
进入 cmd 命令行提示符
进入到jdk的bin目录(如图:)
如图进入java jdk的bin目录后,输入:
keytool -genkey -alias android.keystore -keyalg RSA -validity 20000 -keystore android.keystore -alias android.keystore 回车
然后接下来就是按照提示输入(如图:)
完成上面的步骤后, 该 .keystore文件就已经生成了, 在 bin目录下
说明: android.keystore 是.keystore文件的名称, 这里可以根据需要设定,
-validity 是该文件的有效期时间
密钥口令就是以后签名时需要填写的Password
注意: 如果生成不成功,可能是因为没有管理员权限,用需要以管理员身份生成.keystore文件
b.使用Eclipse 来生成一个keysotre文件
1. 在eclipse中 选择一个Android 工程, 然后"右键" -- Android Tools --Export Signed Application Package--Create new keystore
说明:上图的Location是要生成的keystore文件的位置,Password是keystore文件的密钥
2.接下来还是和命令提示符里一样的填写相关信息
注意: 一定要记住输入的密码
java中如何实现对文件和字符串加密. 解密?
DES 密钥生成,加解密方法,,你可以看一下
//DES 密钥生成工具
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
public class GenKey {
private static final String DES = "DES";
public static final String SKEY_NAME = "key.des";
public static void genKey1(String path) {
// 密钥
SecretKey skey = null;
// 密钥随机数生成
SecureRandom sr = new SecureRandom();
//生成密钥文件
File file = genFile(path);
try {
// 获取密钥生成实例
KeyGenerator gen = KeyGenerator.getInstance(DES);
// 初始化密钥生成器
gen.init(sr);
// 生成密钥
skey = gen.generateKey();
// System.out.println(skey);
ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream(file));
oos.writeObject(skey);
oos.close();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @param file : 生成密钥的路径
* SecretKeyFactory 方式生成des密钥
* */
public static void genKey2(String path) {
// 密钥随机数生成
SecureRandom sr = new SecureRandom();
// byte[] bytes = {11,12,44,99,76,45,1,8};
byte[] bytes = sr.generateSeed(20);
// 密钥
SecretKey skey = null;
//生成密钥文件路径
File file = genFile(path);
try {
//创建deskeyspec对象
DESKeySpec desKeySpec = new DESKeySpec(bytes,9);
//实例化des密钥工厂
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
//生成密钥对象
skey = keyFactory.generateSecret(desKeySpec);
//写出密钥对象
ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream(file));
oos.writeObject(skey);
oos.close();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static File genFile(String path) {
String temp = null;
File newFile = null;
if (path.endsWith("/") || path.endsWith("\\")) {
temp = path;
} else {
temp = path + "/";
}
File pathFile = new File(temp);
if (!pathFile.exists())
pathFile.mkdirs();
newFile = new File(temp+SKEY_NAME);
return newFile;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
genKey2("E:/a/aa/");
}
}
//DES加解密方法
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.SecretKey;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
*制卡文件加/解密 加密方式DES
*/
public class SecUtil {
public static final Log log = LogFactory.getLog(SecUtil.class);
/**
* 解密
*
* @param keyPath
* 密钥路径
* @param source
* 解密前文件
* @param dest
* 解密后文件
*/
public static void decrypt(String keyPath, String source, String dest) {
SecretKey key = null;
try {
ObjectInputStream keyFile = new ObjectInputStream(
// 读取加密密钥
new FileInputStream(keyPath));
key = (SecretKey) keyFile.readObject();
keyFile.close();
} catch (FileNotFoundException ey1) {
log.info("Error when read keyFile");
throw new RuntimeException(ey1);
} catch (Exception ey2) {
log.info("error when read the keyFile");
throw new RuntimeException(ey2);
}
// 用key产生Cipher
Cipher cipher = null;
try {
// 设置算法,应该与加密时的设置一样
cipher = Cipher.getInstance("DES");
// 设置解密模式
cipher.init(Cipher.DECRYPT_MODE, key);
} catch (Exception ey3) {
log.info("Error when create the cipher");
throw new RuntimeException(ey3);
}
// 取得要解密的文件并解密
File file = new File(source);
String filename = file.getName();
try {
// 输出流,请注意文件名称的获取
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream(dest));
// 输入流
CipherInputStream in = new CipherInputStream(
new BufferedInputStream(new FileInputStream(file)), cipher);
int thebyte = 0;
while ((thebyte = in.read()) != -1) {
out.write(thebyte);
}
in.close();
out.close();
} catch (Exception ey5) {
log.info("Error when encrypt the file");
throw new RuntimeException(ey5);
}
}
/**
* 加密
* @param keyPath 密钥路径
* @param source 加密前文件
* @param dest 加密后文件
*/
public static void encrypt(String keyPath, String source, String dest) {
SecretKey key = null;
try {
ObjectInputStream keyFile = new ObjectInputStream(
// 读取加密密钥
new FileInputStream(keyPath));
key = (SecretKey) keyFile.readObject();
keyFile.close();
} catch (FileNotFoundException ey1) {
log.info("Error when read keyFile");
throw new RuntimeException(ey1);
} catch (Exception ey2) {
log.info("error when read the keyFile");
throw new RuntimeException(ey2);
}
// 用key产生Cipher
Cipher cipher = null;
try {
// 设置算法,应该与加密时的设置一样
cipher = Cipher.getInstance("DES");
// 设置解密模式
cipher.init(Cipher.ENCRYPT_MODE, key);
} catch (Exception ey3) {
log.info("Error when create the cipher");
throw new RuntimeException(ey3);
}
// 取得要解密的文件并解密
File file = new File(source);
String filename = file.getName();
try {
// 输出流,请注意文件名称的获取
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream(dest));
// 输入流
CipherInputStream in = new CipherInputStream(
new BufferedInputStream(new FileInputStream(file)), cipher);
int thebyte = 0;
while ((thebyte = in.read()) != -1) {
out.write(thebyte);
}
in.close();
out.close();
} catch (Exception ey5) {
log.info("Error when encrypt the file");
throw new RuntimeException(ey5);
}
}
}
如何在由openssl生成的java中使用.key和.crt文件
您好,这样:
第一步,从key和crt生成pkcs12格式的keystore
openssl pkcs12 -export -in mycert.crt -inkey mykey.key
-out mycert.p12 -name tomcat -CAfile myCA.crt
-caname root -chain
第二步 生成tomcat需要的keystore
keytool -importkeystore -v -srckeystore mycert.p12 -srcstoretype pkcs12 -srcstorepass 123456 -destkeystore tomcat.keystore -deststoretype jks -deststorepass 123456 。
.key文件java的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于key文件如何编辑、.key文件java的信息别忘了在本站进行查找喔。