BouncyCastle 密钥转换 - Java
转自: https://blog.csdn.net/a351945755/article/details/63707040
1. PKCS#8 转 PKCS#1
You will need BouncyCastle:
import org.bouncycastle.asn1.ASN1Encodable;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
import org.bouncycastle.util.io.pem.PemObject;
import org.bouncycastle.util.io.pem.PemWriter;
The code snippets below have been checked and found working with Bouncy Castle 1.52.
Private key
Convert private key from PKCS8 to PKCS1:
PrivateKey priv = pair.getPrivate();
byte[] privBytes = priv.getEncoded(); PrivateKeyInfo pkInfo = PrivateKeyInfo.getInstance(privBytes);
ASN1Encodable encodable = pkInfo.parsePrivateKey();
ASN1Primitive primitive = encodable.toASN1Primitive();
byte[] privateKeyPKCS1 = primitive.getEncoded(); ====> //pkcs8Bytes contains PKCS#8 DER-encoded key as a byte[]
PrivateKeyInfo pki = PrivateKeyInfo.getInstance(encodeByte);
RSAPrivateKeyStructure pkcs1Key = RSAPrivateKeyStructure.getInstance(pki.getPrivateKey());
byte[] pkcs1Bytes = pkcs1Key.getEncoded();//etc.
Convert private key in PKCS1 to PEM:
PemObject pemObject = new PemObject("RSA PRIVATE KEY", privateKeyPKCS1);
StringWriter stringWriter = new StringWriter();
PemWriter pemWriter = new PemWriter(stringWriter);
pemWriter.writeObject(pemObject);
pemWriter.close();
String pemString = stringWriter.toString();
Check with command line OpenSSL that the key format is as expected:
openssl rsa -in rsa_private_key.pem -noout -text
2. PKCS#1 转 PKCS#8
//format PKCS#1 to PKCS#8
public static String formatPkcs1ToPkcs8(String rawKey) throws Exception {
String result = null;
//extract valid key content
String validKey = RsaPemUtil.extractFromPem(rawKey);
if(!Strings.isNullOrEmpty(validKey)) {
//将BASE64编码的私钥字符串进行解码
byte[] encodeByte = Base64.decodeBase64(validKey);
AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PKCSObjectIdentifiers.pkcs8ShroudedKeyBag); //PKCSObjectIdentifiers.pkcs8ShroudedKeyBag
ASN1Object asn1Object = ASN1Object.fromByteArray(encodeByte);
PrivateKeyInfo privKeyInfo = new PrivateKeyInfo(algorithmIdentifier, asn1Object);
byte[] pkcs8Bytes = privKeyInfo.getEncoded(); String type = "PRIVATE KEY";
result = format2PemString(type, pkcs8Bytes);
}
return result;
}
Notice:
生成出来的代码与openssl转换的还是有点不一样,主要是第一行。可能是有些属性字段不同,但经测试上述代码转换产生的key也可以解码。
3. 完整代码
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.Security;
import java.security.interfaces.RSAPrivateCrtKey;
import java.security.spec.EncodedKeySpec;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.RSAPrivateKeySpec;
import java.util.List; import org.bouncycastle.asn1.ASN1Encodable;
import org.bouncycastle.asn1.ASN1Object;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.asn1.pkcs.RSAPrivateKeyStructure;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.util.io.pem.PemObject;
import org.bouncycastle.util.io.pem.PemReader;
import org.bouncycastle.util.io.pem.PemWriter;
import org.bouncycastle.openssl.MiscPEMGenerator;
import org.bouncycastle.openssl.PKCS8Generator; import com.ctrip.lzyan.test.cipher.cscm.RsaPemUtil;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import org.apache.commons.codec.binary.Base64; /**
* Transform PKCS format
* PKCS#1 -> PKCS#8
* PKCS#8 -> PKCS#1
*
*/
public class RsaPkcsTransformer {
private static final String COMMENT_BEGIN_FLAG = "-----";
private static final String RETURN_FLAG_R = "\r";
private static final String RETURN_FLAG_N = "\n"; //format PKCS#8 to PKCS#1
public static String formatPkcs8ToPkcs1(String rawKey) throws Exception {
String result = null;
//extract valid key content
String validKey = RsaPemUtil.extractFromPem(rawKey);
if(!Strings.isNullOrEmpty(validKey)) {
//将BASE64编码的私钥字符串进行解码
byte[] encodeByte = Base64.decodeBase64(validKey); //==========
//pkcs8Bytes contains PKCS#8 DER-encoded key as a byte[]
PrivateKeyInfo pki = PrivateKeyInfo.getInstance(encodeByte);
RSAPrivateKeyStructure pkcs1Key = RSAPrivateKeyStructure.getInstance(pki.getPrivateKey());
byte[] pkcs1Bytes = pkcs1Key.getEncoded();//etc.
//========== String type = "RSA PRIVATE KEY";
result = format2PemString(type, pkcs1Bytes);
}
return result;
} //format PKCS#1 to PKCS#8
public static String formatPkcs1ToPkcs8(String rawKey) throws Exception {
String result = null;
//extract valid key content
String validKey = RsaPemUtil.extractFromPem(rawKey);
if(!Strings.isNullOrEmpty(validKey)) {
//将BASE64编码的私钥字符串进行解码
byte[] encodeByte = Base64.decodeBase64(validKey); AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PKCSObjectIdentifiers.pkcs8ShroudedKeyBag); //PKCSObjectIdentifiers.pkcs8ShroudedKeyBag
ASN1Object asn1Object = ASN1Object.fromByteArray(encodeByte);
PrivateKeyInfo privKeyInfo = new PrivateKeyInfo(algorithmIdentifier, asn1Object);
byte[] pkcs8Bytes = privKeyInfo.getEncoded(); String type = "PRIVATE KEY";
result = format2PemString(type, pkcs8Bytes); }
return result;
} // Write to pem file
private static String format2PemString(String type, byte[] privateKeyPKCS1) throws Exception {
PemObject pemObject = new PemObject(type, privateKeyPKCS1);
StringWriter stringWriter = new StringWriter();
PemWriter pemWriter = new PemWriter(stringWriter);
pemWriter.writeObject(pemObject);
pemWriter.close();
String pemString = stringWriter.toString();
return pemString;
} //=== Testing ===
public static void main(String[] args) throws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bwIveGTxkXqxTe3AkEA1gzPz9IAzFPx8RjkOxgRag3g+Rp+35nuLw18Rt04EODinurCsg3t5k2rOx2Fic6GVozqM3B3p4ATr4GsyhALLwJAFi8vp/47d7p+FpES7e1kgdFNF9muxes3oyrB3Adjcsb8w/ZcTJ5Zjf4vgg7jExdxHbxYoOqAwllcH8jsmZaMAwJAXdJjzF3qR6WL6PfWIijciTXoJIf+kJfyFOG+VXllt9A6xRl1mYINguMMaO75t3u02n8NsNpdOCgleMpIKJF7tQJBALsDYb59npdnKL2V0zk5bZ7Nd40dy4KbRcXIG2Ll/Lo6CeY/6Jes868R0yyuyLGP5CcayMKVAhcVk3xEWI7dluU="; String formatKey = formatPkcs1ToPkcs8(rawKey_pkcs1);
//String formatKey = formatPkcs8ToPkcs1(rawKey_pkcs8);
System.out.println(formatKey); //expect: abcdef
} }
RsaPkcsTransformer
package com.ctrip.lzyan.test.element.rsa.sample4; import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;
import javax.crypto.Cipher;
import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.pkcs.RSAPrivateKeyStructure; /**
*
* RSA+Base64加密解密工具类 RSA 一般用在数据传输过程中的加密和解密 先用RSA加密后是字节数组 再用BASE64加密 成字符串进行传输 测试
* RSA1024产生的公钥字节长度在160-170之间 私钥长度在630-640之间 经过base64加密后长度 公钥字节产度在210-220之间
* 私钥长度在840-850之间 所以数据库设计时如果存公钥长度设计varchar(256) 私钥长度varchar(1024)
*
*/
public abstract class LzyanRSAUtilTest2 {
public static final String KEY_ALGORITHM = "RSA";
private static final String PUBLIC_KEY = "rsa_public_key";
private static final String PRIVATE_KEY = "rsa_private_key";
private static final String ENCODING = "UTF-8"; /**
*
* 加密
* 用公钥加密
* @param content
* @param base64PublicKeyStr
* @return
* @throws Exception
*/
public static String encryptByPublicKey(String content, String base64PublicKeyStr) throws Exception {
byte[] inputBytes = content.getBytes(ENCODING);
byte[] outputBytes = encryptByPublicKey(inputBytes, base64PublicKeyStr);
return Base64.encodeBase64String(outputBytes);
} /**
*
* 加密
*
* 用私钥加密
* @param content
* @param base64PrivateKeyStr
* @return
* @throws Exception
*/
public static String encryptByPrivateKey(String content, String base64PrivateKeyStr) throws Exception {
byte[] inputBytes = content.getBytes(ENCODING);
byte[] outputBytes = encryptByPrivateKey(inputBytes, base64PrivateKeyStr);
return Base64.encodeBase64String(outputBytes);
} /**
*
* 解密
* 用公钥解密
* @param content
* @param base64PublicKeyStr
* @return
* @throws Exception
*/
public static String decryptByPublicKey(String content, String base64PublicKeyStr) throws Exception {
byte[] inputBytes = Base64.decodeBase64(content);
byte[] outputBytes = decryptByPublicKey(inputBytes, base64PublicKeyStr);
return new String(outputBytes, ENCODING);
} /**
*
* 解密
* 用私钥解密
* @param content
* @param privateKeyStr
* @return
* @throws Exception
*/
public static String decryptByPrivateKey(String content, String base64PrivateKeyStr) throws Exception {
byte[] inputBytes = Base64.decodeBase64(content);
byte[] outputBytes = decryptByPrivateKey(inputBytes, base64PrivateKeyStr);
return new String(outputBytes, ENCODING); } /**
*
* 加密
* 用公钥加密
* @param content
* @param base64PublicKeyStr
* @return
* @throws Exception
*/
public static byte[] encryptByPublicKey(byte[] content, String base64PublicKeyStr) throws Exception {
// 对公钥解密
byte[] publicKeyBytes = Base64.decodeBase64(base64PublicKeyStr); // 取得公钥
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(publicKeyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
Key publicKey = keyFactory.generatePublic(x509KeySpec); // 对数据加密
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, publicKey); return cipher.doFinal(content); } /**
*
* 加密
* 用私钥加密
* @param content
* @param base64PrivateKeyStr
* @return
* @throws Exception
*/
public static byte[] encryptByPrivateKey(byte[] content, String base64PrivateKeyStr) throws Exception {
// 对密钥解密
byte[] privateKeyBytes = Base64.decodeBase64(base64PrivateKeyStr); // 取得私钥
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
Key privateKey = keyFactory.generatePrivate(pkcs8KeySpec); // 对数据加密
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, privateKey); return cipher.doFinal(content); } /**
*
* 解密
* 用公钥解密
* @param content
* @param base64PublicKeyStr
* @return
* @throws Exception
*/
public static byte[] decryptByPublicKey(byte[] content, String base64PublicKeyStr) throws Exception {
// 对密钥解密
byte[] publicKeyBytes = Base64.decodeBase64(base64PublicKeyStr); // 取得公钥
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(publicKeyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
Key publicKey = keyFactory.generatePublic(x509KeySpec); // 对数据解密
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, publicKey); return cipher.doFinal(content); } /**
* 解密
* 用私钥解密
* @param content
* @param privateKeyStr
* @return
* @throws Exception
*/
public static byte[] decryptByPrivateKey(byte[] content, String base64PrivateKeyStr) throws Exception {
// 对密钥解密
byte[] privateKeyBytes = Base64.decodeBase64(base64PrivateKeyStr); // 取得私钥 for PKCS#1
// RSAPrivateKeyStructure asn1PrivKey = new RSAPrivateKeyStructure((ASN1Sequence) ASN1Sequence.fromByteArray(privateKeyBytes));
// RSAPrivateKeySpec rsaPrivKeySpec = new RSAPrivateKeySpec(asn1PrivKey.getModulus(), asn1PrivKey.getPrivateExponent());
// KeyFactory keyFactory= KeyFactory.getInstance("RSA");
// PrivateKey priKey= keyFactory.generatePrivate(rsaPrivKeySpec); // 取得私钥 for PKCS#8
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
Key priKey = keyFactory.generatePrivate(pkcs8KeySpec); // 对数据解密
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, priKey);
return cipher.doFinal(content);
} /**
*
* 取得私钥
* @param keyMap
* @return
* @throws Exception
*/
public static String getBase64PrivateKeyStr(Map keyMap) throws Exception {
Key key = (Key) keyMap.get(PRIVATE_KEY);
return Base64.encodeBase64String(key.getEncoded());
} /**
*
* 取得公钥
*
* @param keyMap
* @return
* @throws Exception
*/
public static String getBase64PublicKeyStr(Map keyMap) throws Exception {
Key key = (Key) keyMap.get(PUBLIC_KEY);
return Base64.encodeBase64String(key.getEncoded());
} /**
*
* 初始化密钥
* @return
* @throws Exception
*/
public static Map initKey() throws Exception {
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);
keyPairGen.initialize(1024); // 初始化RSA1024安全些
KeyPair keyPair = keyPairGen.generateKeyPair();
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); // 公钥
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); // 私钥
Map keyMap = new HashMap(2);
keyMap.put(PUBLIC_KEY, publicKey);
keyMap.put(PRIVATE_KEY, privateKey);
return keyMap;
} private static void test() throws Exception {
String pubKey =
"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDgDxka1U8SWI1vBY7pA1UiCVnr" +
"dtSgE+PpyTqe2YSEWCgkYQ2YsohZwsaUao7nya7QnBgRiPKEHgS/Eey+L9iwo32S" +
"n5fUwb0nJ1+JeXRA6JsDEKpONJojIbF2nfgHLWsNn4bzn5Webc6WZLx0GyLTQuZG" +
"adFVuVq2dQqEsrq7HwIDAQAB"; String str = "123456";
String content = encryptByPublicKey(str, pubKey);
System.out.println(content); String priKey_pkcs1 = "MIICXQIBAAKBgQDgDxka1U8SWI1vBY7pA1UiCVnrdtSgE+PpyTqe2YSEWCgkYQ2YsohZwsaUao7nya7QnBgRiPKEHgS/Eey+L9iwo32Sn5fUwb0nJ1+JeXRA6JsDEKpONJojIbF2nfgHLWsNn4bzn5Webc6WZLx0GyLTQuZGadFVuVq2dQqEsrq7HwIDAQABAoGAYtaGLo4WWXNywJzlE+kCbwdNAU/kL9FWYtT/5P7zNCZnXtTpWIi5GU+QpfvzmlAfq6qP+3w77wgG8/qGQsd8gGu3mydi0ImmD9sJdhhsJuWZhCMM+qmvSmvG/gvIr+bdEmPhpCQpa3BLveUkFDA/OnwfTVL6ruwZayMzuToB6WECQQD63Gx9DZVhYoSxR7qSmiGf/TjfOJusTcrmc27Z5X5MS36a3Ux9Z+c9EaYFldZ3cPzP521ugNVvZdovKqFKIcQ5AkEA5KYeKBVlkrLaamSEu5WAX3DqJ6iDRqEjzMoVad5B1I7kJHO+NijUxNHaWaSqLOHuk37X+EAjSTozzwOkKwbqFwJBAM1NhhAWBNHtcdEwddWzBJ/N+jRdPLIX/Fz7zZXQRruj8VpGkGn1lf6ZqfjaNuoLcyunKB0OnR6NCbIePl/QIKkCQGgEQjfN9BVWlBJOhCuqCWphvcQo3v+kktq5HCC7YYtHLfZ/SQrubEzVgtXBGUGtzpD+5VUkKGlJtwP4Dhkc3iUCQQCwiFKuQe/OdlkYk1L4mb0H0fzy+/6mYxyUqpTXUw/6BVDOyowvzieh9oh2ZhnQS7YPBWz5ZXzwUH4YVwGqxiwA";
String priKey_pkcs8_openssl = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAOAPGRrVTxJYjW8FjukDVSIJWet21KAT4+nJOp7ZhIRYKCRhDZiyiFnCxpRqjufJrtCcGBGI8oQeBL8R7L4v2LCjfZKfl9TBvScnX4l5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JClrcEu95SQUMD86fB9NUvqu7BlrIzO5OgHpYQJBAPrcbH0NlWFihLFHupKaIZ/9ON84m6xNyuZzbtnlfkxLfprdTH1n5z0RpgWV1ndw/M/nbW6A1W9l2i8qoUohxDkCQQDkph4oFWWSstpqZIS7lYBfcOonqINGoSPMyhVp3kHUjuQkc742KNTE0dpZpKos4e6Tftf4QCNJOjPPA6QrBuoXAkEAzU2GEBYE0e1x0TB11bMEn836NF08shf8XPvNldBGu6PxWkaQafWV/pmp+No26gtzK6coHQ6dHo0Jsh4+X9AgqQJAaARCN830FVaUEk6EK6oJamG9xCje/6SS2rkcILthi0ct9n9JCu5sTNWC1cEZQa3OkP7lVSQoaUm3A/gOGRzeJQJBALCIUq5B7852WRiTUviZvQfR/PL7/qZjHJSqlNdTD/oFUM7KjC/OJ6H2iHZmGdBLtg8FbPllfPBQfhhXAarGLAA=";
String priKey = priKey_pkcs8_new; String output = decryptByPrivateKey(content, priKey);
System.out.println(output);
} // === Testing ===
public static void main(String[] args) throws Exception {
test();
} }
LzyanRSAUtilTest2
4. Java中使用PKCS#1秘钥
要引入一个bouncycastle包(bcprov-jdk16),代码多几行。
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk16</artifactId>
<version>1.46</version>
</dependency>
// 取得私钥 for PKCS#1
RSAPrivateKeyStructure asn1PrivKey = new RSAPrivateKeyStructure((ASN1Sequence) ASN1Sequence.fromByteArray(privateKeyBytes));
RSAPrivateKeySpec rsaPrivKeySpec = new RSAPrivateKeySpec(asn1PrivKey.getModulus(), asn1PrivKey.getPrivateExponent());
KeyFactory keyFactory= KeyFactory.getInstance("RSA");
PrivateKey priKey= keyFactory.generatePrivate(rsaPrivKeySpec); // 取得私钥 for PKCS#8
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
Key priKey = keyFactory.generatePrivate(pkcs8KeySpec);
BouncyCastle 密钥转换 - Java的更多相关文章
- (转)C#与Java的证书密钥转换
前言 最近由于项目需求,服务端由c#编写,客户端由java编写.通信数据使用RSA非对称加密.但是java和c#生成的密钥格式是不一样的,所以需要转换格式才可以正常使用.网上搜到使用java进行格式转 ...
- Rsa加解密Java、C#、php通用代码 密钥转换工具
之前发了一篇"TripleDes的加解密Java.C#.php通用代码",后面又有项目用到了Rsa加解密,还是在不同系统之间进行交互,Rsa在不同语言的密钥格式不一样,所以过程中主 ...
- RSA的密钥把JAVA格式转换成C#的格式
RSA算法在C#与JAVA之前的交互 在JAVA生成一对RSA私钥和公钥的时候,是以下的形式给到C#去调用: string publickey = @"MIGfMA0GCSqGSIb4DQE ...
- RSA的密钥把JAVA格式转换成C#的格式(2)
把C#格式转换成Java:RSA的密钥把JAVA格式转换成C#的格式(1) 我已经在第一篇介绍过如何把C#格式转换成Java,现在来看看如何把Java格式转换成C#. /// <summary& ...
- Atitit 跨平台异常处理(2)--------异常转换 -----java c# js异常对象结构比较and转换
Atitit 跨平台异常处理(2)--------异常转换 -----java c# js异常对象结构比较and转换 { "@type":"java.lang.Runti ...
- paip.c++ 转换 java 解决方案
paip.c++ 转换 java 解决方案 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.net/a ...
- 转换java keytools的keystore证书到OPENSSL的PEM格式文件
背景:原先业务使用的前端为haproxy,直接端口转发至tomcat,后端进行ssl连接,所以当时生成的步骤如下 ? 1 2 •生成密钥对:keytool -genkey -alias tomcat- ...
- 安卓、IOS端AEC密钥加密 Java端密钥解密通用实现(16进制表现形式)
由于业务需求,需要实现在客户端对重要信息进行加密,在服务端进行解密.客户端包括IOS和安卓的 服务端位Java. 注意密钥 需要保持一致,可以自己定义 . 安卓端加密代码: ============= ...
- ffmpeg视频格式转换(Java)
命令: 高品质: ffmpeg -i E:\input\a.wmv -ab 128 -acodec libmp3lame -ac 1 -ar 22050 -r 29.97 -qscale 4 -y E ...
随机推荐
- spring aop 之annotation
1.CutPointInterface public interface CutPointInterface { void method(); } 2.CutPointClass @Component ...
- Nginx 基本命令
基础命令 1.启动 直接点击Nginx目录下的nginx.exe 或者 cmd运行start nginx 2.关闭 nginx -s stop 或者 nginx -s quit stop表示立即停止n ...
- table给tbody设置滚动条
table结构例子: <table class="layui-table"> <thead> <tr> <th> 贷款项目 < ...
- Mac 安装 Jenkins
Mac 安装 Jenkins 有两种方法 方法一: 从官方下载最新版本:http://mirrors.jenkins-ci.org/osx/latest 点击安装. 方法二(推荐): 使用 homeb ...
- PowerDesigner 16PDM显示备注
默认显示的列是Name及类型 name和code不支持同时显示,设置方法: Tool->Model Options->Name Convention->右侧display中选择显示n ...
- [PyData] 01 - Web Crawler
前言 一.总体策略 一些常见抓取数据的例子.三步走: 抓取数据并存储 <---- 数据处理 数据展示 二.学习资源 首先,通过Beautiful Soup抓取数据 from http://www ...
- 【代码审计】XYHCMS V3.5任意文件删除漏洞分析
0x00 环境准备 XYHCMS官网:http://www.xyhcms.com/ 网站源码版本:XYHCMS V3.5(2017-12-04 更新) 程序源码下载:http://www.xyhc ...
- 使用LevelListDrawable实现Html.fromHtml多张图片显示
stackoverflow网站果然强大,帮了我不少忙! http://stackoverflow.com/questions/16179285/html-imagegetter-textview 首先 ...
- TensorFlow MNIST 问题解决
TensorFlow MNIST 问题解决 一.数据集下载错误 错误:IOError: [Errno socket error] [Errno 101] Network is unreachable ...
- MVC的ViewData自动给Razor写的input赋值
问题: 写编辑的时候,突然发现,没有值的model,突然出现了值,而且值是ViewData中值. 后台: this.ViewData["test"] = "测试" ...