标签: rsa

用于C/C++中RSA实现的开源代码(使用库或自己编写)

我需要用于RSA实现的开源代码(加密/解密和其他).谁能提出一些建议.

编辑:使用像opessl这样的开源库或者自己编写它是否很好(库也包含其他冗余的东西)

c c++ cryptography rsa public-key-encryption

1
推荐指数
1
解决办法
3万
查看次数

C++中这行代码的含义

我刚开始拿起C,我正在使用代码中的RSA密码.但是,这行代码让我很困惑.积分在此处从此站点发送给作者.

char* intmsg = new char[strlen(msg)*3 + 1];
Run Code Online (Sandbox Code Playgroud)

这是可以找到该行的方法.

inline void encrypt(char* msg,FILE* fout)
{
    /* This function actually does the encrypting of each message */

    unsigned int i;
    int tmp;
    char tmps[4];
    char* intmsg = new char[strlen(msg)*3 + 1];


    /* Here, (mpz_t) M is the messsage in gmp integer 
    *  and (mpz_t) c is the cipher in gmp integer */

    char ciphertext[1000];

    strcpy(intmsg,"");

    for(i=0;i<strlen(msg);i++)
    {
        tmp = (int)msg[i];

        /* print it in a 3 character wide format …
Run Code Online (Sandbox Code Playgroud)

c++ rsa

1
推荐指数
1
解决办法
322
查看次数

使用RSA在C#中使用正确的私钥和公钥对进行解密时出现错误数据异常

这是我的解密过程代码:

    private RSACryptoServiceProvider _rsa;
    private string _privateKey;
    private string _publicKey;

    public RsaLibrary()
    {
        //initialsing the RSA object taking the option of a 1024 key size
        _rsa = new RSACryptoServiceProvider(1024);
        _privateKey = _rsa.ToXmlString(true);
        _publicKey = _rsa.ToXmlString(false);

    }

 public string Decrypt(string ciphertext, string privateKey_ = null)
    {
        if (String.IsNullOrEmpty(privateKey_))
        {
            return DecryptToBytes(ciphertext, _privateKey);
        }
        else
        {
            return DecryptToBytes(ciphertext, privateKey_);
        }
    }

    private string DecryptToBytes(string ciphertext, string privateKey)
    {
        if (String.IsNullOrEmpty(privateKey))
        {
            throw new ArgumentNullException("Error: No key provided.");
        }
        if (ciphertext.Length<=0)
        {
            throw …
Run Code Online (Sandbox Code Playgroud)

c# rsa rsacryptoserviceprovider

1
推荐指数
1
解决办法
4913
查看次数

Golang令牌验证错误

我需要验证谷歌id_token,一步涉及检查令牌签名.

首先,我从以下网址获取证书:https://www.googleapis.com/oauth2/v2/certs并从证书中提取模数(n)和指数(e)部分并生成公钥,然后我拆开令牌(标题,有效负载和摘要),然后我将解码header.payload与谷歌pKey +摘要一起发送到rsa函数rsa.VerifyPKCS1v15.

我遇到了这个验证错误: crypto/rsa: verification error

这是代码(我评论了失败的部分代码// validation here fails):

func ValidateIDToken(auth_token string) (err error){    
    res, err := http.Get("https://www.googleapis.com/oauth2/v2/certs")
    if err != nil {
        log.Fatal(err)
        return err
    }

    certs, err := ioutil.ReadAll(res.Body)
    res.Body.Close()
    if err != nil {
        log.Fatal(err)
        return err
    }

    //get modulus and exponent from the cert
    var goCertificate interface{}

    err = json.Unmarshal(certs, &goCertificate)    
    k := goCertificate.(map[string]interface{})["keys"]
    j := k.([]interface{})
    x := j[1]
    h := …
Run Code Online (Sandbox Code Playgroud)

rsa go oauth-2.0 jwt google-oauth

1
推荐指数
1
解决办法
2212
查看次数

无法在iOS Swift中获取SecKey UnsafeMutablePointer

我正在一个涉及RSA加密的swift项目中工作,我遇到了如下指针问题:

我有一个全局var publicKey: SecKey?可选值,我需要获取UnsafeMutablePointer<Unmanaged<SecKey>?>指向它的指针,这是SecKeyGeneratePair函数中所需的数据类型 .

我试图将指针定义为:

var keyPointer = UnsafeMutablePointer<Unmanaged<SecKey>?>(publicKey!)
Run Code Online (Sandbox Code Playgroud)

但是编译器抱怨a 不能使用@lvalue SecKey类型的参数调用'init'错误

根据使用Swift with Cocoa和Objective-C的书,在Core FoundationUnmanaged Objects部分中,它声明该Unmanaged<T>结构提供了2种方法takeUnretainedValue()takeRetainedValue().但试图实现它们,会出现以下错误

var keyPointer = UnsafeMutablePointer<Unmanaged<SecKey>?>(publicKey!.takeRetainedValue())
Run Code Online (Sandbox Code Playgroud)

'SecKey'没有名为'takeRetainedValue'的成员

任何帮助解决这个问题将不胜感激

encryption rsa unmanaged ios swift

1
推荐指数
1
解决办法
1370
查看次数

如何从未格式化的String获取公共RSA密钥

我在属性文件中有一个未格式化的公钥,这意味着它只包含公钥hexa值:

K_PUB_CCE =  3082010902820100A515281FAC9ABAA8E966DC1B6EC0F1C431674B4E7BCB718955A34211D5CC6BA53F2C93F67C030A970D4E41341949E6BC3F9336287EEA21702FE663C83D4BAFEE2AAA2EEE7A6AFDC423A159D420E42ABDE2792080249C5D6E25367F804333665CAFB79FD5A59D70B9F69159F4EDAD2DA4B434F41D0EC5217808E7D91FF547C83774E4BDE813302E16377156E52CAF02D1E68371D536AA0E7E32DE484FF4863538DCBC69E8D3E3C1F3CECEA9861DA5516A06D3208F6363B86CF66641BE18C4F41ABD7F1B5CDC9BD964914515DDC58F32F49437BD7E89431C8F484BBCEBA86A5FFF74E01D12E6D1D7EBBF6E5DCD7A9134BF27185F4BD347B23007FF366C0009E14F0203010001
Run Code Online (Sandbox Code Playgroud)

如你所见,它的长度为538六进制.我想要实现的是获取具有此值的java.security.PublicKey.

但是当使用这种方法时:

private PublicKey createPublicKey(String stringPublicKey) throws NoSuchAlgorithmException, InvalidKeySpecException {
    byte[] bytesKey= Hex.decodeHex(stringPublicKey.toCharArray());
    X509EncodedKeySpec encodedKeySpec = new X509EncodedKeySpec(bytesKey);
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    return  keyFactory.generatePublic(encodedKeySpec);
}
Run Code Online (Sandbox Code Playgroud)

我得到以下异常:

java.security.InvalidKeyException: IOException: algid parse error, not a sequence
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?我应该使用其他任何关键规范类吗?

java rsa

1
推荐指数
1
解决办法
3997
查看次数

RSA加密/解密不返回原始消息

初学者,尝试一些RSA加密.我写了一个python代码,大部分时间都返回正确的消息,但有时加密和解密不会返回原始消息.

我虽然在我的代码中出现了一些错误,但是一些在线资源也返回错误:

http://extranet.cryptomathic.com/rsacalc/index

https://www.cs.drexel.edu/~jpopyack/IntroCS/HW/RSAWorksheet.html

选择的参数是:

p = 11

q = 269

n = 2959

e = 13

d = 1237

消息= 13355

crypt text = 1079

解密= 1519

我错过了对RSA的某种限制吗?文本的一些最小参数大小?

python encryption cryptography rsa

1
推荐指数
1
解决办法
326
查看次数

如何使RSA与更大的p和q值一起工作?

我的项目是使用由讲师自己给出的C语言编写的骨架来实现RSA.我不熟悉C但是能够做到.但问题是它只有在以下情况下才有效:

p <200且q <200
当值超过该值时,它将失败.我想让p和q大于那个数字.我的代码是否有问题导致它仅在p和q小于200时才起作用或成功.

//unsigned int
uint p, q, e, d, n;
Run Code Online (Sandbox Code Playgroud)

加密/解密时模幂运算的功能:

uint ModPow(uint base, uint exp, uint n) {
uint h;
uint r;
int bin[32];
int i;

r=base;
i=0;

while(exp > 0)
{
    if(exp%2 == 0)
    {
        bin[i] = 0;
    }else{
        bin[i] = 1;
    }
    exp = exp/2;
    i++;
}
i--;
while(i>0)
{
    r = (r*r)%n;
    if(bin[--i]==1)
    {
        r = (r*base)%n;
    }
}
printf("r:%d n:%d\n", r,n );
return r;
} 
Run Code Online (Sandbox Code Playgroud)

检查GCD(a,b)的功能:

uint GCD(uint a, uint b) { …
Run Code Online (Sandbox Code Playgroud)

c encryption cryptography rsa

1
推荐指数
1
解决办法
265
查看次数

密钥库操作因RSA签名和验证失败

我很难使用RSA进行签名和验证.以下是RSA我使用的课程的一部分.这些方法init(),privateKey()并且和方法publicKey()一起工作(这里没有列出最后两个),所以我假设我在我的方法中犯了一些错误.方法尚未测试,因为签名不成功,所以我无法进一步移动.请看一下...encrypt()decrypt()sign()verify()


我的RSA班级是:

class RSA
{
    private final static String ANDROID_KEY_STORE = "AndroidKeyStore";

    public final static int ANY_PURPOSE = KeyProperties.PURPOSE_ENCRYPT | 
            KeyProperties.PURPOSE_DECRYPT | KeyProperties.PURPOSE_SIGN | 
            KeyProperties.PURPOSE_VERIFY;

    public final static long CENTURY = (100 * 365) + 24;
Run Code Online (Sandbox Code Playgroud)
    public enum KeySize
    {
        BIT_512  ( 512),
        BIT_768  ( 768),
        BIT_1024 (1024),
        BIT_2048 (2048),
        BIT_3072 (3072),
        BIT_4096 (4096);

        private int value;

        KeySize(int value)
        {
            this.value = value;
        }

        public int …
Run Code Online (Sandbox Code Playgroud)

java encryption android cryptography rsa

1
推荐指数
1
解决办法
702
查看次数

Java RSA密码术:RSA模数的素数很小

尝试使用RSA加密消息:我一直收到此错误,但是我不确定这意味着什么:

生成密钥的代码

    generator = KeyPairGenerator.getInstance("RSA");
    RSAKeyGenParameterSpec kpgSpec = new RSAKeyGenParameterSpec(2048, BigInteger.valueOf(17489));
    generator.initialize(kpgSpec);

    KeyPair keyPair = generator.generateKeyPair();

    publicKey = (RSAPublicKey) keyPair.getPublic();
    privateKey = (RSAPrivateKey) keyPair.getPrivate();
Run Code Online (Sandbox Code Playgroud)

编码消息的代码

        RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(new BigInteger(publicKeyBytes), BigInteger.valueOf(17489));

    Cipher cipher;
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    PublicKey currentKey = (RSAPublicKey) keyFactory.generatePublic(pubKeySpec);
    cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.ENCRYPT_MODE, currentKey);
    byte[] encryptedBytes = cipher.doFinal(data.getBytes());
    encrypted = bytesToString(encryptedBytes);
Run Code Online (Sandbox Code Playgroud)

错误:

W/System.err: java.lang.IllegalArgumentException: RSA modulus has a small prime factor
W/System.err:     at com.android.org.bouncycastle.crypto.params.RSAKeyParameters.validate(RSAKeyParameters.java:46)
    at com.android.org.bouncycastle.crypto.params.RSAKeyParameters.<init>(RSAKeyParameters.java:28)
    at com.android.org.bouncycastle.jcajce.provider.asymmetric.rsa.RSAUtil.generatePublicKeyParameter(RSAUtil.java:44)
    at com.android.org.bouncycastle.jcajce.provider.asymmetric.rsa.CipherSpi.engineInit(CipherSpi.java:288)
    at com.android.org.bouncycastle.jcajce.provider.asymmetric.rsa.CipherSpi.engineInit(CipherSpi.java:406)
Run Code Online (Sandbox Code Playgroud)

java encryption cryptography rsa public-key-encryption

1
推荐指数
1
解决办法
134
查看次数