RAS加密

      /// <summary>  
        /// RAS加密。  
        /// </summary>  
        private string RSAEncrypt(string originalString)
        {
            CspParameters param = new CspParameters();
            param.KeyContainerName = "USER";

            using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(param))
            {
                byte[] plaindata = Encoding.Default.GetBytes(originalString);
                byte[] encryptdata = rsa.Encrypt(plaindata, false);

                return Convert.ToBase64String(encryptdata);
            }
        }

        /// <summary>  
        /// RAS解密。  
        /// </summary>  
        private string RSADecrypt(string securitylString)
        {
            CspParameters param = new CspParameters();
            param.KeyContainerName = "USER";
            using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(param))
            {
                byte[] encryptdata = Convert.FromBase64String(securitylString);
                byte[] decryptdata = rsa.Decrypt(encryptdata, false);
                return Encoding.Default.GetString(decryptdata);
            }
        } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,似乎是RSA加密算法,而不是RAS加密算法。以下是Python实现RSA加密算法的步骤和代码: 1. 生成公钥和私钥 ```python import random def gcd(a, b): while b != 0: a, b = b, a % b return a def multiplicative_inverse(e, phi): d = 0 x1 = 0 x2 = 1 y1 = 1 temp_phi = phi while e > 0: temp1 = temp_phi // e temp2 = temp_phi - temp1 * e temp_phi = e e = temp2 x = x2 - temp1 * x1 y = d - temp1 * y1 x2 = x1 x1 = x d = y1 y1 = y if temp_phi == 1: return d + phi def generate_keypair(p, q): n = p * q phi = (p-1) * (q-1) e = random.randrange(1, phi) g = gcd(e, phi) while g != 1: e = random.randrange(1, phi) g = gcd(e, phi) d = multiplicative_inverse(e, phi) return ((e, n), (d, n)) ``` 2. 加密和解密 ```python def encrypt(pk, plaintext): key, n = pk cipher = [pow(ord(char), key, n) for char in plaintext] return cipher def decrypt(pk, ciphertext): key, n = pk plain = [chr(pow(char, key, n)) for char in ciphertext] return ''.join(plain) ``` 3. 使用示例 ```python p = 61 q = 53 public_key, private_key = generate_keypair(p, q) print("Public key: ", public_key) print("Private key: ", private_key) message = "Hello, World!" encrypted_message = encrypt(public_key, message) print("Encrypted message: ", ''.join(map(lambda x: str(x), encrypted_message))) decrypted_message = decrypt(private_key, encrypted_message) print("Decrypted message: ", decrypted_message) ``` 输出: ``` Public key: (1913, 3233) Private key: (1783, 3233) Encrypted message: 246811111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值