A. 关于j2me提取der证书中的公钥问题
什么异常啊
用数组来读取
调试一下把,这样能具体看是哪行代码出错了
B. 后缀名cer和crt的不同之处,为什么申请证明书之后要把后缀名从cer改为crt
.CRT = 扩展名CRT用于证书。证书可以是DER编码,也可以是PEM编码。扩展名CER和CRT几乎是同义词。内这种情容况在各种unix/linux系统中很常见。
CER = CRT证书的微软型式。可以用微软的工具把CRT文件转换为CER文件(CRT和CER必须是相同编码的,DER或者PEM)。扩展名为CER的文件可以被IE识别并作为命令调用微软的cryptoAPI(具体点就是rudll32.exe
(2)der证书扩展阅读
Windows中的证书扩展名有好几种,比如.cer和.crt。通常而言,.cer文件是二进制数据,而.crt文件包含的是ASCII数据。
cer文件包含依据DER(Distinguished Encoding Rules)规则编码的证书数据,这是x.690标准中指定的编码格式。
X.509是一个最基本的公钥格式标准,里面规定了证书需要包含的各种信息。通常我们提到的证书,都是这个格式的,里面包含了公钥、发布者的数字签名、有效期等内容。要强调的是,它只里面是不包含私钥的。相关的格式有:DER、PEM、CER、CRT。
C. 如何查看证书的16进制der编码,及证书的各个域der格式
证书一般都是x.509格式的证书,然后经过DER编码,DER是TLV编码,然后再经过base64编码后存储的。
正确的方法,应该是,把证书文件,用binary方式,传送到linux下,然后用linux中的base64来进行文件解 码。
命令如下:base64
-d -i ca.crt > crt.hex
-d的命令是,然后-i是--ignore-garbage
When decoding, ignore non-alphabet characters.
Decoding
require compliant input by default, use --ignore-garbage to
attempt to
recover from non-alphabet characters (such as newlines) in
the encoded
stream.
然后再用vim打开crt.hex,这时候再转换成16进制,就可以查看到正常的证书16进制的DER编码了。
D. php读取der证书出现乱码怎么办
直接读取当然是乱码了,der密钥证书本来就是二进制编码或者BASE64编码的文件,php可以用openssl_x509_parse函数来解析:
<?php
$cert = file_get_contents('filename.crt');
$ssl = openssl_x509_parse($cert);
var_mp($ssl);
?>
E. ios开发 https导入的是cer还是der证书
在 Mac OS 上将 iPhone 开发人员证书转换为 P12 文件 从 Apple 下载 Apple iPhone 证书后,将其导出为 P12 证书格式。在 Mac? OS 上执行以下操作: 打开钥匙串访问应用程序(位于应用程序/实用工具文件夹中)。 如果尚未将该证书添加到钥匙串,请选择“文件”>“导入”。然后浏览到您从 Apple 获取的证书文件(.cer 文件)。 在钥匙串访问中选择密钥类别。 选择与 iPhone 开发证书相关联的私钥。 该私钥由 iPhone 开发人员识别:与之配对的<名字><姓氏>公共证书。 选择“文件”>“导出项目”。 以个人信息交换 (.p12) 文件格式保存您的密钥。 系统将提示您创建一个尝试在其他计算机上导入此密钥时需要使用的密码。 在 Windows 上将 Apple 开发人员证书转换为 P12 文件 要使用 Flash CS5 开发 iPhone 应用程序,则必须使用 P12 证书文件。基于从 Apple 收到的 Apple iPhone 开发人员证书文件生成此证书。 将从 Apple 收到的开发人员证书文件转换成 PEM 证书文件。从 OpenSSL bin 目录运行以下命令行语句: openssl x509 -in developer_identity.cer -inform DER -out developer_identity.pem -outform PEM 如果您使用的是 Mac 计算机上钥匙串中的私钥,则将其转换成 PEM 密钥: openssl pkcs12 -nocerts -in mykey.p12 -out mykey.pem 现在,您可以基于密钥和 PEM 版本的 iPhone 开发人员证书生成有效的 P12 文件: openssl pkcs12 -export -inkey mykey.key -in developer_identity.pem -out iphone_dev.p12 如果您使用的是 Mac OS 钥匙串中的密钥,则使用上一步骤中生成的 PEM 版本。否则,请使用以前生成的 OpenSSL 密钥(位于 Windows 上)。
F. 火狐导出证书文件时,保存类型怎么选,[*.crt]和[*.der]和[*.p7c]有什么区别,是否"含链"有什么影响
您好!很高兴为您答疑。
关于这些证书区别的详细说明可参考此文档:证书格式版区别。而实际使用权中,则完全取决于您所部署的环境而定,不同的证书类型适用于不同的部署方式,其没有一定的模式或固定的要求。简单地说,就是完全取决于您的需要,关于格式可以不必过于纠结。
如果对我们的回答存在任何疑问,欢迎继续问询。
G. 如何制作cer证书
cer证书是二进制编码的,所以获取到它的内容后做次转码就可以啦。
cer证书由p12导出(只不过cer是二进制,p12是base64,可以网上查der(二进制)转pem(base64)),导出命令:
keytool -export -alias p12client -keystore dlt.p12 -storetype PKCS12 -storepass 密码 -rfc -file p12.cer
读取cer公钥如下所示:
[css] view plain
function pem2der($pem_data) {
$begin = "KEY-----";
$end = "-----END";
$pem_data = substr($pem_data, strpos($pem_data, $begin)+strlen($begin));
$pem_data = substr($pem_data, 0, strpos($pem_data, $end));
$der = base64_decode($pem_data);
return $der;
}
function der2pem($der_data) {
$pem = chunk_split(base64_encode($der_data), 64, "\n");
$pem = "-----BEGIN PUBLIC KEY-----\n".$pem."-----END PUBLIC KEY-----\n";
return $pem;
}
$file = "898000000000001.cer";
$fd = fopen($file, 'r');
$p12buf = fread($fd, filesize($file));
var_mp(der2pem($p12buf));-----------------------:这里调用 der2pem 、pem2der 都可以,两种方式。
H. 我怎样才能获得SecKeyRef从DER / PEM文件
最近几天折腾了一下如何在iOS上使用RSA来加密。iOS上并没有直接的RSA加密API。但是iOS提供了x509的API,而x509是支持RSA加密的。因此,我们可以通过制作自签名的x509证书(由于对安全性要求不高,我们并不需要使用CA认证的证书),再调用x509的相关API来进行加密。接下来记录一下整个流程。
第一步,制作自签名的证书
1.最简单快捷的方法,打开Terminal,使用openssl(Mac OS X自带)生成私钥和自签名的x509证书。
openssl req -x509 -out public_key.der -outform der -new -newkey rsa:1024 -keyout private_key.pem -days 3650
按照命令行的提示输入内容就行了。
几个说明:
public_key.der是输出的自签名的x509证书,即我们要用的。
private_key.pem是输出的私钥,用来解密的,请妥善保管。
rsa:1024这里的1024是密钥长度,1024是比较安全的,如果需要更安全的话,可以用2048,但是加解密代价也会增加。
-days:证书过期时间,一定要加上这个参数,默认的证书过期时间是30天,一般我们不希望证书这么短就过期,所以写上比较合适的天数,例如这里的3650(10年)。
事实上,这一行命令包含了好几个步骤(我研究下面这些步骤的原因是我手头已经由一个private_key.pem私钥了,想直接用这个来生成x509证书,也就是用到了下面的2-3)
1)创建私钥
openssl genrsa -out private_key.pem 1024
2)创建证书请求(按照提示输入信息)
openssl req -new -out cert.csr -key private_key.pem
3)自签署根证书
openssl x509 -req -in cert.csr -out public_key.der -outform der -signkey private_key.pem -days 3650
2.验证证书。把public_key.der拖到xcode中,如果文件没有问题的话,那么就可以直接在xcode中打开,看到证书的各种信息。
第二步,使用public_key.der来进行加密。
1.导入Security.framework。
2.把public_key.der放到mainBundle中(一般直接拖到Xcode就行啦)。
3.从public_key.der读取公钥。
4.加密。
下面是参考代码(只能用于加密长度小于等于116字节的内容,适合于对密码进行加密。使用了ARC,不过还是要注意部分资源需要使用CFRealse来释放)
RSA.h
//
// RSA.h
//
#import <Foundation/Foundation.h>
@interface RSA : NSObject {
SecKeyRef publicKey;
SecCertificateRef certificate;
SecPolicyRef policy;
SecTrustRef trust;
size_t maxPlainLen;
}
- (NSData *) encryptWithData:(NSData *)content;
- (NSData *) encryptWithString:(NSString *)content;
@end
RSA.m
//
// RSA.m
//
#import "RSA.h"
@implementation RSA
- (id)init {
self = [super init];
NSString *publicKeyPath = [[NSBundle mainBundle] pathForResource:@"public_key"
ofType:@"der"];
if (publicKeyPath == nil) {
NSLog(@"Can not find pub.der");
return nil;
}
NSDate *publicKeyFileContent = [NSData dataWithContentsOfFile:publicKeyPath];
if (publicKeyFileContent == nil) {
NSLog(@"Can not read from pub.der");
return nil;
}
certificate = SecCertificateCreateWithData(kCFAllocatorDefault, ( __bridge CFDataRef)publicKeyFileContent);
if (certificate == nil) {
NSLog(@"Can not read certificate from pub.der");
return nil;
}
policy = SecPolicyCreateBasicX509();
OSStatus returnCode = (certificate, policy, &trust);
if (returnCode != 0) {
NSLog(@" fail. Error Code: %ld", returnCode);
return nil;
}
SecTrustResultType trustResultType;
returnCode = SecTrustEvaluate(trust, &trustResultType);
if (returnCode != 0) {
NSLog(@"SecTrustEvaluate fail. Error Code: %ld", returnCode);
return nil;
}
publicKey = SecTrustCopyPublicKey(trust);
if (publicKey == nil) {
NSLog(@"SecTrustCopyPublicKey fail");
return nil;
}
maxPlainLen = SecKeyGetBlockSize(publicKey) - 12;
return self;
}
- (NSData *) encryptWithData:(NSData *)content {
size_t plainLen = [content length];
if (plainLen > maxPlainLen) {
NSLog(@"content(%ld) is too long, must < %ld", plainLen, maxPlainLen);
return nil;
}
void *plain = malloc(plainLen);
[content getBytes:plain
length:plainLen];
size_t cipherLen = 128; // 当前RSA的密钥长度是128字节
void *cipher = malloc(cipherLen);
OSStatus returnCode = SecKeyEncrypt(publicKey, kSecPaddingPKCS1, plain,
plainLen, cipher, &cipherLen);
NSData *result = nil;
if (returnCode != 0) {
NSLog(@"SecKeyEncrypt fail. Error Code: %ld", returnCode);
}
else {
result = [NSData dataWithBytes:cipher
length:cipherLen];
}
free(plain);
free(cipher);
return result;
}
- (NSData *) encryptWithString:(NSString *)content {
return [self encryptWithData:[content dataUsingEncoding:NSUTF8StringEncoding]];
}
- (void)dealloc{
CFRelease(certificate);
CFRelease(trust);
CFRelease(policy);
CFRelease(publicKey);
}
@end
使用方法:
RSA *rsa = [[RSA alloc] init];
if (rsa != nil) {
NSLog(@"%@",[rsa encryptWithString:@"test"]);
}
else {
NSLog(@"init rsa error");
}
RSA.h
//
// RSA.h
//
#import <Foundation/Foundation.h>
@interface RSA : NSObject {
SecKeyRef publicKey;
SecCertificateRef certificate;
SecPolicyRef policy;
SecTrustRef trust;
size_t maxPlainLen;
}
- (NSData *) encryptWithData:(NSData *)content;
- (NSData *) encryptWithString:(NSString *)content;
@end
RSA.m
//
// RSA.m
//
#import "RSA.h"
@implementation RSA
- (id)init {
self = [super init];
NSString *publicKeyPath = [[NSBundle mainBundle] pathForResource:@"public_key"
ofType:@"der"];
if (publicKeyPath == nil) {
NSLog(@"Can not find pub.der");
return nil;
}
NSDate *publicKeyFileContent = [NSData dataWithContentsOfFile:publicKeyPath];
if (publicKeyFileContent == nil) {
NSLog(@"Can not read from pub.der");
return nil;
}
certificate = SecCertificateCreateWithData(kCFAllocatorDefault, ( __bridge CFDataRef)publicKeyFileContent);
if (certificate == nil) {
NSLog(@"Can not read certificate from pub.der");
return nil;
}
policy = SecPolicyCreateBasicX509();
OSStatus returnCode = (certificate, policy, &trust);
if (returnCode != 0) {
NSLog(@" fail. Error Code: %ld", returnCode);
return nil;
}
SecTrustResultType trustResultType;
returnCode = SecTrustEvaluate(trust, &trustResultType);
if (returnCode != 0) {
NSLog(@"SecTrustEvaluate fail. Error Code: %ld", returnCode);
return nil;
}
publicKey = SecTrustCopyPublicKey(trust);
if (publicKey == nil) {
NSLog(@"SecTrustCopyPublicKey fail");
return nil;
}
maxPlainLen = SecKeyGetBlockSize(publicKey) - 12;
return self;
}
- (NSData *) encryptWithData:(NSData *)content {
size_t plainLen = [content length];
if (plainLen > maxPlainLen) {
NSLog(@"content(%ld) is too long, must < %ld", plainLen, maxPlainLen);
return nil;
}
void *plain = malloc(plainLen);
[content getBytes:plain
length:plainLen];
size_t cipherLen = 128; // 当前RSA的密钥长度是128字节
void *cipher = malloc(cipherLen);
OSStatus returnCode = SecKeyEncrypt(publicKey, kSecPaddingPKCS1, plain,
plainLen, cipher, &cipherLen);
NSData *result = nil;
if (returnCode != 0) {
NSLog(@"SecKeyEncrypt fail. Error Code: %ld", returnCode);
}
else {
result = [NSData dataWithBytes:cipher
length:cipherLen];
}
free(plain);
free(cipher);
return result;
}
- (NSData *) encryptWithString:(NSString *)content {
return [self encryptWithData:[content dataUsingEncoding:NSUTF8StringEncoding]];
}
- (void)dealloc{
CFRelease(certificate);
CFRelease(trust);
CFRelease(policy);
CFRelease(publicKey);
}
@end
使用方法:
RSA *rsa = [[RSA alloc] init];
if (rsa != nil) {
NSLog(@"%@",[rsa encryptWithString:@"test"]);
}
else {
NSLog(@"init rsa error");
}
I. iphone怎么安装der证书
iphone5以上哪怕证书没有密码也是需要输入锁屏密码的,建议可以这样做:
打开手机的设置,点击touch id与密码;进入之后,点击更改密码,重新设定一个四位数的锁屏密码;
重启手机,回到安装证书界面,输入新的锁屏密码即可安装证书。
J. java 如何读取后缀为der证书中的内容,字符串显示
der证书的内容都是加密过的,读出来也是一串看不懂的字符串。读取方式用java一般的file操作即可