程序收到一段如下文本

程序收到一段如下文本:
v=0
o=RTSP Session 00 IN IP4 0.0.0.0s=Media Server
c=IN IP40.0.0.0t=o o
a=control:-
a=packetization-supported:DH
rn=video 0 RTP/AVP 96
a=control trackID
a=framerate:25.000000
检验这段文本是否满足如下条件∶
1)除空白行外,每行都有一个等于号;2)紧挨着等于号的左右两边都不存在空格;
3)等于号左边只允许存在一个字母.其中v、o、s有且只有一个, m至少有一个. a的个数不能比m个数少.其他字母不做限制。
请写一段代码校验文本是否符合规定的所有条件。

str1 = """v=0
o= RTSP Session 00 IN IP4 0.0.0.0s=Media Server
c=IN IP40.0.0.0t=o o
a=control:-
s=packetization-supported:DH

a=control trackID
m=framerate:25.000000"""
import re
first_word = re.compile(r'^([a-zA-Z])=( ?)')
def check(text):
    word_list = []
    line_list = text.split('\n')
    try:
        for line in line_list:
            if line: # 忽略空行,当行有内容时按照正则内容匹配
            	# 使用正则匹配每一行内容
                line_content = first_word.findall(line)
                if line_content[0][1]:
                	# 判断=后面是否有空格字符
                    raise Exception(f'{line} 不符合规则,=右边有空格')
                word_list.append(line_content[0][0])
        # 将每行第一个字母加入word_list做数量统计
        v, o, s, m, a= word_list.count('v'), word_list.count('o'), word_list.count('s'), word_list.count('m'), word_list.count('a')
        if v==o==s==1 and a>=m and m >=1:
            print('数据符合规则')
        else:
            raise Exception('数据不符合规则,计数不符合要求')
    except Exception:
        raise Exception('数据不符合规则') # 捕获正则匹配后切片处理异常
check(str1)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是使用Java编写的Diffie-Hellman程序: ```java import java.math.BigInteger; import java.util.Random; public class DiffieHellman { private BigInteger p; // 共享质数 private BigInteger g; // 共享底数 private BigInteger a; // 发送方的私钥 private BigInteger b; // 接收方的私钥 private BigInteger A; // 发送方的公钥 private BigInteger B; // 接收方的公钥 private BigInteger K; // 协商出的密钥 public DiffieHellman(int numBits) { // 生成共享质数和底数 Random random = new Random(); p = BigInteger.probablePrime(numBits, random); g = new BigInteger(numBits, random).mod(p.subtract(BigInteger.ONE)).add(BigInteger.ONE); // 生成发送方和接收方的私钥 a = new BigInteger(numBits - 1, random); b = new BigInteger(numBits - 1, random); // 计算发送方和接收方的公钥 A = g.modPow(a, p); B = g.modPow(b, p); // 协商出密钥 K = B.modPow(a, p); } public BigInteger getP() { return p; } public BigInteger getG() { return g; } public BigInteger getA() { return a; } public BigInteger getB() { return b; } public BigInteger getAKey() { return A; } public BigInteger getBKey() { return B; } public BigInteger getKey() { return K; } public static void main(String[] args) { // 初始化DiffieHellman对象 DiffieHellman dh = new DiffieHellman(1024); // 输出共享质数、底数和协商出的密钥 System.out.println("共享质数p:" + dh.getP()); System.out.println("共享底数g:" + dh.getG()); System.out.println("协商出的密钥K:" + dh.getKey()); // 模拟发送方加密文件 String plaintext = "这是一段加密的文本。"; System.out.println("原文:" + plaintext); // 使用协商出的密钥对文本进行加密 byte[] keyBytes = dh.getKey().toByteArray(); byte[] plaintextBytes = plaintext.getBytes(); byte[] ciphertextBytes = new byte[plaintextBytes.length]; for (int i = 0; i < plaintextBytes.length; i++) { ciphertextBytes[i] = (byte) (plaintextBytes[i] ^ keyBytes[i % keyBytes.length]); } String ciphertext = new String(ciphertextBytes); System.out.println("密文:" + ciphertext); // 模拟接收方解密文件 // 使用协商出的密钥对密文进行解密 byte[] ciphertextBytes2 = ciphertext.getBytes(); byte[] plaintextBytes2 = new byte[ciphertextBytes2.length]; for (int i = 0; i < ciphertextBytes2.length; i++) { plaintextBytes2[i] = (byte) (ciphertextBytes2[i] ^ keyBytes[i % keyBytes.length]); } String plaintext2 = new String(plaintextBytes2); System.out.println("解密后的文本:" + plaintext2); } } ``` 该程序使用了BigInteger类来处理大数,通过生成共享质数和底数、发送方和接收方的私钥、发送方和接收方的公钥,并协商出密钥。然后,模拟发送方加密文件,使用协商出的密钥对文本进行加密。最后,模拟接收方解密文件,使用协商出的密钥对密文进行解密。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值