跳至正文

der格式密钥,密钥用什么格式

java中的 desedekeyspec spec;是什么意思

java中的 desedekeyspec spec;是什么意思

desedekeyspec是一个类. 即: public class DESedeKeySpec extends Object implements KeySpec 此类指定一个 DES-EDE ("triple-DES") 密钥. 构造方法: DESedeKeySpec(byte[] key) 创建一个 DESedeKeySpec 对象,使用 key 中的前 24 个字节作为 DES-EDE 密钥的密钥内容. DESedeKeySpec(byte[] key, int offset) 创建一个 DESedeKeySpec 对象,使用 key 中始于且包含 offset 的前 24 个字节作为 DES-EDE 密钥的密钥内容.

我怎样才能获得SecKeyRef从DER / PEM文件

我怎样才能获得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 @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 = SecTrustCreateWithCertificates(certificate, policy, &trust); if (returnCode != 0) { NSLog(@”SecTrustCreateWithCertificates 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 @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 = SecTrustCreateWithCertificates(certificate, policy, &trust); if (returnCode != 0) { NSLog(@"SecTrustCreateWithCertificates 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"); }

对称加密算法中,des算法的密钥长度是多少,采用什么进行加密

对称加密算法中,des算法的密钥长度是多少,采用什么进行加密

DES使用56位密钥对64位的数据块进行加密,并对64位的数据块进行16轮编码。与每轮编码时,一个48位的“每轮”密钥值由56位的完整密钥得出来。DES用软件进行解码需要用很长时间,而用硬件解码速度非常快,但幸运的是当时大多数黑客并没有足够的设备制造出这种硬件设备。在1977年,人们估计要耗资两千万美元才能建成一个专门计算机用于DES的解密,而且需要12个小时的破解才能得到结果。所以,当时DES被认为是一种十分强壮的加密方法。

但是,当今的计算机速度越来越快了,制造一台这样特殊的机器的花费已经降到了十万美元左右,所以用它来保护十亿美元的银行间线缆时,就会仔细考虑了。另一个方面,如果只用它来保护一台服务器,那么DES确实是一种好的办法,因为黑客绝不会仅仅为入侵一个服务器而花那么多的钱破解DES密文。由于现在已经能用二十万美圆制造一台破译DES的特殊的计算机,所以现在再对要求“强壮”加密的场合已经不再适用了。

三重DES

因为确定一种新的加密法是否真的安全是极为困难的,而且DES的唯一密码学缺点,就是密钥长度相对比较短,所以人们并没有放弃使用DES,而是想出了一个解决其长度问题的方法,即采用三重DES。这种方法用两个密钥对明文进行三次加密,假设两个密钥是K1和K2,其算法的步骤如图5.9所示:

1. 用密钥K1进行DEA加密。

2. 用K2对步骤1的结果进行DES解密。

3. 用步骤2的结果使用密钥K1进行DES加密。

这种方法的缺点,是要花费原来三倍时间,从另一方面来看,三重DES的112位密钥长度是很“强壮”的加密方式了

如何查看证书的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编码了。

DERCO CODE什么意思

DERCO CODE 译(码),解(码); 分析及译解电子信号 很高兴第一时间为您解答,祝学习进步 如有问题请及时追问,谢谢~~O(∩_∩)O

如何用C++实现读取证书文件中的公钥数据?

在C++下,我使用OPENSSL库生成了RSA的公私钥对与DES加密之用的会话密钥,并将这三者及加密结果写入文件以备在Java环境下使用。

在C++程序中,我使用使用公钥来加密了DES的会话密钥,然后在Java下使用私钥来解密会话密钥;在运算结果中,我未做其它方面的码制转换,即按密钥的初始格式DER编码,数学运算结果也是按DER编码来实现。

在Java程序中,我从之前所存储的几个文件中取得密钥与加密结果来做解密。我使用了BC的JCE,即bcprov-jdk14-119.jar,在使用之前,需要先安装此JCE:

假设JDK:jdk1.4\jre\

把BC包放到JRE下的ext:jdk1.4\jre\lib\ext

修改文件jdk1.4\jre\lib\security\java.security:

#

# List of providers and their preference orders (see above):

#

security.provider.1=sun.security.provider.Sun

security.provider.2=com.sun.net.ssl.internal.ssl.Provider

security.provider.3=com.sun.rsajca.Provider

security.provider.4=com.sun.crypto.provider.SunJCE

security.provider.5=sun.security.jgss.SunProvider

security.provider.6=org.bouncycastle.jce.provider.BouncyCastleProvider

======================================================================

C++程序源码:

#include

#include

#include

//#define _RSA_KEY_PAIR_GENERATE_//密钥是否要生成 只需要在第一次运行时打开此宏

#define _RSA_KEY_PAIR_TOFILE_//密钥对是否要写入文件

#define MAX_RSA_KEY_LENGTH 512 //密钥的最大长度是512字节

#define PUBKEY_ENCRYPT

#define PRIKEY_DECRYPT

#pragma comment(lib, “../lib/libeay32.lib”)

static const char * PUBLIC_KEY_FILE = “pubkey.key”;

static const char * PRIVATE_KEY_FILE = “prikey.key”;

int RsaKeyPairGen(void)

{

RSA *rsa = NULL;

#ifdef_RSA_KEY_PAIR_GENERATE_

//生成RSA密钥对:

rsa = RSA_new();

rsa = RSA_generate_key(1024, 0x10001, NULL, NULL);

#endif

//把密钥对写入文件,以后从文件里读取

#ifdef _RSA_KEY_PAIR_TOFILE_

unsigned char ucPubKey[MAX_RSA_KEY_LENGTH] = {0}, ucPriKey[MAX_RSA_KEY_LENGTH] = {0};

int len = i2d_RSAPublicKey(rsa,NULL);

unsigned char* pt = ucPubKey;

len = i2d_RSAPublicKey(rsa, &pt);

FILE *fpubkey = NULL;

fpubkey = fopen(PUBLIC_KEY_FILE, “wb”);

if(fpubkey == NULL)

{

cout << "fopen pubkey.key failed!" << endl;

return 0x01;

}

fwrite(ucPubKey, 1, len, fpubkey);

fclose(fpubkey);

len = i2d_RSAPrivateKey(rsa,NULL);

unsigned char* pt2 = ucPriKey;

len = i2d_RSAPrivateKey(rsa,&pt2);

FILE *fprikey = NULL;

fprikey = fopen(PRIVATE_KEY_FILE, “wb”);

if(fprikey == NULL)

{

cout << "fopen prikey.key failed!" << endl;

return 0x02;

}

fwrite(ucPriKey, 1, len, fprikey);

fclose(fprikey);

#endif

if(rsa != NULL)

{

RSA_free(rsa);

rsa = NULL;

}

return 0;

}

//从文件里读取私钥的数据,取得RSA格式的私钥:

int GetPriKey(unsigned char *pucPriKeyData, unsigned long KeyDataLen, RSA* *priRsa)

{

unsigned char *Pt = pucPriKeyData;

*priRsa = d2i_RSAPrivateKey(NULL, &Pt, KeyDataLen);

if(priRsa == NULL)

{

cout << "priRsa == NULL!" << endl;

return 0x22;

}

return 0;

}

//取得RSA格式的公钥:

int GetPubKey(unsigned char *pucPubKeyData,unsigned long KeyDataLen, RSA* *pubRsa)

{

unsigned char *Pt = pucPubKeyData;

*pubRsa = d2i_RSAPublicKey(NULL, &Pt, KeyDataLen);

if(pubRsa == NULL)

{

cout << "pubRsa == NULL!" << endl;

return 0x31;

}

return 0;

}

//公钥加密会话密钥:

int encSessionKeybyRsaPubKey(RSA *rsa, unsigned char *ucKey, unsigned long ulKeyLen,

unsigned char *outData, unsigned long *pulOutLen)

{

return (*pulOutLen = RSA_public_encrypt(ulKeyLen, ucKey, outData, rsa, 1));

}

//私钥解密会话密钥:

int decSessionKeybyRsaPriKey(RSA *rsa, unsigned char *InData, unsigned long ulDataLen,

unsigned char *ucKey, unsigned long *pulKeyLen)

{

return (*pulKeyLen = RSA_private_decrypt(ulDataLen, InData, ucKey, rsa, 1));

}

int main(int argc, char* argv[])

{

unsigned char ucKey[8] = {0x01, 0x03, 0x99, 0x4, \

0x80, 0x65, 0x34, 0x08};

unsigned char ucEncryptedKey[512] = {0}, ucDecryptedKey[512] = {0};

unsigned long encrypted_len = 0, decrypted_len = 0;

#ifdef _RSA_KEY_PAIR_GENERATE_

RsaKeyPairGen();

#endif

//取得公钥:

unsigned char ucPubKey[MAX_RSA_KEY_LENGTH] = {0};

FILE *fpubkey = NULL;

fpubkey = fopen(PUBLIC_KEY_FILE, “rb”);

if(fpubkey == NULL)

{

cout << "fopen pubkey.key failed!" << endl;

return 0x03;

}

fseek(fpubkey, 0, SEEK_END);

int len_PK = ftell(fpubkey);

fseek(fpubkey, 0, SEEK_SET);

fread(ucPubKey, 1, len_PK, fpubkey);

fclose(fpubkey);

#ifdef PUBKEY_ENCRYPT

RSA *pRsaPubKey = NULL;

pRsaPubKey = RSA_new();

GetPubKey(ucPubKey, len_PK, &pRsaPubKey);

//公钥加密:

encSessionKeybyRsaPubKey(pRsaPubKey, ucKey, sizeof(ucKey), ucEncryptedKey, &encrypted_len);

//write to file:

FILE *fp = NULL;

fp = fopen(“ucKey.data”, “wb”);

fwrite(ucEncryptedKey, 1, encrypted_len, fp);

fclose(fp);

if(pRsaPubKey != NULL)

{

RSA_free(pRsaPubKey); pRsaPubKey = NULL;

}

#endif

//取得私钥:

unsigned char ucPriKey[MAX_RSA_KEY_LENGTH] = {0};

FILE *fprikey = NULL;

fprikey = fopen(PRIVATE_KEY_FILE, “rb”);

if(fprikey == NULL)

{

cout << "fopen prikey.key failed!" << endl;

return 0x02;

}

fseek(fprikey, 0, SEEK_END);

int len_SK = ftell(fprikey);

fseek(fprikey, 0, SEEK_SET);

fread(ucPriKey, 1, len_SK, fprikey);

fclose(fprikey);

#ifdef PRIKEY_DECRYPT

RSA *pRsaPriKey = NULL;

pRsaPriKey = RSA_new();

GetPriKey(ucPriKey, len_SK, &pRsaPriKey);

//私钥解密:

FILE *fp1 = NULL;

fp1 = fopen(“ucKey.data”, “rb”);

int len = ftell(fp1);

fseek(fp1, 0, SEEK_SET);

fread(ucPriKey, 1, len_SK, fp1);

fclose(fp1);

decSessionKeybyRsaPriKey(pRsaPriKey, ucEncryptedKey, encrypted_len, ucDecryptedKey, &decrypted_len);

if(pRsaPriKey != NULL)

{

RSA_free(pRsaPriKey); pRsaPriKey = NULL;

}

//数据对比:

if(0 == memcmp(ucKey, ucDecryptedKey, decrypted_len))

{

cout << "OK!" << endl;

}

else

{

cout << "FAILED!" << endl;

}

#endif

return 0;

}

如何编程读取PKCS#12格式的证书与私钥

pkcs8格式的私钥转换工具。它处理在PKCS#8格式中的私钥文件。它可以用多样的PKCS#5 (v1.5 and v2.0)和 PKCS#12算法来处理没有解密的PKCS#8 PrivateKeyInfo格式和EncryptedPrivateKeyInfo格式。

用法:

[cpp] view plaincopy

openssl pkcs8 [-inform PEM|DER] [-outform PEM|DER] [-in filename] [-passin arg] [-out filename]

[-passout arg] [-topk8] [-noiter] [-nocrypt] [-nooct] [-embed] [-nsdb] [-v2 alg] [-v1 alg] [-engine id]

选项说明:

-inform PEM|DER::输入文件格式,DER或者PEM格式。DER格式采用ASN1的DER标准格式。一般用的多的都是PEM格式,就是base64编码格式。

-outform DER|PEM:输出文件格式,DER或者PEM格式。

-in filename:输入的密钥文件,默认为标准输入。如果密钥被加密,会提示输入一个密钥口令。

-passin arg:输入文件口令保护来源。

-out filename:输出文件,默认为标准输出。如果任何加密操作已经执行,会提示输入一个密钥值。输出的文件名字不能和输入的文件名一样。

-passout arg:输出文件口令保护来源。

der格式是什么?有什么利弊

http://baike.baidu.com/view/100318.htm 百科一下你可以看到很多解释,希望能有你需要的答案

der怎么写?

只有发音,没有相对应的字.der在北方方言里有傻的意思.“懒得der你”应该是“懒得搭理你”的意思.

java/security/spec/KeySpec

KeySpec是一个接口.

中文JDK的解释如下:

public interface KeySpec组成加密密钥的密钥内容的(透明)规范。

如果密钥存储在硬件设备上,则其规范可以包含有助于标识该设备上的密钥的信息。

用特定于算法的方法或独立于算法的编码格式(例如,ASN.1)可以指定密钥。例如,DSA 专用密钥可以由其组件 x、p、q 和 g 指定(请参见 DSAPrivateKeySpec),或使用其 DER 编码指定(请参见 PKCS8EncodedKeySpec)。

此接口不包含任何方法或常量。它仅用于将所有密钥规范分组,并为其提供类型安全。所有密钥规范都必须实现此接口。

包你都写出来了.就这个java.security.spec