如何对字符(打字稿)进行基本加密?

发布于 2025-01-29 12:10:20 字数 1695 浏览 2 评论 0原文

我想加密使用RSA公共和私钥的字符串。但是,当我尝试将角色解密到那里的初始ASCII-Value时,它们会返回一些不同的东西。这是我加密和解密的方法:

/**
 * Method to encrypt a string using associated public-key
 * @param plainText string to cipher
 * @returns string of encrypted plainText
 */
public encryptData(plainText: string): string {
    let cipher = "";
    for (let i = 0; i < plainText.length; i++) {
        console.log(plainText.charCodeAt(i));
        let temp: number = Math.pow(plainText.charCodeAt(i), this.e) % this.n;
        console.log(String.fromCharCode(temp).charCodeAt(i));
        cipher += String.fromCharCode(temp);
    }
    return cipher;
}

/**
 * Method to decrypt a string using associated private-key
 * @param cipherText string to decrypt
 * @returns string of encrypted plainText
 */
public decryptData(cipherText: string): string {
    let text = "";
    for (let i = 0; i < cipherText.length; i++) {
        console.log(cipherText.charCodeAt(i));
        let temp: number = Math.pow(cipherText.charCodeAt(i), this.d) % this.n;
        text += String.fromCharCode(temp);
    }
    return text;
}

ned分别为15、7和13。对此的任何建议将不胜感激!

编辑

找到了解决该问题的解决方案,在创建变量temp时使用以下方法。

private modular_pow(base: number, expo: number, modulo: number) {
    base = base % modulo;
    var result = 1;
    var x = base;
    while(expo > 0){
        var leastSignificantBit = expo % 2;
        expo = Math.floor(expo / 2);
        if (leastSignificantBit == 1) {
            result = result * x;
            result = result % modulo;
        }
        x = x * x;
        x = x % modulo;
    }
    return result;
}

I want to encrypt a string with RSA public and private keys. However, when I try to decrypt the characters back to there initial ascii-value, they return something different. Here are my methods for encrypting and decrypting:

/**
 * Method to encrypt a string using associated public-key
 * @param plainText string to cipher
 * @returns string of encrypted plainText
 */
public encryptData(plainText: string): string {
    let cipher = "";
    for (let i = 0; i < plainText.length; i++) {
        console.log(plainText.charCodeAt(i));
        let temp: number = Math.pow(plainText.charCodeAt(i), this.e) % this.n;
        console.log(String.fromCharCode(temp).charCodeAt(i));
        cipher += String.fromCharCode(temp);
    }
    return cipher;
}

/**
 * Method to decrypt a string using associated private-key
 * @param cipherText string to decrypt
 * @returns string of encrypted plainText
 */
public decryptData(cipherText: string): string {
    let text = "";
    for (let i = 0; i < cipherText.length; i++) {
        console.log(cipherText.charCodeAt(i));
        let temp: number = Math.pow(cipherText.charCodeAt(i), this.d) % this.n;
        text += String.fromCharCode(temp);
    }
    return text;
}

n, e and d are 15, 7 and 13 respectively. Any advice on this would be greatly appreciated!

EDIT

Found a solution to the problem, use the following in method when creating variable temp.

private modular_pow(base: number, expo: number, modulo: number) {
    base = base % modulo;
    var result = 1;
    var x = base;
    while(expo > 0){
        var leastSignificantBit = expo % 2;
        expo = Math.floor(expo / 2);
        if (leastSignificantBit == 1) {
            result = result * x;
            result = result % modulo;
        }
        x = x * x;
        x = x % modulo;
    }
    return result;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瀞厅☆埖开 2025-02-05 12:10:20

模量N的大小决定了教科书RSA的最大有效载荷。由于值15,消息值必须为14或更低。

字符通常至少在0..25范围内,这不是字符值,而是字母内的索引。因此,要么您将字符分开,要么需要使用较大的模量(例如p = 7,q = 19,所有重要的n = 133可以处理任何ASCII角色(当然可以在可打印之外徘徊 )。

ASCII对于某些值 “ nofollow noreferrer”>一种专业的算法,而不是首先执行指数,然后进行模量计算,这很效率,并且可能会导致整数溢出(或类似的东西)在凸起步骤中

也要注意 。在您的方案中,您的教科书RSA必须放入(可打印的)角色中。

The size of the modulus N decides the maximum payload for textbook RSA. As it is the value 15 the message value must be 14 or lower.

Characters are commonly at least in the range 0..25 and that's not the character value but the index in the alphabet. So either you split up the characters even further or you need to use a larger modulus (such as p = 7, q = 19 and the all important n=133 which would be able to handle any ASCII character (of course wandering outside of printable ASCII for some values).

Beware that if you have larger components of your RSA that it becomes imperative to perform modular exponentiation using a specialized algorithm, instead of first performing exponentiation followed by a modulus calculation, which is very inefficient and will likely cause an integer overflow (or something similar) during the exponentiation step.

Note too that the output of your textbook RSA must be put into a (printable) character in your scheme. For practice you could also use just the numbers separated by space, ignoring the growth of the ciphertext.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文