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操作即可